itsi-scheduler 0.1.11 → 0.1.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. checksums.yaml +4 -4
  2. data/CODE_OF_CONDUCT.md +7 -0
  3. data/Cargo.lock +75 -14
  4. data/README.md +5 -0
  5. data/_index.md +7 -0
  6. data/ext/itsi_error/src/lib.rs +9 -0
  7. data/ext/itsi_error/target/debug/build/clang-sys-da71b0344e568175/out/common.rs +355 -0
  8. data/ext/itsi_error/target/debug/build/clang-sys-da71b0344e568175/out/dynamic.rs +276 -0
  9. data/ext/itsi_error/target/debug/build/clang-sys-da71b0344e568175/out/macros.rs +49 -0
  10. data/ext/itsi_error/target/debug/build/rb-sys-49f554618693db24/out/bindings-0.9.110-mri-arm64-darwin23-3.4.2.rs +8865 -0
  11. data/ext/itsi_error/target/debug/incremental/itsi_error-1mmt5sux7jb0i/s-h510z7m8v9-0bxu7yd.lock +0 -0
  12. data/ext/itsi_error/target/debug/incremental/itsi_error-2vn3jey74oiw0/s-h5113n0e7e-1v5qzs6.lock +0 -0
  13. data/ext/itsi_error/target/debug/incremental/itsi_error-37uv9dicz7awp/s-h510ykifhe-0tbnep2.lock +0 -0
  14. data/ext/itsi_error/target/debug/incremental/itsi_error-37uv9dicz7awp/s-h510yyocpj-0tz7ug7.lock +0 -0
  15. data/ext/itsi_error/target/debug/incremental/itsi_error-37uv9dicz7awp/s-h510z0xc8g-14ol18k.lock +0 -0
  16. data/ext/itsi_error/target/debug/incremental/itsi_error-3g5qf4y7d54uj/s-h5113n0e7d-1trk8on.lock +0 -0
  17. data/ext/itsi_error/target/debug/incremental/itsi_error-3lpfftm45d3e2/s-h510z7m8r3-1pxp20o.lock +0 -0
  18. data/ext/itsi_error/target/debug/incremental/itsi_error-3o4qownhl3d7n/s-h510ykifek-1uxasnk.lock +0 -0
  19. data/ext/itsi_error/target/debug/incremental/itsi_error-3o4qownhl3d7n/s-h510yyocki-11u37qm.lock +0 -0
  20. data/ext/itsi_error/target/debug/incremental/itsi_error-3o4qownhl3d7n/s-h510z0xc93-0pmy0zm.lock +0 -0
  21. data/ext/itsi_rb_helpers/Cargo.toml +1 -0
  22. data/ext/itsi_rb_helpers/src/heap_value.rs +18 -0
  23. data/ext/itsi_rb_helpers/src/lib.rs +34 -7
  24. data/ext/itsi_rb_helpers/target/debug/build/clang-sys-da71b0344e568175/out/common.rs +355 -0
  25. data/ext/itsi_rb_helpers/target/debug/build/clang-sys-da71b0344e568175/out/dynamic.rs +276 -0
  26. data/ext/itsi_rb_helpers/target/debug/build/clang-sys-da71b0344e568175/out/macros.rs +49 -0
  27. data/ext/itsi_rb_helpers/target/debug/build/rb-sys-eb9ed4ff3a60f995/out/bindings-0.9.110-mri-arm64-darwin23-3.4.2.rs +8865 -0
  28. data/ext/itsi_rb_helpers/target/debug/incremental/itsi_rb_helpers-040pxg6yhb3g3/s-h5113n7a1b-03bwlt4.lock +0 -0
  29. data/ext/itsi_rb_helpers/target/debug/incremental/itsi_rb_helpers-131g1u4dzkt1a/s-h51113xnh3-1eik1ip.lock +0 -0
  30. data/ext/itsi_rb_helpers/target/debug/incremental/itsi_rb_helpers-131g1u4dzkt1a/s-h5111704jj-0g4rj8x.lock +0 -0
  31. data/ext/itsi_rb_helpers/target/debug/incremental/itsi_rb_helpers-1q2d3drtxrzs5/s-h5113n79yl-0bxcqc5.lock +0 -0
  32. data/ext/itsi_rb_helpers/target/debug/incremental/itsi_rb_helpers-374a9h7ovycj0/s-h51113xoox-10de2hp.lock +0 -0
  33. data/ext/itsi_rb_helpers/target/debug/incremental/itsi_rb_helpers-374a9h7ovycj0/s-h5111704w7-0vdq7gq.lock +0 -0
  34. data/ext/itsi_server/Cargo.toml +69 -30
  35. data/ext/itsi_server/src/lib.rs +79 -147
  36. data/ext/itsi_server/src/{body_proxy → ruby_types/itsi_body_proxy}/big_bytes.rs +10 -5
  37. data/ext/itsi_server/src/{body_proxy/itsi_body_proxy.rs → ruby_types/itsi_body_proxy/mod.rs} +22 -3
  38. data/ext/itsi_server/src/ruby_types/itsi_grpc_request.rs +147 -0
  39. data/ext/itsi_server/src/ruby_types/itsi_grpc_response.rs +19 -0
  40. data/ext/itsi_server/src/ruby_types/itsi_grpc_stream/mod.rs +216 -0
  41. data/ext/itsi_server/src/{request/itsi_request.rs → ruby_types/itsi_http_request.rs} +101 -117
  42. data/ext/itsi_server/src/{response/itsi_response.rs → ruby_types/itsi_http_response.rs} +72 -41
  43. data/ext/itsi_server/src/ruby_types/itsi_server/file_watcher.rs +225 -0
  44. data/ext/itsi_server/src/ruby_types/itsi_server/itsi_server_config.rs +355 -0
  45. data/ext/itsi_server/src/ruby_types/itsi_server.rs +82 -0
  46. data/ext/itsi_server/src/ruby_types/mod.rs +55 -0
  47. data/ext/itsi_server/src/server/bind.rs +13 -5
  48. data/ext/itsi_server/src/server/byte_frame.rs +32 -0
  49. data/ext/itsi_server/src/server/cache_store.rs +74 -0
  50. data/ext/itsi_server/src/server/itsi_service.rs +172 -0
  51. data/ext/itsi_server/src/server/lifecycle_event.rs +3 -0
  52. data/ext/itsi_server/src/server/listener.rs +102 -2
  53. data/ext/itsi_server/src/server/middleware_stack/middleware.rs +153 -0
  54. data/ext/itsi_server/src/server/middleware_stack/middlewares/allow_list.rs +47 -0
  55. data/ext/itsi_server/src/server/middleware_stack/middlewares/auth_api_key.rs +58 -0
  56. data/ext/itsi_server/src/server/middleware_stack/middlewares/auth_basic.rs +82 -0
  57. data/ext/itsi_server/src/server/middleware_stack/middlewares/auth_jwt.rs +321 -0
  58. data/ext/itsi_server/src/server/middleware_stack/middlewares/cache_control.rs +139 -0
  59. data/ext/itsi_server/src/server/middleware_stack/middlewares/compression.rs +300 -0
  60. data/ext/itsi_server/src/server/middleware_stack/middlewares/cors.rs +287 -0
  61. data/ext/itsi_server/src/server/middleware_stack/middlewares/deny_list.rs +48 -0
  62. data/ext/itsi_server/src/server/middleware_stack/middlewares/error_response.rs +127 -0
  63. data/ext/itsi_server/src/server/middleware_stack/middlewares/etag.rs +191 -0
  64. data/ext/itsi_server/src/server/middleware_stack/middlewares/grpc_service.rs +72 -0
  65. data/ext/itsi_server/src/server/middleware_stack/middlewares/header_interpretation.rs +85 -0
  66. data/ext/itsi_server/src/server/middleware_stack/middlewares/intrusion_protection.rs +195 -0
  67. data/ext/itsi_server/src/server/middleware_stack/middlewares/log_requests.rs +82 -0
  68. data/ext/itsi_server/src/server/middleware_stack/middlewares/mod.rs +82 -0
  69. data/ext/itsi_server/src/server/middleware_stack/middlewares/proxy.rs +216 -0
  70. data/ext/itsi_server/src/server/middleware_stack/middlewares/rate_limit.rs +124 -0
  71. data/ext/itsi_server/src/server/middleware_stack/middlewares/redirect.rs +76 -0
  72. data/ext/itsi_server/src/server/middleware_stack/middlewares/request_headers.rs +43 -0
  73. data/ext/itsi_server/src/server/middleware_stack/middlewares/response_headers.rs +34 -0
  74. data/ext/itsi_server/src/server/middleware_stack/middlewares/ruby_app.rs +93 -0
  75. data/ext/itsi_server/src/server/middleware_stack/middlewares/static_assets.rs +162 -0
  76. data/ext/itsi_server/src/server/middleware_stack/middlewares/string_rewrite.rs +158 -0
  77. data/ext/itsi_server/src/server/middleware_stack/middlewares/token_source.rs +12 -0
  78. data/ext/itsi_server/src/server/middleware_stack/mod.rs +315 -0
  79. data/ext/itsi_server/src/server/mod.rs +8 -1
  80. data/ext/itsi_server/src/server/process_worker.rs +38 -12
  81. data/ext/itsi_server/src/server/rate_limiter.rs +565 -0
  82. data/ext/itsi_server/src/server/request_job.rs +11 -0
  83. data/ext/itsi_server/src/server/serve_strategy/cluster_mode.rs +119 -42
  84. data/ext/itsi_server/src/server/serve_strategy/mod.rs +9 -6
  85. data/ext/itsi_server/src/server/serve_strategy/single_mode.rs +256 -111
  86. data/ext/itsi_server/src/server/signal.rs +19 -0
  87. data/ext/itsi_server/src/server/static_file_server.rs +984 -0
  88. data/ext/itsi_server/src/server/thread_worker.rs +139 -94
  89. data/ext/itsi_server/src/server/types.rs +43 -0
  90. data/ext/itsi_tracing/Cargo.toml +1 -0
  91. data/ext/itsi_tracing/src/lib.rs +216 -45
  92. data/ext/itsi_tracing/target/debug/incremental/itsi_tracing-0994n8rpvvt9m/s-h510hfz1f6-1kbycmq.lock +0 -0
  93. data/ext/itsi_tracing/target/debug/incremental/itsi_tracing-0bob7bf4yq34i/s-h5113125h5-0lh4rag.lock +0 -0
  94. data/ext/itsi_tracing/target/debug/incremental/itsi_tracing-2fcodulrxbbxo/s-h510h2infk-0hp5kjw.lock +0 -0
  95. data/ext/itsi_tracing/target/debug/incremental/itsi_tracing-2iak63r1woi1l/s-h510h2in4q-0kxfzw1.lock +0 -0
  96. data/ext/itsi_tracing/target/debug/incremental/itsi_tracing-2kk4qj9gn5dg2/s-h5113124kv-0enwon2.lock +0 -0
  97. data/ext/itsi_tracing/target/debug/incremental/itsi_tracing-2mwo0yas7dtw4/s-h510hfz1ha-1udgpei.lock +0 -0
  98. data/lib/itsi/scheduler/version.rb +1 -1
  99. data/lib/itsi/scheduler.rb +2 -2
  100. metadata +77 -12
  101. data/ext/itsi_server/extconf.rb +0 -6
  102. data/ext/itsi_server/src/body_proxy/mod.rs +0 -2
  103. data/ext/itsi_server/src/request/mod.rs +0 -1
  104. data/ext/itsi_server/src/response/mod.rs +0 -1
  105. data/ext/itsi_server/src/server/itsi_server.rs +0 -288
@@ -1,288 +0,0 @@
1
- use super::{
2
- bind::Bind,
3
- listener::Listener,
4
- serve_strategy::{cluster_mode::ClusterMode, single_mode::SingleMode},
5
- signal::{
6
- clear_signal_handlers, reset_signal_handlers, send_shutdown_event, SIGNAL_HANDLER_CHANNEL,
7
- },
8
- };
9
- use crate::{request::itsi_request::ItsiRequest, server::serve_strategy::ServeStrategy};
10
- use derive_more::Debug;
11
- use itsi_rb_helpers::{call_without_gvl, HeapVal, HeapValue};
12
- use itsi_tracing::{error, run_silently};
13
- use magnus::{
14
- block::Proc,
15
- error::Result,
16
- scan_args::{get_kwargs, scan_args, Args, KwArgs, ScanArgsKw, ScanArgsOpt, ScanArgsRequired},
17
- value::ReprValue,
18
- ArgList, RArray, RHash, Ruby, Symbol, Value,
19
- };
20
- use parking_lot::{Mutex, RwLock};
21
- use std::{cmp::max, collections::HashMap, ops::Deref, sync::Arc};
22
- use tracing::{info, instrument};
23
-
24
- static DEFAULT_BIND: &str = "http://localhost:3000";
25
-
26
- #[magnus::wrap(class = "Itsi::Server", free_immediately, size)]
27
- #[derive(Clone)]
28
- pub struct Server {
29
- pub config: Arc<ServerConfig>,
30
- }
31
-
32
- impl Deref for Server {
33
- type Target = ServerConfig;
34
-
35
- fn deref(&self) -> &Self::Target {
36
- &self.config
37
- }
38
- }
39
-
40
- #[derive(Debug)]
41
- pub struct ServerConfig {
42
- #[debug(skip)]
43
- pub app: HeapVal,
44
- #[allow(unused)]
45
- pub workers: u8,
46
- #[allow(unused)]
47
- pub threads: u8,
48
- #[allow(unused)]
49
- pub shutdown_timeout: f64,
50
- pub script_name: String,
51
- pub(crate) binds: Mutex<Vec<Bind>>,
52
- #[debug(skip)]
53
- pub hooks: HashMap<String, HeapValue<Proc>>,
54
- pub scheduler_class: Option<String>,
55
- pub stream_body: Option<bool>,
56
- pub worker_memory_limit: Option<u64>,
57
- #[debug(skip)]
58
- pub(crate) strategy: RwLock<Option<ServeStrategy>>,
59
- pub silence: bool,
60
- pub oob_gc_responses_threshold: Option<u64>,
61
- }
62
-
63
- #[derive(Debug)]
64
- pub enum RequestJob {
65
- ProcessRequest(ItsiRequest),
66
- Shutdown,
67
- }
68
-
69
- fn extract_args<Req, Opt, Splat>(
70
- scan_args: &Args<(), (), (), (), RHash, ()>,
71
- primaries: &[&str],
72
- rest: &[&str],
73
- ) -> Result<KwArgs<Req, Opt, Splat>>
74
- where
75
- Req: ScanArgsRequired,
76
- Opt: ScanArgsOpt,
77
- Splat: ScanArgsKw,
78
- {
79
- let symbols: Vec<Symbol> = primaries
80
- .iter()
81
- .chain(rest.iter())
82
- .map(|&name| Symbol::new(name))
83
- .collect();
84
-
85
- let hash = scan_args
86
- .keywords
87
- .funcall::<_, _, RHash>("slice", symbols.into_arg_list_with(&Ruby::get().unwrap()))
88
- .unwrap();
89
-
90
- get_kwargs(hash, primaries, rest)
91
- }
92
-
93
- impl Server {
94
- #[instrument(
95
- name = "Itsi",
96
- parent=None,
97
- skip(args),
98
- fields(workers = 1, threads = 1, shutdown_timeout = 5)
99
- )]
100
- pub fn new(args: &[Value]) -> Result<Self> {
101
- let scan_args: Args<(), (), (), (), RHash, ()> = scan_args(args)?;
102
-
103
- type Args1 = KwArgs<
104
- (Value,),
105
- (
106
- // Workers
107
- Option<u8>,
108
- // Threads
109
- Option<u8>,
110
- // Shutdown Timeout
111
- Option<f64>,
112
- // Script Name
113
- Option<String>,
114
- // Binds
115
- Option<Vec<String>>,
116
- // Stream Body
117
- Option<bool>,
118
- ),
119
- (),
120
- >;
121
-
122
- type Args2 = KwArgs<
123
- (),
124
- (
125
- // Hooks
126
- Option<RHash>,
127
- // Scheduler Class
128
- Option<String>,
129
- // Worker Memory Limit
130
- Option<u64>,
131
- // Out-of-band GC Responses Threshold
132
- Option<u64>,
133
- // Silence
134
- Option<bool>,
135
- ),
136
- (),
137
- >;
138
-
139
- let args1: Args1 = extract_args(
140
- &scan_args,
141
- &["app"],
142
- &[
143
- "workers",
144
- "threads",
145
- "shutdown_timeout",
146
- "script_name",
147
- "binds",
148
- "stream_body",
149
- ],
150
- )?;
151
-
152
- let args2: Args2 = extract_args(
153
- &scan_args,
154
- &[],
155
- &[
156
- "hooks",
157
- "scheduler_class",
158
- "worker_memory_limit",
159
- "oob_gc_responses_threshold",
160
- "silence",
161
- ],
162
- )?;
163
-
164
- let hooks = args2
165
- .optional
166
- .0
167
- .map(|rhash| -> Result<HashMap<String, HeapValue<Proc>>> {
168
- let mut hook_map: HashMap<String, HeapValue<Proc>> = HashMap::new();
169
- for pair in rhash.enumeratorize::<_, ()>("each", ()) {
170
- if let Some(pair_value) = RArray::from_value(pair?) {
171
- if let (Ok(key), Ok(value)) =
172
- (pair_value.entry::<Value>(0), pair_value.entry::<Proc>(1))
173
- {
174
- hook_map.insert(key.to_string(), HeapValue::from(value));
175
- }
176
- }
177
- }
178
- Ok(hook_map)
179
- })
180
- .transpose()?
181
- .unwrap_or_default();
182
-
183
- let config = ServerConfig {
184
- app: HeapVal::from(args1.required.0),
185
- workers: max(args1.optional.0.unwrap_or(1), 1),
186
- threads: max(args1.optional.1.unwrap_or(1), 1),
187
- shutdown_timeout: args1.optional.2.unwrap_or(5.0),
188
- script_name: args1.optional.3.unwrap_or("".to_string()),
189
- binds: Mutex::new(
190
- args1
191
- .optional
192
- .4
193
- .unwrap_or_else(|| vec![DEFAULT_BIND.to_string()])
194
- .into_iter()
195
- .map(|s| s.parse())
196
- .collect::<itsi_error::Result<Vec<Bind>>>()?,
197
- ),
198
- stream_body: args1.optional.5,
199
- hooks,
200
- scheduler_class: args2.optional.1.clone(),
201
- worker_memory_limit: args2.optional.2,
202
- strategy: RwLock::new(None),
203
- oob_gc_responses_threshold: args2.optional.3,
204
- silence: args2.optional.4.is_some_and(|s| s),
205
- };
206
-
207
- if !config.silence {
208
- if let Some(scheduler_class) = args2.optional.1 {
209
- info!(scheduler_class, fiber_scheduler = true);
210
- } else {
211
- info!(fiber_scheduler = false);
212
- }
213
- }
214
-
215
- Ok(Server {
216
- config: Arc::new(config),
217
- })
218
- }
219
-
220
- #[instrument(name = "Bind", skip_all, fields(binds=format!("{:?}", self.config.binds.lock())))]
221
- pub(crate) fn build_listeners(&self) -> Result<Vec<Listener>> {
222
- let listeners = self
223
- .config
224
- .binds
225
- .lock()
226
- .iter()
227
- .cloned()
228
- .map(Listener::try_from)
229
- .collect::<std::result::Result<Vec<Listener>, _>>()?
230
- .into_iter()
231
- .collect::<Vec<_>>();
232
- info!("Bound {:?} listeners", listeners.len());
233
- Ok(listeners)
234
- }
235
-
236
- pub(crate) fn build_strategy(self) -> Result<()> {
237
- let listeners = self.build_listeners()?;
238
- let server = Arc::new(self);
239
- let server_clone = server.clone();
240
-
241
- let strategy = if server.config.workers == 1 {
242
- ServeStrategy::Single(Arc::new(SingleMode::new(
243
- server,
244
- listeners,
245
- SIGNAL_HANDLER_CHANNEL.0.clone(),
246
- )?))
247
- } else {
248
- ServeStrategy::Cluster(Arc::new(ClusterMode::new(
249
- server,
250
- listeners,
251
- SIGNAL_HANDLER_CHANNEL.0.clone(),
252
- )))
253
- };
254
-
255
- *server_clone.strategy.write() = Some(strategy);
256
- Ok(())
257
- }
258
-
259
- pub fn stop(&self) -> Result<()> {
260
- send_shutdown_event();
261
- Ok(())
262
- }
263
-
264
- pub fn start(&self) -> Result<()> {
265
- if self.silence {
266
- run_silently(|| self.build_and_run_strategy())
267
- } else {
268
- self.build_and_run_strategy()
269
- }
270
- }
271
-
272
- fn build_and_run_strategy(&self) -> Result<()> {
273
- reset_signal_handlers();
274
- let rself = self.clone();
275
- call_without_gvl(move || -> Result<()> {
276
- rself.clone().build_strategy()?;
277
- if let Err(e) = rself.strategy.read().as_ref().unwrap().run() {
278
- error!("Error running server: {}", e);
279
- rself.strategy.read().as_ref().unwrap().stop()?;
280
- }
281
- Ok(())
282
- })?;
283
- clear_signal_handlers();
284
- self.strategy.write().take();
285
- info!("Server stopped");
286
- Ok(())
287
- }
288
- }