Paws

 view release on metacpan or  search on metacpan

lib/Paws/API/Retry.pm  view on Meta::CPAN

package Paws::API::Retry;
  use Moose;
  use MooseX::ClassAttribute;
  use Paws::Exception;

  class_has default_rules => (is => 'ro', isa => 'ArrayRef', default => sub { [
    # bad_gateway
    sub { defined $_[0]->http_status and $_[0]->http_status == 502 },
    # gateway_timeout
    sub { defined $_[0]->http_status and $_[0]->http_status == 504 },
    # general_server_error
    sub { defined $_[0]->http_status and $_[0]->http_status == 500 },
    # general_socket_errors
    sub { $_[0]->code eq 'ConnectionError' },
    # limit_exceeded
    sub { defined $_[0]->http_status and $_[0]->http_status == 509 },
    # request_throttled_exception
    sub { defined $_[0]->http_status and $_[0]->http_status == 400 and $_[0]->code eq "RequestThrottledException" },
    # service_unavailable
    sub { defined $_[0]->http_status and $_[0]->http_status == 503 },
    # throttled_exception
    sub { defined $_[0]->http_status and $_[0]->http_status == 400 and $_[0]->code eq "ThrottledException" },
    # throttling
    sub { defined $_[0]->http_status and $_[0]->http_status == 400 and $_[0]->code eq "Throttling" },
    # throttling_exception
    sub { defined $_[0]->http_status and $_[0]->http_status == 400 and $_[0]->code eq "ThrottlingException" },
    # throughput_exceeded
    sub { defined $_[0]->http_status and $_[0]->http_status == 400 and $_[0]->code eq "ProvisionedThroughputExceededException" },
    # too_many_requests
    sub { defined $_[0]->http_status and $_[0]->http_status == 429 },
  ] });

  has max_tries => (is => 'ro', required => 1);
  has type => (is => 'ro', required => 1);

  has tries => (is => 'rw', default => 0);

  has retry_rules => (is => 'ro', required => 1);

  has error_for_die => (is => 'rw');
  has operation_result => (is => 'rw');

  has generator => (is => 'ro');

  around BUILDARGS => sub {
    my ($orig, $class, %args) = @_;

    if ($args{ type } eq 'exponential') {
      $args{ generator } = sub {
        my $self = shift;
        (2 ** ($self->tries - 2)) + (rand(1) / 2);
      };
    } else {
      die "Don't know how to make a retry type of $args{ type }";
    }

    return $class->$orig(%args);
  };

  sub should_retry {
    my $self = shift;
    my $res = $self->operation_result;

    if ($self->result_is_exception and $self->_still_has_retries and $self->exception_is_retriable){
      return 1;
    }



( run in 1.141 second using v1.01-cache-2.11-cpan-39bf76dae61 )