From 7916c6b185cd3abb54a463e63e6fe7c727ec2b58 Mon Sep 17 00:00:00 2001 From: Michael Bishop Date: Fri, 9 Nov 2018 12:06:37 -0400 Subject: [PATCH] [DEVOPS-1126] throttle github status calls to remain under api ratelimits --- src/lib/Hydra/Plugin/GithubStatus.pm | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/lib/Hydra/Plugin/GithubStatus.pm b/src/lib/Hydra/Plugin/GithubStatus.pm index 08ba25bb..a0ad3393 100644 --- a/src/lib/Hydra/Plugin/GithubStatus.pm +++ b/src/lib/Hydra/Plugin/GithubStatus.pm @@ -6,6 +6,7 @@ use HTTP::Request; use JSON; use LWP::UserAgent; use Hydra::Helper::CatalystUtils; +use List::Util qw(max); sub toGithubState { my ($buildStatus) = @_; @@ -65,6 +66,21 @@ sub common { $req->content($body); my $res = $ua->request($req); print STDERR $res->status_line, ": ", $res->decoded_content, "\n" unless $res->is_success; + my $limit = $res->header("X-RateLimit-Limit"); + my $limitRemaining = $res->header("X-RateLimit-Remaining"); + my $limitReset = $res->header("X-RateLimit-Reset"); + my $now = time(); + my $diff = $limitReset - $now; + my $delay = (($limit - $limitRemaining) / $diff) * 5; + if ($limitRemaining < 1000) { + $delay = max(1, $delay); + } + if ($limitRemaining < 2000) { + print STDERR "GithubStatus ratelimit $limitRemaining/$limit, resets in $diff, sleeping $delay\n"; + sleep $delay; + } else { + print STDERR "GithubStatus ratelimit $limitRemaining/$limit, resets in $diff\n"; + } } } }