Skip to content
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

Open
101mirsha opened this issue May 31, 2021 · 4 comments
Open

why four times thread are getting created for a single server? #205

101mirsha opened this issue May 31, 2021 · 4 comments

Comments

@101mirsha
Copy link

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

@dheijl
Copy link

dheijl commented May 31, 2021

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.

@brandonros
Copy link

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?

@dheijl
Copy link

dheijl commented Oct 13, 2023

Incoming http requests are still accepted and queued, but not handled until a server thread is available.

@brandonros
Copy link

brandonros commented Oct 14, 2023

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

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?

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 /ping takes over 5 seconds/fails.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants