Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal to handle rate limiting #71

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/octopi/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Octopi
# Dummy class, so AnonymousApi and AuthApi have somewhere to inherit from
class Api
include Self
attr_accessor :format, :login, :token, :trace_level, :read_only
attr_accessor :format, :login, :token, :trace_level, :read_only, :conn_remaining
end

# Used for accessing the Github API anonymously
Expand Down Expand Up @@ -173,6 +173,10 @@ def submit(path, params = {}, klass=nil, format = :yaml, &block)
end
end
begin
## Holy hack, wait 1 min before doing the next request
if self.conn_remaining == 0
sleep 60
end
key = "#{Api.api.class.to_s}:#{path}"
resp = if cache
APICache.get(key, :cache => 61) do
Expand Down Expand Up @@ -200,7 +204,8 @@ def submit(path, params = {}, klass=nil, format = :yaml, &block)
raise FormatError, [ctype, format] unless CONTENT_TYPE[format.to_s].include?(ctype)
if format == 'yaml' && resp['error']
raise APIError, resp['error']
end
end
self.conn_remaining = resp.headers['x-ratelimit-remaining'].to_i
resp
end

Expand Down