Created
November 30, 2022 17:11
-
-
Save JJ/830f1274a3e39e384dae59620ad94007 to your computer and use it in GitHub Desktop.
Smarter server post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sub format-error(X::Cro::HTTP::Error $_) { | |
my $status-line = .response.Str.lines.first; | |
my $resp-body = do { await .response.body-blob }.decode; | |
my $req-method = .request.method; | |
my $req-target = .request.target; | |
my $req-body = do { await .request.body-blob }.decode; | |
"ERROR $status-line WITH $resp-body FOR $req-method $req-target WITH $req-body" | |
} | |
sub server-post($data) { | |
our $cro; | |
do { | |
my $r = await $cro.post: "{SERVER-URI}/{DATA-PATH}", body => $data; | |
my $count = NUMBER-OF-RETRIES; | |
while $count-- and $r.status == 503|401 { | |
sleep COOLING-OFF-PERIOD; | |
server-login if $r.status == 401; | |
$r = await $cro.post: "{SERVER-URI}/{DATA-PATH}", body => $data; | |
} | |
await $r.body | |
} | |
CATCH { | |
when X::Cro::HTTP::Client::Timeout { | |
note 'got a timeout, cooling off for a little bit more'; | |
sleep 5 * COOLING-OFF-PERIOD; | |
server-login | |
} | |
when X::Cro::HTTP::Error { | |
note format-error $_ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment