Quick actions

cmd+k|ctrl+k

Navigation

Languages

Untitled

Snippet info

Language

Raku

Visibility

unlisted

Author

legacy-anonymous

Created

2018-01-25T11:52:13Z

Updated

2018-01-25T11:52:13Z

use v6;

class X::Timeout is Exception {
    method message { "Timed out" }
}

sub timeout(Supply $in, $timeout) {
    supply {
        my $last = now;
        whenever $in {
            $last = now;
            emit $_
        }
        whenever Supply.interval(0.1) {
            if now - $last > $timeout {
                X::Timeout.new.throw;
            }
        }
    }
}

react {
    whenever IO::Socket::Async.listen('0.0.0.0', 3333) -> $conn {
        whenever timeout($conn.Supply.lines, 4) -> $line {
            $conn.print("$line\n");
            QUIT {
                when X::Timeout {
                    $conn.print("TIMEOUT\n");
                    $conn.close;
                }
            }
        }
    }
    whenever signal(SIGINT) { done(); exit; }
}
INFO