Skip to content

Commit 5ab7b87

Browse files
Support setting the number of tokio threads.
1 parent fe3cf7e commit 5ab7b87

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

ext/hyper_ruby/src/lib.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@ use http_body_util::Full;
3434
#[derive(Clone)]
3535
struct ServerConfig {
3636
bind_address: String,
37+
tokio_threads: Option<usize>,
3738
}
3839

3940
impl ServerConfig {
4041
fn new() -> Self {
4142
Self {
4243
bind_address: String::from("127.0.0.1:3000"),
44+
tokio_threads: None,
4345
}
4446
}
4547
}
@@ -78,6 +80,11 @@ impl Server {
7880
if let Some(bind_address) = config.get(magnus::Symbol::new("bind_address")) {
7981
server_config.bind_address = String::try_convert(bind_address)?;
8082
}
83+
84+
if let Some(tokio_threads) = config.get(magnus::Symbol::new("tokio_threads")) {
85+
server_config.tokio_threads = Some(usize::try_convert(tokio_threads)?);
86+
}
87+
8188
Ok(())
8289
}
8390

@@ -163,9 +170,16 @@ impl Server {
163170
.as_ref()
164171
.ok_or_else(|| MagnusError::new(magnus::exception::runtime_error(), "Work channel not initialized"))?
165172
.clone();
166-
167-
let rt = Arc::new(tokio::runtime::Builder::new_multi_thread()
168-
.enable_all()
173+
174+
let mut rt_builder = tokio::runtime::Builder::new_multi_thread();
175+
176+
rt_builder.enable_all();
177+
178+
if let Some(tokio_threads) = config.tokio_threads {
179+
rt_builder.worker_threads(tokio_threads);
180+
}
181+
182+
let rt = Arc::new(rt_builder
169183
.build()
170184
.map_err(|e| MagnusError::new(magnus::exception::runtime_error(), e.to_string()))?);
171185

@@ -311,6 +325,7 @@ fn init(ruby: &Ruby) -> Result<(), MagnusError> {
311325
request_class.define_method("http_method", method!(Request::method, 0))?;
312326
request_class.define_method("path", method!(Request::path, 0))?;
313327
request_class.define_method("header", method!(Request::header, 1))?;
328+
request_class.define_method("body", method!(Request::body, 0))?;
314329
request_class.define_method("fill_body", method!(Request::fill_body, 1))?;
315330
request_class.define_method("body_size", method!(Request::body_size, 0))?;
316331
request_class.define_method("inspect", method!(Request::inspect, 0))?;

ext/hyper_ruby/src/request.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ impl Request {
3636
self.request.body().len()
3737
}
3838

39+
pub fn body(&self) -> RString {
40+
let buffer = RString::buf_new(self.body_size());
41+
self.fill_body(buffer);
42+
buffer
43+
}
44+
3945
pub fn fill_body(&self, buffer: RString) -> usize {
4046
let body = self.request.body();
4147
let body_len = body.len();

test/test_hyper_ruby.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def test_blocking
7070

7171
def with_server(request_handler, &block)
7272
server = HyperRuby::Server.new
73-
server.configure({ bind_address: "127.0.0.1:3010" })
73+
server.configure({ bind_address: "127.0.0.1:3010",tokio_threads: 1 })
7474
server.start
7575

7676
# Create ruby worker threads that process requests;
@@ -94,7 +94,7 @@ def with_server(request_handler, &block)
9494

9595
def with_unix_socket_server(request_handler, &block)
9696
server = HyperRuby::Server.new
97-
server.configure({ bind_address: "unix:/tmp/hyper_ruby_test.sock" })
97+
server.configure({ bind_address: "unix:/tmp/hyper_ruby_test.sock", tokio_threads: 1 })
9898
server.start
9999

100100
# Create ruby worker threads that process requests;

0 commit comments

Comments
 (0)