How do I distinguish between Ctrl+C vs regular unbind using EventMachine?

65 views
Skip to first unread message

Brian

unread,
Dec 27, 2011, 1:04:19 AM12/27/11
to EventMachine
Is there a good way to distinguish between when I do a Ctrl+C to
manually close all connections, and when a client disconnects from my
server? They both seem to call the unbind method.

When I manually shut down using Ctrl+C, I can probably try to
reconnect to those peers later since they are still online. If a
client disconnects from me I don't want to try reconnecting to it for
a while, because it probably shut down even though I'm still up.

Aman Gupta

unread,
Dec 27, 2011, 1:08:40 AM12/27/11
to eventm...@googlegroups.com
With the latest 1.0 beta, unbind takes an optional argument which contains the errno that triggered the unbind. This won't help you distinguish between a ctrl+c and graceful disconnect though.

For reconnect behavior, I usually use EM.next_tick in unbind to reconnect. In a ctrl+c situation, there will be no next_tick since the reactor is shutting down, and thus no reconnect attempts.

  Aman


--
You received this message because you are subscribed to the Google Groups "EventMachine" group.
To post to this group, send email to eventm...@googlegroups.com.
To unsubscribe from this group, send email to eventmachine...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/eventmachine?hl=en.


Brian

unread,
Dec 27, 2011, 2:34:28 AM12/27/11
to EventMachine
Thanks!

The EM.next_tick was what I needed (had never thought of it in this
context of avoiding a ctrl-c).

Really appreciate it.

Iñaki Baz Castillo

unread,
Dec 27, 2011, 3:58:32 AM12/27/11
to eventm...@googlegroups.com
2011/12/27 Brian <barms...@gmail.com>:

You can trap de INT signal with:

trap :INT do
@interrupted = true
end

and then in unbind() callback inspect such attribute.

--
Iñaki Baz Castillo
<i...@aliax.net>

Brian

unread,
Dec 27, 2011, 7:48:49 PM12/27/11
to EventMachine
Hi,

Interesting, I actually tried this - but if I trap the INT then unbind
doesn't get called.

Should you set @interrupted = true and then call close_connection or
unbind manually?

Thanks!
Brian

On Dec 27, 12:58 am, Iñaki Baz Castillo <i...@aliax.net> wrote:
> 2011/12/27 Brian <barmstr...@gmail.com>:

Iñaki Baz Castillo

unread,
Dec 28, 2011, 3:56:11 AM12/28/11
to eventm...@googlegroups.com
2011/12/28 Brian <barms...@gmail.com>:

> Interesting, I actually tried this - but if I trap the INT then unbind
> doesn't get called.
>
> Should you set @interrupted = true and then call close_connection or
> unbind manually?

Right. You should call EM::Connection.close_connection on the instance
you want to stop.

Reply all
Reply to author
Forward
0 new messages