Skip to content

Instantly share code, notes, and snippets.

@kodaka
Created March 9, 2022 07:30
Show Gist options
  • Save kodaka/1a4bd612ed71a55b19c7eb0707d1cd2a to your computer and use it in GitHub Desktop.
Save kodaka/1a4bd612ed71a55b19c7eb0707d1cd2a to your computer and use it in GitHub Desktop.
#!perl
#
# eg:
# $ perl aws_stepfunctions_retry.pl --interval_seconds 5 --max_attempts 20 --backoff_rate 1.7
#
use strict;
use warnings;
use Getopt::Long;
use Text::Table;
# default parameters come from https://docs.aws.amazon.com/step-functions/latest/dg/concepts-error-handling.html#error-handling-retrying-after-an-error
my $interval_seconds = 1.0;
my $max_attempts = 3;
my $backoff_rate = 2.0;
GetOptions(
'interval_seconds=f' => \$interval_seconds,
'max_attempts=i' => \$max_attempts,
'backoff_rate=f' => \$backoff_rate,
);
my $table = Text::Table->new(
"retry\ncount",
"after\nseconds",
"total\nseconds",
"total\nmins",
"total\nhours",
"total\ndays",
);
my $total_seconds = 0;
for my $retry_count (1 .. $max_attempts) {
my $after_seconds = $interval_seconds * ($backoff_rate ** ($retry_count - 1));
$total_seconds += $after_seconds;
my $total_mins = $total_seconds / 60;
my $total_hours = $total_mins / 60;
my $total_days = $total_hours / 24;
$table->add(
$retry_count,
sprintf('%.3f', $after_seconds),
sprintf('%.3f', $total_seconds),
sprintf('%.3f', $total_mins),
sprintf('%.3f', $total_hours),
sprintf('%.3f', $total_days),
);
}
printf "interval_seconds: %f\n", $interval_seconds;
printf "max_attempts: %d\n", $max_attempts;
printf "backoff_rate: %f\n", $backoff_rate;
print $table->stringify();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment