Mojo::UserAgent::Role::Retry(3)

NAME
Mojo::UserAgent::Role::Retry - Retry requests on failure

VERSION
version 0.003

SYNOPSIS
  use Mojo::UserAgent;
  use v5.10;

  my $ua = Mojo::UserAgent->with_roles('+Retry')->new;
  say $ua->get('https://www.perl.org/')->result->dom->at('title')->text;

DESCRIPTION
This role adds retry capabilities to Mojo::UserAgent HTTP requests. By default (see "retry_policy"), if a connection error is returned, or if a 429 or 503 response code is received, then a retry is invoked after a wait period.

ATTRIBUTES
Mojo::UserAgent::Role::Retry adds the following attributes:

  retries
Defaults to 5. The maximum number of retries. If after all retries, the request still fails, then the last response is returned back to the caller to interpret.

  my $ua = Mojo::UserAgent->with_roles('+Retry')->new(retries => 5);

  retry_wait_min
Defaults to 1. The minimum wait time between retries in seconds. The Retry-After <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After> header value from the response is used if it is greater than this value but lower than "retry_wait_max".

  my $ua = Mojo::UserAgent->with_roles('+Retry')->new(retry_wait_min => 1);

  retry_wait_max
Defaults to 20. The maximum wait time between retries in seconds. It's used if the Retry-After <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After> header value from the response is greater than this value.

  my $ua = Mojo::UserAgent->with_roles('+Retry')->new(retry_wait_max => 20);

  retry_policy
The policy to determine if a request should be retried. It must return a subroutine that returns false if the request should be retried, or true otherwise. On each invocation, the subroutine receives a new Mojo::Transaction::HTTP to evaluate.

By default, it retries on connection errors, 429 and 503 HTTP response codes.

  my $ua = Mojo::UserAgent->with_roles('+Retry')->new(retry_policy => sub {
    # Retry on 418 HTTP response codes
    if (shift->res->code == 418) { return 0; }
    return 1;
  });

SEE ALSO
Mojolicious::UserAgent, Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.

AUTHOR
Christian Segundo <ssmn@cpan.org>

COPYRIGHT AND LICENSE
This software is copyright (c) 2024 by Christian Segundo <ssmn@cpan.org>.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.