Action-CircuitBreaker

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN


    The code to run to check if the error should count towards the circuit
    breaker. It defaults to:

      # Returns true if there were an exception evaluating to something true
      sub { $_[0] }

    It will be given these arguments:

      * as first argument, a scalar which is the value of any exception
      that were raised by the $attempt_code. Otherwise, undef.

      * as second argument, a HashRef, which contains these keys:

    action_retry

      it's a reference on the ActionRetry instance. That way you can have
      access to the other attributes.

    attempt_result

lib/Action/CircuitBreaker.pm  view on Meta::CPAN

    is => 'ro',
    lazy => 1,
    default => sub { 10 },
);

# the current number of retries
has _current_retries_number => (
    is => 'rw',
    lazy => 1,
    default => sub { 0 },
    init_arg => undef,
    clearer => 1,
);


has open_time => (
    is => 'ro',
    lazy => 1,
    default => sub { 10 },
);

# Timestamp at which the circuit is available again
has _circuit_open_until => (
    is => 'rw',
    default => sub { 0 },
    init_arg => undef,
);


sub run {
    my ($self, $attempt_code) = @_;

    if (my $timestamp = $self->_circuit_open_until) {
        # we can't execute until the timestamp has done
        my ($seconds, $microseconds) = gettimeofday;
        $seconds * 1000 + int($microseconds / 1000) >= $timestamp

lib/Action/CircuitBreaker.pm  view on Meta::CPAN

  # Returns true if there were an exception evaluating to something true
  sub { $_[0] }

It will be given these arguments:

=over

=item * 

as first argument, a scalar which is the value of any exception that were
raised by the C<$attempt_code>. Otherwise, undef.

=item *

as second argument, a HashRef, which contains these keys:

=back

=over

=item action_retry

t/00-report-prereqs.t  view on Meta::CPAN


# This test was generated by Dist::Zilla::Plugin::Test::ReportPrereqs 0.027

use Test::More tests => 1;

use ExtUtils::MakeMaker;
use File::Spec;

# from $version::LAX
my $lax_version_re =
    qr/(?: undef | (?: (?:[0-9]+) (?: \. | (?:\.[0-9]+) (?:_[0-9]+)? )?
            |
            (?:\.[0-9]+) (?:_[0-9]+)?
        ) | (?:
            v (?:[0-9]+) (?: (?:\.[0-9]+)+ (?:_[0-9]+)? )?
            |
            (?:[0-9]+)? (?:\.[0-9]+){2,} (?:_[0-9]+)?
        )
    )/x;

# hide optional CPAN::Meta modules from prereq scanner

t/00-report-prereqs.t  view on Meta::CPAN

        for my $mod ( sort keys %{ $req_hash->{$phase}{$type} } ) {
            next if $mod eq 'perl';
            next if grep { $_ eq $mod } @exclude;

            my $file = $mod;
            $file =~ s{::}{/}g;
            $file .= ".pm";
            my ($prefix) = grep { -e File::Spec->catfile($_, $file) } @INC;

            my $want = $req_hash->{$phase}{$type}{$mod};
            $want = "undef" unless defined $want;
            $want = "any" if !$want && $want == 0;

            my $req_string = $want eq 'any' ? 'any version required' : "version '$want' required";

            if ($prefix) {
                my $have = MM->parse_version( File::Spec->catfile($prefix, $file) );
                $have = "undef" unless defined $have;
                push @reports, [$mod, $want, $have];

                if ( $DO_VERIFY_PREREQS && $HAS_CPAN_META && $type eq 'requires' ) {
                    if ( $have !~ /\A$lax_version_re\z/ ) {
                        push @dep_errors, "$mod version '$have' cannot be parsed ($req_string)";
                    }
                    elsif ( ! $full_prereqs->requirements_for( $phase, $type )->accepts_module( $mod => $have ) ) {
                        push @dep_errors, "$mod version '$have' is not in required range '$want'";
                    }
                }

t/20-retry.t  view on Meta::CPAN


use Try::Tiny;
use Action::Retry;

{
    my $died = 0;
    my $action = Action::CircuitBreaker->new(max_retries_number => 9);
    try {
      my $foo = Action::Retry->new(
          attempt_code => sub { $action->run(sub { die "plop"; }) }, # ie. the database failed
          on_failure_code => sub { my ($error, $h) = @_; die $error; }, # by default Action::Retry would return undef
      )->run();
    } catch {
        $died = 1;
    };

    is($died, 1);
}

done_testing;



( run in 2.144 seconds using v1.01-cache-2.11-cpan-d80b1682f3f )