1Retry(3) User Contributed Perl Documentation Retry(3)
2
3
4
6 Retry
7
9 A one-feature module, this provides a method to wrap any function in
10 automatic retry logic, with exponential back-off delays, and a callback
11 for each time an attempt fails.
12
13 Example:
14
15 use Retry;
16 use Try::Tiny;
17 use LWP::UserAgent;
18
19 my $code_to_retry = sub {
20 my $r = LWP::UserAgent->new->get("http://example.com");
21 die $r->status_line unless $r->is_success;
22 return $r;
23 };
24
25 my $agent = Retry->new(
26 # This callback is optional:
27 failure_callback => sub { warn "Transient error: " . $_[0]; },
28 );
29
30 try {
31 $agent->retry($code_to_retry)
32 }
33 catch {
34 warn "All attempts failed: $_";
35 };
36
38 retry_delay
39 This is the initial delay used when the routine failed, before retrying
40 again.
41
42 Every subsequent failure doubles the amount.
43
44 It defaults to 8 seconds.
45
46 max_retry_attempts
47 The maximum number of retries we should attempt before giving up
48 completely.
49
50 It defaults to 5.
51
52 failure_callback
53 Optional. To be notified of *every* failure (even if we eventually
54 succeed on a later retry), install a subroutine callback here.
55
56 For example:
57
58 Retry->new(
59 failure_callback => sub { warn "failed $count++ times" }
60 );
61
63 retry
64 Its purpose is to execute the passed subroutine, over and over, until
65 it succeeds, or the number of retries is exceeded. The delay between
66 retries increases exponentially. (Failure is indicated by the sub
67 dying)
68
69 If the subroutine succeeds, then its scalar return value will be
70 returned by retry.
71
72 For example, you could replace this:
73
74 my $val = unreliable_web_request();
75
76 With this:
77
78 my $val = Retry->new->retry(
79 sub { unreliable_web_request() }
80 );
81
83 Toby Corkindale -- <https://github.com/TJC/>
84
86 This module is released under the Perl Artistic License 2.0:
87 <http://www.perlfoundation.org/artistic_license_2_0>
88
89 It is based upon source code which is Copyright 2010 Strategic Data Pty
90 Ltd, however it is used and released with permission.
91
93 Attempt
94
95 Retry differs from Attempt in having exponentially increasing delays,
96 and by having a callback inbetween attempts.
97
98 However Attempt has a simpler syntax.
99
100
101
102perl v5.34.0 2021-07-22 Retry(3)