New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
why four times thread are getting created for a single server? #205
Comments
Each thread can handle a single request at a time. Until it has finished responding to the request, it can handle no other requests. So in the example code you can handle 4 requests simultaneously, unless of course each serving thread spawns a new thread itself to handle the request, in which case you can serve many more requests simultaneously at the expense of having an uncontrollable number of threads active at the same time. |
To clarify, is that to say under the hood the default behavior if you just have 1 instance of server in a loop handling incoming_requests() is to process 1 HTTP request at a time and block all other requests until that is finished? |
Incoming http requests are still accepted and queued, but not handled until a server thread is available. |
Interesting. I read the various conversations around #221 I'm sure it's something I probably did (and I'm sure this probably isn't the right place to discuss this) but I figured that
was the case because I wrote a small HTTP server that is handling a handful of requests at a time and it typically needs to be restarted because Then I stumbled across this and applied it: let server = Arc::new(Server::http("0.0.0.0:9000").expect("failed to start http server"));
let num_threads = 16;
let pool = threadpool::ThreadPool::new(num_threads);
for _ in 0..num_threads {
let server = server.clone();
let router = Router::new(route_handlers_map.clone());
pool.execute(move || {
for request in server.incoming_requests() {
match router.handle_request(request) {
Ok(_) => (),
Err(err) => log::error!("{err:?}")
}
}
});
} The issue went away. But I guess it makes sense. Giving "16x" the amount of processing power probably just hid whatever the original issue was. |
Could you please a bit elaborate?
for _ in 0..4 {
let server = server.clone();
handles.push(thread::spawn(move || {
...
https://github.com/tiny-http/tiny-http/blob/master/examples/hello-world.rs#L7
The text was updated successfully, but these errors were encountered: