Discussion:
unknown
1970-01-01 00:00:00 UTC
Permalink
<div><br></div><div>2. I had tried this before, but ran into threading issu=
e, it made my app crash</div><div><br></div><div>3. So you suggest to use a=
global mutex to make sure when I am closing one socket, no async_read or a=
snyc_write operation happens on it in another thread.</div>



<div><br></div><div>4. I need to apply this mutex to all my read/write oper=
ations like this:</div><div>Sign is one connected object in my server app:<=
/div><div><br></div><div><div>void Sign::DoWrite(boost::shared_ptr&lt;Messa=
ge&gt; msg) {</div>



<div>&nbsp; write_msgs_.push_back(msg);</div><div>&nbsp; if (write_msgs_.si=
ze() =3D=3D 1) {</div><div>&nbsp; &nbsp; boost::shared_ptr&lt;vector&lt;cha=
r&gt; &gt; data(new vector&lt;char&gt;(400, 0));</div><div>&nbsp; &nbsp; ms=
g-&gt;Write(*data);</div><div>



&nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; clientsPoolMutex.lock();</div><=
div>&nbsp; &nbsp; async_write(socket,</div><div>&nbsp; &nbsp; &nbsp; &nbsp;=
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strand_.wrap(=
bind(&amp;Sign::AfterSend, shared_from_this(), _1, data)));</div>



<div>&nbsp; }</div><div>}</div></div><div><br></div><div><br></div></div><d=
iv class=3D"gmail_extra"><div><br clear=3D"all"><div><div dir=3D"ltr">Dean =
Chen&nbsp;<br>Best regards<br><a href=3D"http://blog.csdn.net/csfreebird" t=
arget=3D"_blank">http://blog.csdn.net/csfreebird</a></div>



</div>
<br><br></div><div><div><div class=3D"gmail_quote">On Wed, Apr 2, 2014 at 1=
2:02 AM, Slav <span dir=3D"ltr">&lt;<a href=3D"mailto:***@gmail.com" ta=
rget=3D"_blank">***@gmail.com</a>&gt;</span> wrote:<br><blockquote clas=
s=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;pad=
ding-left:1ex">



<div dir=3D"ltr"><div><div><div><div><div><div><div>To safely work with con=
nections asio::strand can be used:<br><br></div><div>&nbsp;&nbsp;&nbsp; //c=
reated once when io_service is created:<br></div>&nbsp;&nbsp;&nbsp; asio::s=
trand strand( io_service );<br>




&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; ...<br>&nbsp;&nbsp;&nbsp; <br></d=
iv>&nbsp;&nbsp;&nbsp; //at any moment connection must be &quot;touched&quot=
;:<br></div><div>&nbsp;&nbsp;&nbsp; //no parallel async_read/write or handl=
e_accept() can be called since we&#39;re within asio::strand, but your serv=
er can start using clients so lock them in that case:<br>




