Regarding wheel ID vs. reference, I meant it might be better to pass around the client's wheel ID rather than a reference to the wheel itself. As in:
$kernel->yield('keepalive', $io_wheel->ID, $kernel);
and
keepalive => sub {
my ( $io_wheel_id, $kernel, $heap ) = @_[ ARG0, KERNEL ];
return unless exists $heap->{clients}{$io_wheel_id};
$heap->{clients}{$io_wheel_id}->put("keepalive");
$kernel->delay( 'keepalive' => 10, $io_wheel_id );
},
It's more work, for you and for the program, but it stops repeating the timer after the client has disconnected. Of course, another way is to make sure that the timer is canceled wherever you are deleting the client. If you find yourself doing that in multiple places, refactor it into a subroutine like:
sub delete_client {
my ($kernel, $heap, $io_wheel_id) = @_;
delete $heap->{clients}{$io_wheel_id};
$kernel->delay(keepalive => undef);
}
which would be called the usual Perlish way
on_connect_error => sub {
# Handle client error, including disconnect.
my $wheel_id = $_[ARG3];
delete_client(@_[KERNEL, HEAP], $wheel_id);
},
It really helps when cleanup gets complex, as it often does when programs evolve and grow.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.