Discussion:
[Boost-users] [ASIO] Do coroutines allow serving multiple clients at once?
m***@libero.it
2016-10-10 07:44:02 UTC
Permalink
I'm using ASIO with coroutines2 to create a ssl echo server. The code I've been experimenting is the following: http://nopaste.linux-dev.org/?1120051
While this is highly readable compared to the asynchronous callbacks version (thanks coroutines), I'd like to know if this code is able to serve multiple clients connecting at the same time to the server.
In theory, every time I return control to the io_service object (i.e. I yield in the spawn coroutine) it should take care of whatever connection is pending / buffered / scheduled to be accepted next, right? Is this a reliable way to handle multiple requests? I don't plan on supporting massive workloads (e.g. 500k connections at once) but having some resilience would be nice indeed.
Any other thoughts/tips are also warmly welcome.
Oliver Kowalke
2016-10-10 13:19:46 UTC
Permalink
Post by m***@libero.it
I'd like to know if this code is able to serve multiple clients connecting
at the same time to the server.
I would say no. You spawn only one coroutine that calls in the for-loop
async. operations of accept(), read() and write() in a sequence.
No other client is able to connect to your server, e.g. connect() on the
client side returns, until you enter accept() again.

In order to serve multiple clients at the same time you need to spawn a
coroutine that accepts new connections and for each client request
(read()/write() operations) you need to spawn a new coroutine.
m***@libero.it
2016-10-10 15:35:25 UTC
Permalink
Thanks, does spawning a coroutine also spawn a new thread? I'd be inclined to think so from what you told me
Nat Goodspeed
2016-10-10 21:08:19 UTC
Permalink
Post by m***@libero.it
Thanks, does spawning a coroutine also spawn a new thread? I'd be
inclined to think so from what you told me

No. Multiple coroutines execute within the same thread. It's the same level
of concurrency that you get with callbacks, but much more maintainably
organized.

m***@libero.it
2016-10-10 16:03:22 UTC
Permalink
Thanks, does spawning a coroutine also spawn a new thread? I'd be inclined to think so from what you told me
m***@libero.it
2016-10-10 16:08:36 UTC
Permalink
Thanks, does spawning a coroutine also spawn a new thread? I'd be inclined to think so from what you told me




----Messaggio originale----

Da: "Oliver Kowalke" <***@gmail.com>

Data: 10-ott-2016 15.19

A: "boost-users"<boost-***@lists.boost.org>

Ogg: Re: [Boost-users] [ASIO] Do coroutines allow serving multiple clients at once?



2016-10-10 9:44 GMT+02:00 ***@libero.it <***@libero.it>:
I'd like to know if this code is able to serve multiple clients connecting at the same time to the server.

I would say no. You spawn only one coroutine that calls in the for-loop async. operations of accept(), read() and write() in a sequence.
No other client is able to connect to your server, e.g. connect() on the client side returns, until you enter accept() again.

In order to serve multiple clients at the same time you need to spawn a coroutine that accepts new connections and for each client request (read()/write() operations) you need to spawn a new coroutine.
Loading...