</div>&nbsp;&nbsp;&nbsp; clientsPoolMutex.lock();<br><br></div>&nbsp;&nbsp;=
&nbsp; for each client in clients<br>&nbsp;&nbsp;&nbsp; {<br></div><div>&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //multiple harmless errors can aris=
e (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 serv=
er if not catch them:<br>




</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; asio::error_code shut=
downErrorCode;<br></div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; asi=
o::error_code closeErrorCode;<br></div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp; <br></div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //sho=
uld be kicked due to being too old:<br></div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp; if ( client-&gt;lastMessageReceived &lt; currentTime - ( 60 * =
60 ) )<br>




&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br></div>&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; client-&gt;socket.shutdown( a=
sio::socket_base::shutdown_both, shutdownErrorCode );<br></div>&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; client-&gt;socket.cl=
ose( closeErrorCode );<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&=
nbsp;&nbsp;&nbsp; }<br><div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>




</div></div></div><div class=3D"gmail_extra"><br><br><div class=3D"gmail_qu=
ote">2014-04-01 17:51 GMT+04:00 Dean Chen <span dir=3D"ltr">&lt;<a href=3D"=
mailto:***@gmail.com" target=3D"_blank">***@gmail.com</a>&gt;=
</span>:<div>



<div><br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex">Hi, Slav:<br>
&nbsp; Thanks for your response. I used to create one thread to close the<b=
r>
&nbsp; connection if it&#39;s timeout. But this cause my process to crash. =
Because I<br>
&nbsp; cannot close one socket in another thread when there is another<br>
&nbsp; async_write or async_read operation on it.<br>
Accordin to this post:<br>
&nbsp; <a href=3D"http://web.archiveorange.com/archive/v/Q0J4VefPMc2v8QYvcV=
r3" target=3D"_blank">http://web.archiveorange.com/archive/v/Q0J4VefPMc2v8Q=
YvcVr3</a><br>
<br>
Most likely you have a threading issue in your program where you close a<br=
&nbsp;socket from one thread while simultaneously starting another async<br=
&nbsp;operation on the same socket from another thread. If you are sure thi=
s is<br>
&nbsp;not the case, please attach a small, complete program that exhibits t=
he<br>
&nbsp;problem. Thanks.<br>
<br>
&nbsp; Could you teach me your way, how to close the socket safely?<br>
<div><div><br>
Slav &lt;<a href=3D"mailto:***@gmail.com" target=3D"_blank">***@gma=
il.com</a>&gt; writes:<br>
<br>
&gt; I had the same problem. Each day ~15000 users was connecting the<br>
&gt; server but some of connections wasn&#39;t closed, so when ~500000<br>
&gt; connections was hanging (some of them was live), server must be<br>
&gt; restarted - it happened like each month-two.<br>
&gt; Timers will significantly slow your server down, indeed. As was<br>
&gt; answered to me in this mailing lists, it should be solved with<br>
&gt; periodic iteration through all connections to kill any which hang too<=
br>
&gt; much depending your inner server logic.<br>
&gt; For my server I kick everyone who didn&#39;t sent anything to server f=
or<br>
&gt; an hour. Kicking process is being invoked each 5 minutes. Iteration<br=
&gt; through 10000 connections is lightning fast: takes less time than<br>
&gt; human can measure (so less than 80 milliseconds) so it&#39;s possible =
to<br>
&gt; run it each second but not needed.<br>
&gt;<br>
&gt; 2014-04-01 16:46 GMT+04:00 =B3=C2=CA=E3 &lt;<a href=3D"mailto:csfreebi=
***@gmail.com" target=3D"_blank">***@gmail.com</a>&gt;:<br>
&gt;<br>
&gt;<br>
&gt; &nbsp; &nbsp; Hi,<br>
&gt; &nbsp; &nbsp; My server needs to handle thousands of TCP connections. =
But I find<br>
&gt; &nbsp; &nbsp; many connections are not closed because async_read metho=
d blocks,<br>
&gt; &nbsp; &nbsp; it&#39;s weird!<br>
&gt; &nbsp; &nbsp; Today my server cannot work because there are too many T=
CP<br>
&gt; &nbsp; &nbsp; connections, I have to restart OS.<br>
&gt; &nbsp; &nbsp; I read a few articles which describes how to use dead li=
ne timer<br>
&gt; &nbsp; &nbsp; with async_read, but I have some sad experience, thousan=
ds of<br>
&gt; &nbsp; &nbsp; timers will slow down my server.<br>
&gt; &nbsp; &nbsp; See my question on stackoverflow:<br>
&gt; &nbsp; &nbsp; <a href=3D"http://stackoverflow.com/questions/22777835/i=
s-boostasio-asyn-read-" target=3D"_blank">http://stackoverflow.com/question=
s/22777835/is-boostasio-asyn-read-</a><br>
&gt; &nbsp; &nbsp; with-timer-a-good-idea<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; &nbsp; &nbsp; My question is:<br>
&gt; &nbsp; &nbsp; Why does my async_read blocks?<br>
&gt; &nbsp; &nbsp; Can I use dead line timer in this case?<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; &nbsp; &nbsp; Dean Chen<br>
&gt; &nbsp; &nbsp; Best regards<br>
&gt; &nbsp; &nbsp; <a href=3D"http://blog.csdn.net/csfreebird" target=3D"_b=
lank">http://blog.csdn.net/csfreebird</a><br>
&gt;<br>
&gt; &nbsp; &nbsp; --------------------------------------------------------=
-----------<br>
&gt; &nbsp; &nbsp; -----------<br>
&gt;<br>
&gt; &nbsp; &nbsp; _______________________________________________<br>
&gt; &nbsp; &nbsp; asio-users mailing list<br>
&gt; &nbsp; &nbsp; <a href=3D"mailto:asio-***@lists.sourceforge.net" targ=
et=3D"_blank">asio-***@lists.sourceforge.net</a><br>
&gt; &nbsp; &nbsp; <a href=3D"https://lists.sourceforge.net/lists/listinfo/=
asio-users" target=3D"_blank">https://lists.sourceforge.net/lists/listinfo/=
asio-users</a><br>
&gt; &nbsp; &nbsp; _______________________________________________<br>
&gt; &nbsp; &nbsp; Using Asio? List your project at<br>
&gt; &nbsp; &nbsp; <a href=3D"http://think-async.com/Asio/WhoIsUsingAsio" t=
arget=3D"_blank">http://think-async.com/Asio/WhoIsUsingAsio</a><br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; ----------------------------------------------------------------------=
--------<br>
&gt; _______________________________________________<br>
&gt; asio-users mailing list<br>
&gt; <a href=3D"mailto:asio-***@lists.sourceforge.net" target=3D"_blank">=
asio-***@lists.sourceforge.net</a><br>
&gt; <a href=3D"https://lists.sourceforge.net/lists/listinfo/asio-users" ta=
rget=3D"_blank">https://lists.sourceforge.net/lists/listinfo/asio-users</a>=
<br>
&gt; _______________________________________________<br>
&gt; Using Asio? List your project at<br>
&gt; <a href=3D"http://think-async.com/Asio/WhoIsUsingAsio" target=3D"_blan=
k">http://think-async.com/Asio/WhoIsUsingAsio</a><br>
<br>
---------------------------------------------------------------------------=
---<br>
_______________________________________________<br>
asio-users mailing list<br>
<a href=3D"mailto:asio-***@lists.sourceforge.net" target=3D"_blank">asio-=
***@lists.sourceforge.net</a><br>
<a href=3D"https://lists.sourceforge.net/lists/listinfo/asio-users" target=
=3D"_blank">https://lists.sourceforge.net/lists/listinfo/asio-users</a><br>
_______________________________________________<br>
Using Asio? List your project at<br>
<a href=3D"http://think-async.com/Asio/WhoIsUsingAsio" target=3D"_blank">ht=
tp://think-async.com/Asio/WhoIsUsingAsio</a><br>
</div></div></blockquote></div></div></div><br></div>
<br>-----------------------------------------------------------------------=
-------<br>
<br>_______________________________________________<br>
asio-users mailing list<br>
<a href=3D"mailto:asio-***@lists.sourceforge.net" target=3D"_blank">asio-=
***@lists.sourceforge.net</a><br>
<a href=3D"https://lists.sourceforge.net/lists/listinfo/asio-users" target=
=3D"_blank">https://lists.sourceforge.net/lists/listinfo/asio-users</a><br>
_______________________________________________<br>
Using Asio? List your project at<br>
<a href=3D"http://think-async.com/Asio/WhoIsUsingAsio" target=3D"_blank">ht=
tp://think-async.com/Asio/WhoIsUsingAsio</a><br>
<br></blockquote></div><br></div></div></div>
</blockquote></div><br></div></div></div></div><br>------------------------=
------------------------------------------------------<br>
<br>_______________________________________________<br>
asio-users mailing list<br>
<a href=3D"mailto:asio-***@lists.sourceforge.net">asio-***@lists.source=
forge.net</a><br>
<a href=3D"https://lists.sourceforge.net/lists/listinfo/asio-users" target=
=3D"_blank">https://lists.sourceforge.net/lists/listinfo/asio-users</a><br>
_______________________________________________<br>
Using Asio? List your project at<br>
<a href=3D"http://think-async.com/Asio/WhoIsUsingAsio" target=3D"_blank">ht=
tp://think-async.com/Asio/WhoIsUsingAsio</a><br>
<br></blockquote></div><br></div>

--089e0158ab5c1aa96d04f60c99c1--

Loading...