Robert Spier wrote:
Definitely on my list of things to add.  We've had issues with
connection limiting and lack of timeouts.

Do you have an idea of how you would do it?


Two different pieces.

First, a timeout callback for each "transport".
(tcpserver,select,fork).  In some cases, this might just set a flag,
in others it'll trigger cleanups and terminate a process.

Second, a scheme for enabling/disabling the timeouts.  We only want to
do this kind of timeout when we're waiting for IO..

-R

{ # variable scope for $to and $toh my $to; # timeout sub set_timeout($) { $to = shift }; sub enable_timeout() { alarm($to) }; sub disable_timeout() { alarm(0) }; my $toh; # timeout handler $toh = sub { die "timed out\n" };

                sub set_timeout_handler(&){
                        $toh = shift;
                };
                $SIG{ALRM} = sub { &$toh };

        }


so after setting that up, wrap all IO:

instead of

        $line = <SOCK>

have

        set_timeout $waiting_for_a_line;
        enable_timeout;
        undef($line); # what if the alarm goes off while
                      # we are disabling it?
        set_timeout_handler { die "timed out XYZ" };
        eval {
                $line = <SOCK>;
                disable_timeout;
        };
        if($@ && !$line){
        if( $@ =~ /timed out XYZ/ ){
                # wait longer? give up? i don't know
                ...
        }else{ die "readsock threw execption: $@" };

        };


and that can be wrapped in a couple different myreadline functions depending on what the timeout result is



not sure how you want to integrate "callbacks" in -- perl's
native $SIG{ALRM} being settable is "the timeout callback" in
a naive way, the fact that you can die from alarm in an eval-block
and you exit the eval normally is very cool and gives you
the ability to deal with the timeout as an exception, as shown
above.

verification:


[EMAIL PROTECTED]:~> perl -le 'sub z(&){ print $s=shift; print &$s }; alarm 1; eval {z { sleep 2 ; die 3 }}; print "threw: $@"'
CODE(0x817bdac)
Alarm clock
[EMAIL PROTECTED]:~> perl -le '$SIG{ALRM}=sub{die 4};sub z(&){ print $s=shift; print &$s }; alarm 1; eval {z { sleep 2 ; die 3 }}; print "threw: $@"'
CODE(0x817ff38)
threw: 4 at -e line 1.


[EMAIL PROTECTED]:~>

--
[EMAIL PROTECTED]
"There's a fine line between participation and mockery" -- Scott Adams

Reply via email to