Skip to content

Commit

Permalink
[DEVOPS-1126] throttle github status calls to remain under api rateli…
Browse files Browse the repository at this point in the history
…mits
  • Loading branch information
cleverca22 committed Nov 9, 2018
1 parent 1c44de1 commit 7916c6b
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/lib/Hydra/Plugin/GithubStatus.pm
Expand Up @@ -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) = @_;
Expand Down Expand Up @@ -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";
}
}
}
}
Expand Down

0 comments on commit 7916c6b

Please sign in to comment.