Marc-André Cournoyer
2008-Jan-26 20:38 UTC
[Eventmachine-talk] stop_event_loop doesn''t let connection send data
I got a server running: EventMachine.run do EventMachine.start_server(@host, @port, Connection) end When I call: EventMachine.stop_event_loop it looks like it''s letting the connections finish their work before returning and stopping the server, but doesn''t seem like the data is sent back the the client. Is that the correct way to stop the server without dropping any connection? Thx, Marc
Francis Cianfrocca
2008-Jan-27 10:49 UTC
[Eventmachine-talk] stop_event_loop doesn''t let connection send
On Jan 26, 2008 11:38 PM, Marc-Andr? Cournoyer <macournoyer at gmail.com> wrote:> I got a server running: > > EventMachine.run do > EventMachine.start_server(@host, @port, Connection) > end > > When I call: > EventMachine.stop_event_loop > > it looks like it''s letting the connections finish their work before > returning and stopping the server, but doesn''t seem like the data is > sent back the the client. > > Is that the correct way to stop the server without dropping any > connection? >No, just call EM::stop. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/eventmachine-talk/attachments/20080127/014aa736/attachment-0001.html
Marc-André Cournoyer
2008-Jan-27 17:49 UTC
[Eventmachine-talk] stop_event_loop doesn''t let connection send
same behaviour w/ EM::stop, the connections keep processing but the data is not sent back to the client. On 27-Jan-08, at 1:49 PM, Francis Cianfrocca wrote:> On Jan 26, 2008 11:38 PM, Marc-Andr? Cournoyer > <macournoyer at gmail.com> wrote: > I got a server running: > > EventMachine.run do > EventMachine.start_server(@host, @port, Connection) > end > > When I call: > EventMachine.stop_event_loop > > it looks like it''s letting the connections finish their work before > returning and stopping the server, but doesn''t seem like the data is > sent back the the client. > > Is that the correct way to stop the server without dropping any > connection? > > > No, just call EM::stop. > > > _______________________________________________ > Eventmachine-talk mailing list > Eventmachine-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/eventmachine-talk-------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/eventmachine-talk/attachments/20080127/552fa318/attachment.html
Francis Cianfrocca
2008-Jan-28 05:37 UTC
[Eventmachine-talk] stop_event_loop doesn''t let connection send
On Jan 26, 2008 11:38 PM, Marc-Andr? Cournoyer <macournoyer at gmail.com> wrote:> I got a server running: > > EventMachine.run do > EventMachine.start_server(@host, @port, Connection) > end > > When I call: > EventMachine.stop_event_loop > > it looks like it''s letting the connections finish their work before > returning and stopping the server, but doesn''t seem like the data is > sent back the the client. >Tell me what you mean by "connections finish their work." EM#stop works by scheduling a halt to the reactor loop, after the current pass. It''s entirely possible for some already-scheduled events to be passed to your Ruby code after a call to EM#stop. I suppose it would be possible to add a version of EM#stop that would first drain all outbound data. Would that help you? Why are you calling EM#stop in the first place? Does your design rely on starting, stopping and then restarting the reactor? If so, then there may be a more effective design. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/eventmachine-talk/attachments/20080128/e3669884/attachment.html
James Tucker
2008-Jan-28 07:56 UTC
[Eventmachine-talk] stop_event_loop doesn''t let connection send
On 28 Jan 2008, at 01:49, Marc-Andr? Cournoyer wrote:> same behaviour w/ EM::stop, the connections keep processing but the > data is not sent back to the client.$stdout.sync = true ?
Marc-André Cournoyer
2008-Jan-28 14:37 UTC
[Eventmachine-talk] stop_event_loop doesn''t let connection send
here''s my code to reproduce: =====require ''rubygems'' require ''eventmachine'' require ''open-uri'' class Connection < EventMachine::Connection def receive_data(data) EventMachine.stop send_data "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent- Length: 3\r\n\r\nok!" close_connection_after_writing end end fork do sleep 1 puts "Received " + open(''http://0.0.0.0:5000/'').read end EventMachine.run do puts ''Starting server ...'' EventMachine.start_server ''0.0.0.0'', 5000, Connection end ===== I get: ruby stop.rb Starting server ... /usr/local/lib/ruby/1.8/net/protocol.rb:133:in `sysread'': end of file reached (EOFError) from /usr/local/lib/ruby/1.8/net/protocol.rb:133:in `rbuf_fill'' from /usr/local/lib/ruby/1.8/timeout.rb:56:in `timeout'' from /usr/local/lib/ruby/1.8/timeout.rb:76:in `timeout'' from /usr/local/lib/ruby/1.8/net/protocol.rb:132:in `rbuf_fill'' from /usr/local/lib/ruby/1.8/net/protocol.rb:116:in `readuntil'' from /usr/local/lib/ruby/1.8/net/protocol.rb:126:in `readline'' from /usr/local/lib/ruby/1.8/net/http.rb:2029:in `read_status_line'' from /usr/local/lib/ruby/1.8/net/http.rb:2018:in `read_new'' ... 10 levels... from /usr/local/lib/ruby/1.8/open-uri.rb:30:in `open'' from stop.rb:15 from stop.rb:13:in `fork'' from stop.rb:13 I would like to let the connection send the data before closing. Is it possible? On 28-Jan-08, at 8:37 AM, Francis Cianfrocca wrote:> On Jan 26, 2008 11:38 PM, Marc-Andr? Cournoyer > <macournoyer at gmail.com> wrote: > I got a server running: > > EventMachine.run do > EventMachine.start_server(@host, @port, Connection) > end > > When I call: > EventMachine.stop_event_loop > > it looks like it''s letting the connections finish their work before > returning and stopping the server, but doesn''t seem like the data is > sent back the the client. > > > > Tell me what you mean by "connections finish their work." EM#stop > works by scheduling a halt to the reactor loop, after the current > pass. It''s entirely possible for some already-scheduled events to be > passed to your Ruby code after a call to EM#stop. > > I suppose it would be possible to add a version of EM#stop that > would first drain all outbound data. Would that help you? > > Why are you calling EM#stop in the first place? Does your design > rely on starting, stopping and then restarting the reactor? If so, > then there may be a more effective design. > _______________________________________________ > Eventmachine-talk mailing list > Eventmachine-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/eventmachine-talk-------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/eventmachine-talk/attachments/20080128/ca1a66c5/attachment-0001.html
Francis Cianfrocca
2008-Jan-28 15:04 UTC
[Eventmachine-talk] stop_event_loop doesn''t let connection send
On Jan 28, 2008 5:37 PM, Marc-Andr? Cournoyer <macournoyer at gmail.com> wrote:> here''s my code to reproduce: > =====> require ''rubygems'' > require ''eventmachine'' > require ''open-uri'' > > class Connection < EventMachine::Connection > def receive_data(data) > EventMachine.stop > send_data "HTTP/1.1 200 OK\r\nContent-Type: > text/plain\r\nContent-Length: 3\r\n\r\nok!" > close_connection_after_writing > end > end > > fork do > sleep 1 > puts "Received " + open(''http://0.0.0.0:5000/'').read<http://0.0.0.0:5000/%27%29.read> > end > > EventMachine.run do > puts ''Starting server ...'' > EventMachine.start_server ''0.0.0.0'', 5000, Connection > end > =====> >If you want to stop the reactor after serving only a single connection, then put the EM#stop call in the unbind method of the connection object. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/eventmachine-talk/attachments/20080128/8af26dfd/attachment.html
Marc-André Cournoyer
2008-Jan-28 16:06 UTC
[Eventmachine-talk] stop_event_loop doesn''t let connection send
On 28-Jan-08, at 6:04 PM, Francis Cianfrocca wrote:> > If you want to stop the reactor after serving only a single > connection, then put the EM#stop call in the unbind method of the > connection object.no, this was just to show you what I''m trying to accomplish. It''s for my web server Thin, I don''t wanna drop any request that is currently processing. EventMachine.stop is called when a INT signal is caught. Remove the stop from my example and imagine it''s stopped from another thread.
Francis Cianfrocca
2008-Jan-28 17:34 UTC
[Eventmachine-talk] stop_event_loop doesn''t let connection send
On Jan 28, 2008 7:06 PM, Marc-Andr? Cournoyer <macournoyer at gmail.com> wrote:> On 28-Jan-08, at 6:04 PM, Francis Cianfrocca wrote: > > > > If you want to stop the reactor after serving only a single > > connection, then put the EM#stop call in the unbind method of the > > connection object. > > no, this was just to show you what I''m trying to accomplish. It''s for > my web server Thin, I don''t wanna drop any request that is currently > processing. > EventMachine.stop is called when a INT signal is caught. > Remove the stop from my example and imagine it''s stopped from another > thread. > > >So when you catch INT, you want to complete all the pending transactions before you terminate the program? -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/eventmachine-talk/attachments/20080128/c92e2ef2/attachment.html
Marc-André Cournoyer
2008-Jan-28 17:50 UTC
[Eventmachine-talk] stop_event_loop doesn''t let connection send
On 28-Jan-08, at 8:34 PM, Francis Cianfrocca wrote:> > So when you catch INT, you want to complete all the pending > transactions before you terminate the program? >Exactly! Is there something builtin EM to do this or do I have to implement this logic myself?
Francis Cianfrocca
2008-Jan-29 05:25 UTC
[Eventmachine-talk] stop_event_loop doesn''t let connection send
On Jan 28, 2008 8:50 PM, Marc-Andr? Cournoyer <macournoyer at gmail.com> wrote:> > On 28-Jan-08, at 8:34 PM, Francis Cianfrocca wrote: > > > > So when you catch INT, you want to complete all the pending > > transactions before you terminate the program? > > > > Exactly! > > Is there something builtin EM to do this or do I have to implement > this logic myself? > >You have several choices. The key question is: when you receive SIGINT, do you want to force connections to close immediately, or do you want them to finish at their own pace? In either case, you''ll need either a class variable or a global array that remembers all accepted connections. Then, when you want to run them down, all you have to do is keep track of when they end by adding some code to the #unbind method. Your sigint handler should NOT do all this work. Rather, do something like this: trap("INT") { EM.next_tick { your_function_to_stop_the_reactor } } You also need to call EM#stop_tcp_server to keep new connections from being accepted while the existing ones drain out. Feel free to contact me off list and I can help you get this written. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/eventmachine-talk/attachments/20080129/e828f309/attachment.html
Marc-André Cournoyer
2008-Jan-29 22:02 UTC
[Eventmachine-talk] stop_event_loop doesn''t let connection send
thx Francis that worked perfectly! On 29-Jan-08, at 8:25 AM, Francis Cianfrocca wrote:> On Jan 28, 2008 8:50 PM, Marc-Andr? Cournoyer > <macournoyer at gmail.com> wrote: > > On 28-Jan-08, at 8:34 PM, Francis Cianfrocca wrote: > > > > So when you catch INT, you want to complete all the pending > > transactions before you terminate the program? > > > > Exactly! > > Is there something builtin EM to do this or do I have to implement > this logic myself? > > > You have several choices. > > The key question is: when you receive SIGINT, do you want to force > connections to close immediately, or do you want them to finish at > their own pace? > > In either case, you''ll need either a class variable or a global > array that remembers all accepted connections. Then, when you want > to run them down, all you have to do is keep track of when they end > by adding some code to the #unbind method. > > Your sigint handler should NOT do all this work. Rather, do > something like this: > > trap("INT") { > EM.next_tick { your_function_to_stop_the_reactor } > } > > You also need to call EM#stop_tcp_server to keep new connections > from being accepted while the existing ones drain out. > > Feel free to contact me off list and I can help you get this written. > _______________________________________________ > Eventmachine-talk mailing list > Eventmachine-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/eventmachine-talk-------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/eventmachine-talk/attachments/20080130/82d72185/attachment.html