No global mutex, strand should enough.
Just push all function call of same socket to the same strand .
But if you access those connections/sessions from multiple threads, you should use Slav's global mutex
------------------ ÔÊŒÓÊŒþ ------------------
·¢ŒþÈË: "³ÂÊã";<***@gmail.com>;
·¢ËÍʱŒä: 2014Äê4ÔÂ2ÈÕ(ÐÇÆÚÈý) ÉÏÎç9:51
ÊÕŒþÈË: "asio-***@lists.sourceforge.net"<asio-***@lists.sourceforge.net>;
Ö÷Ìâ: Re: [asio-users] Can I use dead_line_timer with async_read togetherwhen keeping thousands of TCP connections?
But If all read/write operations need to get the global mutex, it might be another performance issue.
Dean Chen
Best regards
http://blog.csdn.net/csfreebird
On Wed, Apr 2, 2014 at 9:40 AM, ³ÂÊã <***@gmail.com> wrote:
Hi, Slav: From you code, I guess I should do the following jobs:
1. in one thread, use for(;;) loop to iterate all connected client objects, check them to see if they are timeout, if it is, close the socket
2. I had tried this before, but ran into threading issue, it made my app crash
3. So you suggest to use a global mutex to make sure when I am closing one socket, no async_read or asnyc_write operation happens on it in another thread.
4. I need to apply this mutex to all my read/write operations like this:
Sign is one connected object in my server app:
void Sign::DoWrite(boost::shared_ptr<Message> msg) {
write_msgs_.push_back(msg);
if (write_msgs_.size() == 1) {
boost::shared_ptr<vector<char> > data(new vector<char>(400, 0));
msg->Write(*data);
clientsPoolMutex.lock();
async_write(socket,
buffer(&(*data)[0], data->size()),
strand_.wrap(bind(&Sign::AfterSend, shared_from_this(), _1, data)));
}
}
Dean Chen
Best regards
http://blog.csdn.net/csfreebird
On Wed, Apr 2, 2014 at 12:02 AM, Slav <***@gmail.com> wrote:
To safely work with connections asio::strand can be used:
//created once when io_service is created:
asio::strand strand( io_service );
...
//at any moment connection must be "touched":
//no parallel async_read/write or handle_accept() can be called since we're within asio::strand, but your server can start using clients so lock them in that case:
clientsPoolMutex.lock();
for each client in clients
{
//multiple harmless errors can arise (connection was already closed, connection was forced to be close and so on) - just ignoring them is fine, but usage of overloaded function which do not accept error codes field can throw exceptions which can kill your server if not catch them:
asio::error_code shutdownErrorCode;
asio::error_code closeErrorCode;
//should be kicked due to being too old:
if ( client->lastMessageReceived < currentTime - ( 60 * 60 ) )
{
client->socket.shutdown( asio::socket_base::shutdown_both, shutdownErrorCode );
client->socket.close( closeErrorCode );
}
}
2014-04-01 17:51 GMT+04:00 Dean Chen <***@gmail.com>:
Hi, Slav:
Thanks for your response. I used to create one thread to close the
connection if it's timeout. But this cause my process to crash. Because I
cannot close one socket in another thread when there is another
async_write or async_read operation on it.
Accordin to this post:
http://web.archiveorange.com/archive/v/Q0J4VefPMc2v8QYvcVr3
Most likely you have a threading issue in your program where you close a
socket from one thread while simultaneously starting another async
operation on the same socket from another thread. If you are sure this is
not the case, please attach a small, complete program that exhibits the
problem. Thanks.
Could you teach me your way, how to close the socket safely?
Post by SlavI had the same problem. Each day ~15000 users was connecting the
server but some of connections wasn't closed, so when ~500000
connections was hanging (some of them was live), server must be
restarted - it happened like each month-two.
Timers will significantly slow your server down, indeed. As was
answered to me in this mailing lists, it should be solved with
periodic iteration through all connections to kill any which hang too
much depending your inner server logic.
For my server I kick everyone who didn't sent anything to server for
an hour. Kicking process is being invoked each 5 minutes. Iteration
through 10000 connections is lightning fast: takes less time than
human can measure (so less than 80 milliseconds) so it's possible to
run it each second but not needed.
Hi,
My server needs to handle thousands of TCP connections. But I find
many connections are not closed because async_read method blocks,
it's weird!
Today my server cannot work because there are too many TCP
connections, I have to restart OS.
I read a few articles which describes how to use dead line timer
with async_read, but I have some sad experience, thousands of
timers will slow down my server.
http://stackoverflow.com/questions/22777835/is-boostasio-asyn-read-
with-timer-a-good-idea
Why does my async_read blocks?
Can I use dead line timer in this case?
Dean Chen
Best regards
http://blog.csdn.net/csfreebird
-------------------------------------------------------------------
-----------
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
------------------------------------------------------------------------------
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
------------------------------------------------------------------------------
_______________________________________________
asio-users mailing list
asio-***@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
------------------------------------------------------------------------------
_______________________________________________
asio-users mailing list
asio-***@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio