itsi 0.2.10 → 0.2.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 (57) hide show
  1. checksums.yaml +4 -4
  2. data/Cargo.lock +2 -2
  3. data/LICENSE.txt +169 -0
  4. data/README.md +8 -0
  5. data/crates/itsi_error/Cargo.toml +1 -1
  6. data/crates/itsi_rb_helpers/Cargo.toml +1 -1
  7. data/crates/itsi_scheduler/Cargo.toml +1 -1
  8. data/crates/itsi_server/Cargo.toml +1 -1
  9. data/crates/itsi_server/src/default_responses/mod.rs +3 -0
  10. data/crates/itsi_server/src/lib.rs +2 -0
  11. data/crates/itsi_server/src/ruby_types/itsi_http_request.rs +15 -10
  12. data/crates/itsi_server/src/ruby_types/itsi_http_response.rs +10 -2
  13. data/crates/itsi_server/src/ruby_types/itsi_server/itsi_server_config.rs +6 -0
  14. data/crates/itsi_server/src/server/middleware_stack/middlewares/ruby_app.rs +13 -7
  15. data/crates/itsi_server/src/server/thread_worker.rs +4 -1
  16. data/docs/content/features/_index.md +2 -2
  17. data/docs/content/itsi_scheduler/_index.md +31 -1
  18. data/examples/hybrid_scheduler_mode/Itsi.rb +60 -0
  19. data/examples/hybrid_scheduler_mode/config.ru +9 -0
  20. data/examples/hybrid_scheduler_mode/slow_service/Itsi.rb +12 -0
  21. data/gems/scheduler/Cargo.lock +1 -1
  22. data/gems/scheduler/lib/itsi/schedule_refinement.rb +96 -0
  23. data/gems/scheduler/lib/itsi/scheduler/version.rb +1 -1
  24. data/gems/scheduler/lib/itsi/scheduler.rb +1 -0
  25. data/gems/server/Cargo.lock +1 -1
  26. data/gems/server/lib/itsi/http_request.rb +19 -20
  27. data/gems/server/lib/itsi/server/config/config_helpers.rb +5 -2
  28. data/gems/server/lib/itsi/server/config/middleware/endpoint/_index.md +2 -1
  29. data/gems/server/lib/itsi/server/config/middleware/endpoint/delete.rb +3 -2
  30. data/gems/server/lib/itsi/server/config/middleware/endpoint/endpoint.rb +14 -3
  31. data/gems/server/lib/itsi/server/config/middleware/endpoint/get.rb +3 -2
  32. data/gems/server/lib/itsi/server/config/middleware/endpoint/patch.rb +3 -2
  33. data/gems/server/lib/itsi/server/config/middleware/endpoint/post.rb +3 -2
  34. data/gems/server/lib/itsi/server/config/middleware/endpoint/put.rb +3 -2
  35. data/gems/server/lib/itsi/server/config/middleware/rackup_file.md +18 -0
  36. data/gems/server/lib/itsi/server/config/middleware/rackup_file.rb +2 -0
  37. data/gems/server/lib/itsi/server/config/middleware/run.md +20 -1
  38. data/gems/server/lib/itsi/server/config/middleware/run.rb +2 -0
  39. data/gems/server/lib/itsi/server/config/options/certificates.md +2 -2
  40. data/gems/server/lib/itsi/server/config/options/ruby_thread_request_backlog_size.md +18 -0
  41. data/gems/server/lib/itsi/server/config/options/ruby_thread_request_backlog_size.rb +19 -0
  42. data/gems/server/lib/itsi/server/config/options/scheduler_threads.md +8 -1
  43. data/gems/server/lib/itsi/server/config.rb +3 -0
  44. data/gems/server/lib/itsi/server/default_config/Itsi.rb +2 -2
  45. data/gems/server/lib/itsi/server/rack/handler/itsi.rb +3 -2
  46. data/gems/server/lib/itsi/server/rack_interface.rb +5 -6
  47. data/gems/server/lib/itsi/server/version.rb +1 -1
  48. data/gems/server/test/helpers/test_helper.rb +4 -1
  49. data/gems/server/test/options/ruby_thread_request_backlog_size.rb +37 -0
  50. data/gems/server/test/rack/test_rack_server.rb +127 -47
  51. data/lib/itsi/version.rb +1 -1
  52. data/tasks.txt +3 -2
  53. metadata +12 -9
  54. data/gems/scheduler/README.md +0 -76
  55. data/gems/scheduler/sig/itsi_scheduler.rbs +0 -4
  56. data/gems/server/README.md +0 -49
  57. data/gems/server/sig/itsi_server.rbs +0 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4c0be7500e24ba264247f8ea8a8d2e0e7ce1d69caf449cd194e3ac1c849cffb6
4
- data.tar.gz: 5dd34736783a2f8fe81f3ce3bd7626285ff8f364407248f0bbc5b74ba6c708dc
3
+ metadata.gz: 9069ae3d194e76dc4c4451d6506594a2a3e3789541b546a31dd3703a172f4e32
4
+ data.tar.gz: 6aacbd0351095bbde2e6db7c943bcd2444715519b079af2880c93cd5f623572f
5
5
  SHA512:
6
- metadata.gz: 619d63e46c4286bf336cca1fad51e51dadf9eac57e106a1fd0493b00a838be36bbea16e6a96a5003870752afbba5c11123d9a42e02c43ee2994175f4273811cc
7
- data.tar.gz: 264db7920c1c628ad0c65827dd63d5aeae3ce7c1ccba7a8940ebfc478000b9d728e804cd14c5700bd831fcd7f906bed6db85d4dc298119119eeb65249ec2bb96
6
+ metadata.gz: 36dcaf525c6852659d11c98f2d7813e4f01f86d934508a2ed45f071004f50fe6faee7a921ff9395bbc02198b10e8dc68887415781de3ae53aa3e90019a62f60e
7
+ data.tar.gz: ac990a66a605e5da8bec5f8bf26f51859ab50a1e4d8f50d95c9c4bf7c2ce6aefa0a882db1e7ef06593ab97ea4210e0faebe98f2a5d5001d9431fda8b1cf06c51
data/Cargo.lock CHANGED
@@ -1644,7 +1644,7 @@ checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c"
1644
1644
 
1645
1645
  [[package]]
1646
1646
  name = "itsi-scheduler"
1647
- version = "0.2.10"
1647
+ version = "0.2.12"
1648
1648
  dependencies = [
1649
1649
  "bytes",
1650
1650
  "derive_more",
@@ -1662,7 +1662,7 @@ dependencies = [
1662
1662
 
1663
1663
  [[package]]
1664
1664
  name = "itsi-server"
1665
- version = "0.2.10"
1665
+ version = "0.2.12"
1666
1666
  dependencies = [
1667
1667
  "argon2",
1668
1668
  "async-channel",
data/LICENSE.txt CHANGED
@@ -19,9 +19,178 @@ Please contact: commercial@itsi.fyi
19
19
  --------------------------------------------------------------------------------
20
20
  GNU LESSER GENERAL PUBLIC LICENSE
21
21
  Version 3, 29 June 2007
22
+ --------------------------------------------------------------------------------
23
+ GNU LESSER GENERAL PUBLIC LICENSE
24
+ Version 3, 29 June 2007
25
+
26
+ Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
27
+ Everyone is permitted to copy and distribute verbatim copies
28
+ of this license document, but changing it is not allowed.
29
+
30
+
31
+ This version of the GNU Lesser General Public License incorporates
32
+ the terms and conditions of version 3 of the GNU General Public
33
+ License, supplemented by the additional permissions listed below.
34
+
35
+ 0. Additional Definitions.
36
+
37
+ As used herein, "this License" refers to version 3 of the GNU Lesser
38
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
39
+ General Public License.
40
+
41
+ "The Library" refers to a covered work governed by this License,
42
+ other than an Application or a Combined Work as defined below.
43
+
44
+ An "Application" is any work that makes use of an interface provided
45
+ by the Library, but which is not otherwise based on the Library.
46
+ Defining a subclass of a class defined by the Library is deemed a mode
47
+ of using an interface provided by the Library.
48
+
49
+ A "Combined Work" is a work produced by combining or linking an
50
+ Application with the Library. The particular version of the Library
51
+ with which the Combined Work was made is also called the "Linked
52
+ Version".
53
+
54
+ The "Minimal Corresponding Source" for a Combined Work means the
55
+ Corresponding Source for the Combined Work, excluding any source code
56
+ for portions of the Combined Work that, considered in isolation, are
57
+ based on the Application, and not on the Linked Version.
58
+
59
+ The "Corresponding Application Code" for a Combined Work means the
60
+ object code and/or source code for the Application, including any data
61
+ and utility programs needed for reproducing the Combined Work from the
62
+ Application, but excluding the System Libraries of the Combined Work.
63
+
64
+ 1. Exception to Section 3 of the GNU GPL.
65
+
66
+ You may convey a covered work under sections 3 and 4 of this License
67
+ without being bound by section 3 of the GNU GPL.
68
+
69
+ 2. Conveying Modified Versions.
70
+
71
+ If you modify a copy of the Library, and, in your modifications, a
72
+ facility refers to a function or data to be supplied by an Application
73
+ that uses the facility (other than as an argument passed when the
74
+ facility is invoked), then you may convey a copy of the modified
75
+ version:
76
+
77
+ a) under this License, provided that you make a good faith effort to
78
+ ensure that, in the event an Application does not supply the
79
+ function or data, the facility still operates, and performs
80
+ whatever part of its purpose remains meaningful, or
81
+
82
+ b) under the GNU GPL, with none of the additional permissions of
83
+ this License applicable to that copy.
84
+
85
+ 3. Object Code Incorporating Material from Library Header Files.
86
+
87
+ The object code form of an Application may incorporate material from
88
+ a header file that is part of the Library. You may convey such object
89
+ code under terms of your choice, provided that, if the incorporated
90
+ material is not limited to numerical parameters, data structure
91
+ layouts and accessors, or small macros, inline functions and templates
92
+ (ten or fewer lines in length), you do both of the following:
93
+
94
+ a) Give prominent notice with each copy of the object code that the
95
+ Library is used in it and that the Library and its use are
96
+ covered by this License.
97
+
98
+ b) Accompany the object code with a copy of the GNU GPL and this license
99
+ document.
100
+
101
+ 4. Combined Works.
102
+
103
+ You may convey a Combined Work under terms of your choice that,
104
+ taken together, effectively do not restrict modification of the
105
+ portions of the Library contained in the Combined Work and reverse
106
+ engineering for debugging such modifications, if you also do each of
107
+ the following:
108
+
109
+ a) Give prominent notice with each copy of the Combined Work that
110
+ the Library is used in it and that the Library and its use are
111
+ covered by this License.
112
+
113
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
114
+ document.
115
+
116
+ c) For a Combined Work that displays copyright notices during
117
+ execution, include the copyright notice for the Library among
118
+ these notices, as well as a reference directing the user to the
119
+ copies of the GNU GPL and this license document.
120
+
121
+ d) Do one of the following:
122
+
123
+ 0) Convey the Minimal Corresponding Source under the terms of this
124
+ License, and the Corresponding Application Code in a form
125
+ suitable for, and under terms that permit, the user to
126
+ recombine or relink the Application with a modified version of
127
+ the Linked Version to produce a modified Combined Work, in the
128
+ manner specified by section 6 of the GNU GPL for conveying
129
+ Corresponding Source.
130
+
131
+ 1) Use a suitable shared library mechanism for linking with the
132
+ Library. A suitable mechanism is one that (a) uses at run time
133
+ a copy of the Library already present on the user's computer
134
+ system, and (b) will operate properly with a modified version
135
+ of the Library that is interface-compatible with the Linked
136
+ Version.
137
+
138
+ e) Provide Installation Information, but only if you would otherwise
139
+ be required to provide such information under section 6 of the
140
+ GNU GPL, and only to the extent that such information is
141
+ necessary to install and execute a modified version of the
142
+ Combined Work produced by recombining or relinking the
143
+ Application with a modified version of the Linked Version. (If
144
+ you use option 4d0, the Installation Information must accompany
145
+ the Minimal Corresponding Source and Corresponding Application
146
+ Code. If you use option 4d1, you must provide the Installation
147
+ Information in the manner specified by section 6 of the GNU GPL
148
+ for conveying Corresponding Source.)
149
+
150
+ 5. Combined Libraries.
151
+
152
+ You may place library facilities that are a work based on the
153
+ Library side by side in a single library together with other library
154
+ facilities that are not Applications and are not covered by this
155
+ License, and convey such a combined library under terms of your
156
+ choice, if you do both of the following:
157
+
158
+ a) Accompany the combined library with a copy of the same work based
159
+ on the Library, uncombined with any other library facilities,
160
+ conveyed under the terms of this License.
161
+
162
+ b) Give prominent notice with the combined library that part of it
163
+ is a work based on the Library, and explaining where to find the
164
+ accompanying uncombined form of the same work.
165
+
166
+ 6. Revised Versions of the GNU Lesser General Public License.
167
+
168
+ The Free Software Foundation may publish revised and/or new versions
169
+ of the GNU Lesser General Public License from time to time. Such new
170
+ versions will be similar in spirit to the present version, but may
171
+ differ in detail to address new problems or concerns.
172
+
173
+ Each version is given a distinguishing version number. If the
174
+ Library as you received it specifies that a certain numbered version
175
+ of the GNU Lesser General Public License "or any later version"
176
+ applies to it, you have the option of following the terms and
177
+ conditions either of that published version or of any later version
178
+ published by the Free Software Foundation. If the Library as you
179
+ received it does not specify a version number of the GNU Lesser
180
+ General Public License, you may choose any version of the GNU Lesser
181
+ General Public License ever published by the Free Software Foundation.
182
+
183
+ If the Library as you received it specifies that a proxy can decide
184
+ whether future versions of the GNU Lesser General Public License shall
185
+ apply, that proxy's public statement of acceptance of any version is
186
+ permanent authorization for you to choose that version for the
187
+ Library.
188
+
189
+
22
190
  --------------------------------------------------------------------------------
23
191
  GNU GENERAL PUBLIC LICENSE
24
192
  Version 3, 29 June 2007
193
+ --------------------------------------------------------------------------------
25
194
 
26
195
  Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
27
196
  Everyone is permitted to copy and distribute verbatim copies
data/README.md CHANGED
@@ -104,3 +104,11 @@ You can also run gem-specific variants of the above. E.g.
104
104
  * `rake server:test` or `rake scheduler:test`
105
105
  * `rake server:compile` or `rake scheduler:compile`
106
106
  * `rake server:build` or `rake scheduler:build`
107
+
108
+
109
+ ### Contributing
110
+ Developer Certificate of Origin
111
+
112
+ By submitting a pull-request you certify that:
113
+ 1. The contribution is your original work or you have the right to submit it;
114
+ 2. You have read and understood the [Developer Certificate of Origin](https://developercertificate.org/) and you accept it.
@@ -1,7 +1,7 @@
1
1
  [package]
2
2
  name = "itsi_error"
3
3
  version = "0.1.0"
4
- edition = "2024"
4
+ edition = "2021"
5
5
 
6
6
  [dependencies]
7
7
  thiserror = "2.0.11"
@@ -1,7 +1,7 @@
1
1
  [package]
2
2
  name = "itsi_rb_helpers"
3
3
  version = "0.1.0"
4
- edition = "2024"
4
+ edition = "2021"
5
5
 
6
6
  [dependencies]
7
7
  cfg-if = "1.0.0"
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "itsi-scheduler"
3
- version = "0.2.10"
3
+ version = "0.2.12"
4
4
  edition = "2021"
5
5
  authors = ["Wouter Coppieters <wc@pico.net.nz>"]
6
6
  license = "MIT"
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "itsi-server"
3
- version = "0.2.10"
3
+ version = "0.2.12"
4
4
  edition = "2021"
5
5
  authors = ["Wouter Coppieters <wc@pico.net.nz>"]
6
6
  license = "MIT"
@@ -9,3 +9,6 @@ pub static NOT_FOUND_RESPONSE: LazyLock<ErrorResponse> = LazyLock::new(ErrorResp
9
9
 
10
10
  pub static INTERNAL_SERVER_ERROR_RESPONSE: LazyLock<ErrorResponse> =
11
11
  LazyLock::new(ErrorResponse::internal_server_error);
12
+
13
+ pub static SERVICE_UNAVAILABLE_RESPONSE: LazyLock<ErrorResponse> =
14
+ LazyLock::new(ErrorResponse::service_unavailable);
@@ -86,6 +86,8 @@ fn init(ruby: &Ruby) -> Result<()> {
86
86
  response.define_method("<<", method!(ItsiHttpResponse::send_frame, 1))?;
87
87
  response.define_method("write", method!(ItsiHttpResponse::send_frame, 1))?;
88
88
  response.define_method("read", method!(ItsiHttpResponse::recv_frame, 0))?;
89
+ response.define_method("flush", method!(ItsiHttpResponse::flush, 0))?;
90
+ response.define_method("closed?", method!(ItsiHttpResponse::is_closed, 0))?;
89
91
  response.define_method(
90
92
  "send_and_close",
91
93
  method!(ItsiHttpResponse::send_and_close, 1),
@@ -22,6 +22,7 @@ use super::{
22
22
  itsi_http_response::ItsiHttpResponse,
23
23
  };
24
24
  use crate::{
25
+ default_responses::{INTERNAL_SERVER_ERROR_RESPONSE, SERVICE_UNAVAILABLE_RESPONSE},
25
26
  server::{
26
27
  byte_frame::ByteFrame,
27
28
  http_message_types::{HttpRequest, HttpResponse},
@@ -186,16 +187,20 @@ impl ItsiHttpRequest {
186
187
  } else {
187
188
  &context.sender
188
189
  };
189
- match sender
190
- .send(RequestJob::ProcessHttpRequest(request, app))
191
- .await
192
- {
193
- Err(err) => {
194
- error!("Error occurred: {}", err);
195
- let mut response = Response::new(BoxBody::new(Empty::new()));
196
- *response.status_mut() = StatusCode::BAD_REQUEST;
197
- Ok(response)
198
- }
190
+ match sender.try_send(RequestJob::ProcessHttpRequest(request, app)) {
191
+ Err(err) => match err {
192
+ async_channel::TrySendError::Full(_) => {
193
+ Ok(SERVICE_UNAVAILABLE_RESPONSE
194
+ .to_http_response(context.accept.clone())
195
+ .await)
196
+ }
197
+ async_channel::TrySendError::Closed(err) => {
198
+ error!("Error occurred: {:?}", err);
199
+ Ok(INTERNAL_SERVER_ERROR_RESPONSE
200
+ .to_http_response(context.accept.clone())
201
+ .await)
202
+ }
203
+ },
199
204
  _ => match receiver.recv().await {
200
205
  Some(first_frame) => Ok(response
201
206
  .build(first_frame, receiver, shutdown_channel)
@@ -272,6 +272,14 @@ impl ItsiHttpResponse {
272
272
  // not implemented
273
273
  }
274
274
 
275
+ pub fn flush(&self) {
276
+ // no-op
277
+ }
278
+
279
+ pub fn is_closed(&self) -> bool {
280
+ self.data.response_writer.write().is_none()
281
+ }
282
+
275
283
  pub fn send_and_close(&self, frame: Bytes) -> MagnusResult<()> {
276
284
  let result = self.send_frame_into(ByteFrame::End(frame), &self.data.response_writer);
277
285
  self.data.response_writer.write().take();
@@ -339,7 +347,7 @@ impl ItsiHttpResponse {
339
347
  })?;
340
348
  let header_value = unsafe { HeaderValue::from_maybe_shared_unchecked(value) };
341
349
  if let Some(ref mut resp) = *self.data.response.write() {
342
- resp.headers_mut().insert(header_name, header_value);
350
+ resp.headers_mut().append(header_name, header_value);
343
351
  }
344
352
  Ok(())
345
353
  }
@@ -356,7 +364,7 @@ impl ItsiHttpResponse {
356
364
  })?;
357
365
  for value in values {
358
366
  let header_value = unsafe { HeaderValue::from_maybe_shared_unchecked(value) };
359
- headers_mut.insert(&header_name, header_value);
367
+ headers_mut.append(&header_name, header_value);
360
368
  }
361
369
  }
362
370
  }
@@ -69,6 +69,7 @@ pub struct ServerParams {
69
69
  pub pin_worker_cores: bool,
70
70
  pub scheduler_class: Option<String>,
71
71
  pub oob_gc_responses_threshold: Option<u64>,
72
+ pub ruby_thread_request_backlog_size: Option<usize>,
72
73
  pub middleware_loader: HeapValue<Proc>,
73
74
  pub middleware: OnceLock<MiddlewareSet>,
74
75
  pub binds: Vec<Bind>,
@@ -222,6 +223,10 @@ impl ServerParams {
222
223
  let scheduler_class: Option<String> = rb_param_hash.fetch("scheduler_class")?;
223
224
  let oob_gc_responses_threshold: Option<u64> =
224
225
  rb_param_hash.fetch("oob_gc_responses_threshold")?;
226
+
227
+ let ruby_thread_request_backlog_size: Option<usize> =
228
+ rb_param_hash.fetch("ruby_thread_request_backlog_size")?;
229
+
225
230
  let middleware_loader: Proc = rb_param_hash.fetch("middleware_loader")?;
226
231
  let log_level: Option<String> = rb_param_hash.fetch("log_level")?;
227
232
  let log_target: Option<String> = rb_param_hash.fetch("log_target")?;
@@ -310,6 +315,7 @@ impl ServerParams {
310
315
  scheduler_threads,
311
316
  streamable_body,
312
317
  scheduler_class,
318
+ ruby_thread_request_backlog_size,
313
319
  oob_gc_responses_threshold,
314
320
  binds,
315
321
  itsi_server_token_preference,
@@ -18,6 +18,7 @@ use std::sync::Arc;
18
18
  pub struct RubyApp {
19
19
  app: Arc<HeapValue<Proc>>,
20
20
  request_type: RequestType,
21
+ script_name: Option<String>,
21
22
  sendfile: bool,
22
23
  nonblocking: bool,
23
24
  base_path: Regex,
@@ -53,6 +54,9 @@ impl RubyApp {
53
54
  let base_path_src = params
54
55
  .funcall::<_, _, String>(Symbol::new("[]"), ("base_path",))
55
56
  .unwrap_or("".to_owned());
57
+ let script_name = params
58
+ .funcall::<_, _, Option<String>>(Symbol::new("[]"), ("script_name",))
59
+ .unwrap_or(None);
56
60
  let base_path = Regex::new(&base_path_src).unwrap();
57
61
 
58
62
  let request_type: RequestType = params
@@ -65,6 +69,7 @@ impl RubyApp {
65
69
  app: Arc::new(app.into()),
66
70
  sendfile,
67
71
  nonblocking,
72
+ script_name,
68
73
  request_type,
69
74
  base_path,
70
75
  }))
@@ -82,13 +87,14 @@ impl MiddlewareLayer for RubyApp {
82
87
  match self.request_type {
83
88
  RequestType::Http => {
84
89
  let uri = req.uri().path();
85
- let script_name = self
86
- .base_path
87
- .captures(uri)
88
- .and_then(|caps| caps.name("base_path"))
89
- .map(|m| m.as_str())
90
- .unwrap_or("/")
91
- .to_owned();
90
+ let script_name = self.script_name.clone().unwrap_or_else(|| {
91
+ self.base_path
92
+ .captures(uri)
93
+ .and_then(|caps| caps.name("base_path"))
94
+ .map(|m| m.as_str())
95
+ .unwrap_or("/")
96
+ .to_owned()
97
+ });
92
98
  ItsiHttpRequest::process_request(
93
99
  self.app.clone(),
94
100
  req,
@@ -65,9 +65,12 @@ type ThreadWorkerBuildResult = Result<(
65
65
  pub fn build_thread_workers(params: Arc<ServerParams>, pid: Pid) -> ThreadWorkerBuildResult {
66
66
  let blocking_thread_count = params.threads;
67
67
  let nonblocking_thread_count = params.scheduler_threads;
68
+ let ruby_thread_request_backlog_size: usize = params
69
+ .ruby_thread_request_backlog_size
70
+ .unwrap_or_else(|| (blocking_thread_count as u16 * 30) as usize);
68
71
 
69
72
  let (blocking_sender, blocking_receiver) =
70
- async_channel::bounded((blocking_thread_count as u16 * 30) as usize);
73
+ async_channel::bounded(ruby_thread_request_backlog_size);
71
74
  let blocking_receiver_ref = Arc::new(blocking_receiver);
72
75
  let blocking_sender_ref = blocking_sender;
73
76
  let scheduler_class = load_scheduler_class(params.scheduler_class.clone())?;
@@ -273,9 +273,9 @@ Note that despite the header being named `X-Sendfile`, Itsi does not use the Sen
273
273
  {{% /details %}}
274
274
 
275
275
  {{% details title="Graceful Memory Limits" closed="true" %}}
276
- * Itsi allows you to specify memory limits for Ruby processes. When the limit is reached, Itsi gracefully terminates the process and also invokes a dedicated `after_memory_threshold_reached` callback,
276
+ * Itsi allows you to specify memory limits for Ruby processes. When the limit is reached, Itsi gracefully terminates the process and also invokes a dedicated `after_memory_limit_reached` callback,
277
277
  so that you can log the event for further analysis.
278
- * See <a target="_blank" href="/options/worker_memory_limit">worker_memory_limit</a> and <a target="_blank" href="/options/after_memory_threshold_reached">after_memory_threshold_reached</a>.
278
+ * See <a target="_blank" href="/options/worker_memory_limit">worker_memory_limit</a> and <a target="_blank" href="/options/after_memory_limit_reached">after_memory_limit_reached</a>.
279
279
  {{% /details %}}
280
280
 
281
281
  {{% details title="OOB GC" closed="true" %}}
@@ -101,6 +101,36 @@ end.value
101
101
 
102
102
  puts results.map(&:inspect)
103
103
  ```
104
-
105
104
  to run many blocking operations simultaneously all while occupying only a single Ruby thread!
105
+
106
+ ### 3 (Optional) - Enable Scheduler Refinements
107
+ You can opt-in to a tiny set of Ruby refinements provided by the `Itsi::Scheduler` to make usage even more ergonomic.
108
+ By opting in to this refinement (using `using Itsi::Scheduler`) you gain access to the top-level #schedule(&block) method, as well
109
+ as enumerable methods #schedule_each, and #schedule_map.
110
+
111
+ ```ruby
112
+
113
+ using Itsi::Scheduler
114
+
115
+ # Fire-and-forget: 100 HTTP calls in parallel
116
+ 100.times.schedule_each do |i|
117
+ Net::HTTP.get(URI("https://example.com/#{i}"))
118
+ end
119
+
120
+ # Concurrent transform that keeps the original order
121
+ squares = (1..20).schedule_map { |n| n * n }
122
+ puts squares.inspect
123
+ # => [1, 4, 9, 16, … 400]
124
+
125
+ # Manual orchestration — still one thread
126
+ schedule do
127
+ a, b = Queue.new, Queue.new
128
+
129
+ schedule { a << Net::HTTP.get(URI("https://httpbin.org/get")) }
130
+ schedule { b << Net::HTTP.get(URI("https://httpbin.org/uuid")) }
131
+
132
+ puts a.pop
133
+ puts b.pop
134
+ end
135
+ ```
106
136
  {{% /steps %}}
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Before running this test, go to the ./slow_service directory, and start the slow running service
4
+ # on port 3005, by running `itsi`.
5
+
6
+ # Single thread for blocking test.
7
+ threads 1
8
+
9
+ # Single thread for non-blocking test.
10
+ scheduler_threads 1
11
+
12
+ shutdown_timeout 1.0 # Shutdown Timeout in Seconds
13
+
14
+ # Small backlogs for test purposes
15
+ ruby_thread_request_backlog_size 100
16
+
17
+ # Enable Itsi's fiber scheduler.
18
+ fiber_scheduler true
19
+
20
+ # Send requests to http://localhost:3000/nonblocking.
21
+ # The app at config.ru runs a slow endpoint (takes 3+ seconds).
22
+ # When using this endpoint you can have a very large number of simultaneous inflight requests.
23
+ #
24
+ #
25
+ # Example benchmark:
26
+ # Non-blocking mode. 100 requests at a time, service takes 2s to respond. Throughput of ~50rps expected.
27
+ # ❯ wrk http://0.0.0.0:3000/nonblocking -c 100 -d 10
28
+ # Running 10s test @ http://0.0.0.0:3000/nonblocking
29
+ # 2 threads and 100 connections
30
+ # Thread Stats Avg Stdev Max +/- Stdev
31
+ # Latency 0.00us 0.00us 0.00us nan%
32
+ # Req/Sec 65.00 109.41 316.00 84.62%
33
+ # 500 requests in 10.10s, 69.34KB read
34
+ # Socket errors: connect 0, read 0, write 0, timeout 500
35
+ # Requests/sec: 49.50
36
+ # Transfer/sec: 6.86KB
37
+
38
+ location 'nonblocking' do
39
+ rackup_file 'config.ru', nonblocking: true, script_name: ''
40
+ end
41
+
42
+
43
+ # Send requests to http://localhost:3000/blocking.
44
+ # The app at config.ru runs a slow endpoint (takes 3+ seconds).
45
+ # When using this endpoint, concurrency is limited by the number of blocking threads available.
46
+ #
47
+ # Blocking mode. Requests are executed sequentially , service takes 2s to respond. Throughput of ~0.5rps expected.
48
+ # ❯ wrk http://0.0.0.0:3000/nonblocking -c 100 -d 10
49
+ # Running 10s test @ http://0.0.0.0:3000/blocking
50
+ # 2 threads and 100 connections
51
+ # Thread Stats Avg Stdev Max +/- Stdev
52
+ # Latency 0.00us 0.00us 0.00us nan%
53
+ # Req/Sec 0.00 0.00 0.00 100.00%
54
+ # 5 requests in 10.10s, 710.00B read
55
+ # Socket errors: connect 0, read 0, write 0, timeout 5
56
+ # Requests/sec: 0.49
57
+
58
+ location 'blocking' do
59
+ rackup_file 'config.ru', script_name: ''
60
+ end
@@ -0,0 +1,9 @@
1
+ require 'net/http'
2
+
3
+ # This could be any Rack app, a Rails app, a Sinatra app, etc.
4
+ # We're going to use a simple inline rack handler for now.
5
+
6
+ run ->(_) do
7
+ result = Net::HTTP.new('127.0.0.1', 3005).get('/?sleep=2')
8
+ [result.code.to_i, {'content-type' => 'text/html'}, [result.body]]
9
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ threads 1
4
+ fiber_scheduler true
5
+
6
+ bind "http://0.0.0.0:3005"
7
+
8
+ endpoint do |req|
9
+ sleep_length = req.query_params.fetch("sleep", 3).to_i
10
+ sleep sleep_length
11
+ req.ok "It's been #{sleep_length} seconds!"
12
+ end
@@ -213,7 +213,7 @@ checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c"
213
213
 
214
214
  [[package]]
215
215
  name = "itsi-scheduler"
216
- version = "0.2.10"
216
+ version = "0.2.12"
217
217
  dependencies = [
218
218
  "bytes",
219
219
  "derive_more",