1IO::Async::Timer::CountUdsoewrn(C3o)ntributed Perl DocumIeOn:t:aAtsiyonnc::Timer::Countdown(3)
2
3
4
6 "IO::Async::Timer::Countdown" - event callback after a fixed delay
7
9 use IO::Async::Timer::Countdown;
10
11 use IO::Async::Loop;
12 my $loop = IO::Async::Loop->new();
13
14 my $timer = IO::Async::Timer::Countdown->new(
15 delay => 10,
16
17 on_expire => sub {
18 print "Sorry, your time's up\n";
19 $loop->loop_stop;
20 },
21 );
22
23 $timer->start;
24
25 $loop->add( $timer );
26
27 $loop->loop_forever;
28
30 This module provides a subclass of IO::Async::Timer for implementing
31 one-shot fixed delays. The object implements a countdown timer, which
32 invokes its callback after the given period from when it was started.
33 After it has expired the Timer may be started again, when it will wait
34 the same period then invoke the callback again. A timer that is
35 currently running may be stopped or reset.
36
37 For a "Timer" object that repeatedly runs a callback at regular
38 intervals, see instead IO::Async::Timer::Periodic.
39
40 This object may be used in one of two ways; with a callback function,
41 or as a base class.
42
43 Callbacks
44 If the "on_expire" key is supplied to the constructor, it should
45 contain a CODE reference to a callback function to be invoked at
46 the appropriate time:
47
48 $on_expire->( $self )
49
50 Base Class
51 If a subclass is built, then it can override the "on_expire"
52 method.
53
54 $self->on_expire()
55
57 The following named parameters may be passed to "new" or "configure":
58
59 on_expire => CODE
60 CODE reference to callback to invoke when the timer expires. If
61 not supplied, the subclass method will be called instead.
62
63 delay => NUM
64 The delay in seconds after starting the timer until it expires.
65 Cannot be changed if the timer is running.
66
67 Once constructed, the timer object will need to be added to the "Loop"
68 before it will work. It will also need to be started by the "start"
69 method.
70
71 $timer->reset
72 If the timer is running, restart the countdown period from now. If the
73 timer is not running, this method has no effect.
74
76 Watchdog Timer
77 Because the "reset" method restarts a running countdown timer back to
78 its full period, it can be used to implement a watchdog timer. This is
79 a timer which will not expire provided the method is called at least as
80 often as it is configured. If the method fails to be called, the timer
81 will eventually expire and run its callback.
82
83 For example, to expire an accepted connection after 30 seconds of
84 inactivity:
85
86 ...
87
88 on_accept => sub {
89 my ( $newclient ) = @_;
90
91 my $stream;
92
93 my $watchdog = IO::Async::Timer::Countdown->new(
94 delay => 30,
95
96 on_expire => sub { $stream->close },
97 );
98 $stream->add_child( $watchdog );
99
100 $stream = IO::Async::Stream->new(
101 handle => $newclient,
102
103 on_read => sub {
104 my ( $self, $buffref, $closed ) = @_;
105 $watchdog->reset;
106
107 ...
108 },
109
110 on_closed => sub {
111 $watchdog->stop;
112 },
113 ) );
114
115 $watchdog->start;
116
117 $loop->add( $watchdog );
118 }
119
121 Paul Evans <leonerd@leonerd.org.uk>
122
123
124
125perl v5.12.1 2010-06-09 IO::Async::Timer::Countdown(3)