1IO::Async::Timer::CountUdsoewrn(C3o)ntributed Perl DocumIeOn:t:aAtsiyonnc::Timer::Countdown(3)
2
3
4

NAME

6       "IO::Async::Timer::Countdown" - event callback after a fixed delay
7

SYNOPSIS

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->stop;
20           },
21        );
22
23        $timer->start;
24
25        $loop->add( $timer );
26
27        $loop->run;
28

DESCRIPTION

30       This subclass of IO::Async::Timer implements one-shot fixed delays.
31       The object implements a countdown timer, which invokes its callback
32       after the given period from when it was started. After it has expired
33       the Timer may be started again, when it will wait the same period then
34       invoke the callback again. A timer that is currently running may be
35       stopped or reset.
36
37       For a "Timer" object that repeatedly runs a callback at regular
38       intervals, see instead IO::Async::Timer::Periodic. For a "Timer" that
39       invokes its callback at a fixed time in the future, see
40       IO::Async::Timer::Absolute.
41

EVENTS

43       The following events are invoked, either using subclass methods or CODE
44       references in parameters:
45
46   on_expire
47       Invoked when the timer expires.
48

PARAMETERS

50       The following named parameters may be passed to "new" or "configure":
51
52   on_expire => CODE
53       CODE reference for the "on_expire" event.
54
55   delay => NUM
56       The delay in seconds after starting the timer until it expires. Cannot
57       be changed if the timer is running. A timer with a zero delay expires
58       "immediately".
59
60   remove_on_expire => BOOL
61       Optional. If true, remove this timer object from its parent notifier or
62       containing loop when it expires. Defaults to false.
63
64       Once constructed, the timer object will need to be added to the "Loop"
65       before it will work. It will also need to be started by the "start"
66       method.
67

METHODS

69   is_expired
70          $expired = $timer->is_expired
71
72       Returns true if the Timer has already expired.
73
74   reset
75          $timer->reset
76
77       If the timer is running, restart the countdown period from now. If the
78       timer is not running, this method has no effect.
79

EXAMPLES

81   Watchdog Timer
82       Because the "reset" method restarts a running countdown timer back to
83       its full period, it can be used to implement a watchdog timer. This is
84       a timer which will not expire provided the method is called at least as
85       often as it is configured. If the method fails to be called, the timer
86       will eventually expire and run its callback.
87
88       For example, to expire an accepted connection after 30 seconds of
89       inactivity:
90
91        ...
92
93        on_accept => sub {
94           my ( $newclient ) = @_;
95
96           my $watchdog = IO::Async::Timer::Countdown->new(
97              delay => 30,
98
99              on_expire => sub {
100                 my $self = shift;
101
102                 my $stream = $self->parent;
103                 $stream->close;
104              },
105           );
106
107           my $stream = IO::Async::Stream->new(
108              handle => $newclient,
109
110              on_read => sub {
111                 my ( $self, $buffref, $eof ) = @_;
112                 $watchdog->reset;
113
114                 ...
115              },
116
117              on_closed => sub {
118                 $watchdog->stop;
119              },
120           ) );
121
122           $stream->add_child( $watchdog );
123           $watchdog->start;
124
125           $loop->add( $watchdog );
126        }
127
128       Rather than setting up a lexical variable to store the Stream so that
129       the Timer's "on_expire" closure can call "close" on it, the
130       parent/child relationship between the two Notifier objects is used. At
131       the time the Timer "on_expire" closure is invoked, it will have been
132       added as a child notifier of the Stream; this means the Timer's
133       "parent" method will return the Stream Notifier. This enables it to
134       call "close" without needing to capture a lexical variable, which would
135       create a cyclic reference.
136
137   Fixed-Delay Repeating Timer
138       The "on_expire" event fires a fixed delay after the "start" method has
139       begun the countdown. The "start" method can be invoked again at some
140       point during the "on_expire" handling code, to create a timer that
141       invokes its code regularly a fixed delay after the previous invocation
142       has finished. This creates an arrangement similar to an
143       IO::Async::Timer::Periodic, except that it will wait until the previous
144       invocation has indicated it is finished, before starting the countdown
145       for the next call.
146
147        my $timer = IO::Async::Timer::Countdown->new(
148           delay => 60,
149
150           on_expire => sub {
151              my $self = shift;
152
153              start_some_operation(
154                 on_complete => sub { $self->start },
155              );
156           },
157        );
158
159        $timer->start;
160        $loop->add( $timer );
161
162       This example invokes the "start_some_operation" function 60 seconds
163       after the previous iteration has indicated it has finished.
164

AUTHOR

166       Paul Evans <leonerd@leonerd.org.uk>
167
168
169
170perl v5.32.0                      2020-09-24    IO::Async::Timer::Countdown(3)
Impressum