Skip to content

Instantly share code, notes, and snippets.

@hoelzro
Created March 26, 2018 03:16
Show Gist options
  • Save hoelzro/87e3fcbe66b15a3694ff0c2a8d2972a5 to your computer and use it in GitHub Desktop.
Save hoelzro/87e3fcbe66b15a3694ff0c2a8d2972a5 to your computer and use it in GitHub Desktop.
package Local::Twitter::API::RateLimiting {
use Moo::Role;
use HTTP::Status qw(HTTP_TOO_MANY_REQUESTS);
use namespace::clean;
around send_request => sub {
my $orig = shift;
my $self = shift;
my $res = $self->$orig(@_);
while($res->code == HTTP_TOO_MANY_REQUESTS) {
my $sleep_time = $res->header('x-rate-limit-reset') - time;
sleep $sleep_time; # XXX modifiable, please
$res = $self->$orig(@_);
}
return $res;
};
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment