Skip to content

Directly handle rate limiting using client.rate_limit.resets_in. #967

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ PATH
octokit (~> 4.6)
rainbow (>= 2.2.1)
rake (>= 10.0)
retriable (~> 3.0)

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -95,7 +94,6 @@ GEM
rainbow (3.0.0)
rake (13.0.1)
regexp_parser (1.8.1)
retriable (3.1.2)
rexml (3.2.5)
rspec (3.9.0)
rspec-core (~> 3.9.0)
Expand Down
1 change: 0 additions & 1 deletion github_changelog_generator.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,4 @@ Gem::Specification.new do |spec|
spec.add_runtime_dependency("octokit", ["~> 4.6"])
spec.add_runtime_dependency "rainbow", ">= 2.2.1"
spec.add_runtime_dependency "rake", ">= 10.0"
spec.add_runtime_dependency("retriable", ["~> 3.0"])
end
41 changes: 13 additions & 28 deletions lib/github_changelog_generator/octo_fetcher.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require "tmpdir"
require "retriable"
require "set"
require "async"
require "async/barrier"
Expand Down Expand Up @@ -456,10 +455,21 @@ def iterate_pages(client, method, *arguments, parent: nil, **options)
#
# @return [Object] returns exactly the same, what you put in the block, but wrap it with begin-rescue block
# @param [Proc] block
def check_github_response(&block)
Retriable.retriable(retry_options, &block)
def check_github_response
yield
rescue MovedPermanentlyError => e
fail_with_message(e, "The repository has moved, update your configuration")
rescue Octokit::TooManyRequests => e
resets_in = client.rate_limit.resets_in
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 I learned! resets_in (a time) is even provided by Octokit, thanks for finding this out!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are most welcome!

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Helper.log.error("#{e.class} #{e.message}; sleeping for #{resets_in}s...")

if (task = Async::Task.current?)
task.sleep(resets_in)
else
sleep(resets_in)
end

retry
rescue Octokit::Forbidden => e
fail_with_message(e, "Exceeded retry limit")
rescue Octokit::Unauthorized => e
Expand All @@ -474,31 +484,6 @@ def fail_with_message(error, message)
sys_abort(message)
end

# Exponential backoff
def retry_options
{
on: [Octokit::Forbidden],
tries: MAX_FORBIDDEN_RETRIES,
base_interval: sleep_base_interval,
multiplier: 1.0,
rand_factor: 0.0,
on_retry: retry_callback
}
end

def sleep_base_interval
1.0
end

def retry_callback
proc do |exception, try, elapsed_time, next_interval|
Helper.log.warn("RETRY - #{exception.class}: '#{exception.message}'")
Helper.log.warn("#{try} tries in #{elapsed_time} seconds and #{next_interval} seconds until the next try")
Helper.log.warn GH_RATE_LIMIT_EXCEEDED_MSG
Helper.log.warn(client.rate_limit)
end
end

# @param [Object] msg
def sys_abort(msg)
abort(msg)
Expand Down
3 changes: 0 additions & 3 deletions spec/unit/octo_fetcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@

context "when raises Octokit::Forbidden" do
it "sleeps and retries and then aborts" do
retry_limit = GitHubChangelogGenerator::OctoFetcher::MAX_FORBIDDEN_RETRIES - 1
allow(fetcher).to receive(:sleep_base_interval).exactly(retry_limit).times.and_return(0)

expect(fetcher).to receive(:sys_abort).with("Exceeded retry limit")
fetcher.send(:check_github_response) { raise(Octokit::Forbidden) }
end
Expand Down