Skip to content

Commit

Permalink
Add Octokit::Middleware::Retry
Browse files Browse the repository at this point in the history
Octokit::Middleware::Retry allows incorporating the retry logic without
relying on the default middleware stack.
  • Loading branch information
akihikodaki committed Oct 19, 2024
1 parent 2efe1b8 commit c892eb3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ traffic:

```ruby
stack = Faraday::RackBuilder.new do |builder|
builder.use Faraday::Retry::Middleware, exceptions: Faraday::Request::Retry::DEFAULT_EXCEPTIONS + [Octokit::ServerError] # or Faraday::Request::Retry for Faraday < 2.0
builder.use Octokit::Middleware::Retry
builder.use Octokit::Middleware::FollowRedirects
builder.use Octokit::Response::RaiseError
builder.use Octokit::Response::FeedParser
Expand Down
6 changes: 6 additions & 0 deletions lib/octokit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ def method_missing(method_name, *args, &block)
super
end
end

module Middleware
# In Faraday 2.x, Faraday::Request::Retry was moved to a separate gem
# so we use it only when requested.
autoload :Retry, 'octokit/middleware/retry'
end
end

Octokit.setup
19 changes: 19 additions & 0 deletions lib/octokit/middleware/retry.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module Octokit
module Middleware
base = if defined?(Faraday::Request::Retry)
Faraday::Request::Retry
else
require 'faraday/retry'
Faraday::Retry::Middleware
end

# Public: Retries each request a limited number of times.
class Retry < base
def initialize(app, **kwargs)
super(app, **kwargs, exceptions: DEFAULT_EXCEPTIONS + [Octokit::ServerError])
end
end
end
end
21 changes: 21 additions & 0 deletions spec/octokit/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,27 @@
end
end

describe 'retry' do
it 'retries for 504 response' do
client = oauth_client
client.middleware.insert Octokit::Response::RaiseError, Octokit::Middleware::Retry

requested = false

request = stub_get('/foo').to_return do
if requested
{ status: 200 }
else
requested = true
{ status: 504 }
end
end

client.get('/foo')
assert_requested request, times: 2
end
end

describe 'redirect handling' do
it 'follows redirect for 301 response' do
client = oauth_client
Expand Down

0 comments on commit c892eb3

Please sign in to comment.