Skip to content

Commit

Permalink
Limit retries on refresh tokens (#384)
Browse files Browse the repository at this point in the history
Sometimes YouTube gives a response we're not expecting when refreshing
an access token. We shouldn't keep attempting to refresh if this
happens.
  • Loading branch information
dgb committed Apr 10, 2020
1 parent 9af3590 commit e986f3e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/yt/request.rb
Expand Up @@ -196,7 +196,7 @@ def parse_response!
# - when the user has reached the quota for requests/second, and waiting
# for a couple of seconds might solve the connection issues.
def run_again?
refresh_token_and_retry? ||
refresh_token_and_retry? && sleep_and_retry?(3) ||
server_error? && sleep_and_retry?(3) ||
exceeded_quota? && sleep_and_retry?(3)
end
Expand Down
10 changes: 10 additions & 0 deletions spec/models/request_spec.rb
Expand Up @@ -67,6 +67,16 @@
it { expect{request.run}.to fail }
end

context 'an error code 401 with a refresh token' do
before { expect(Net::HTTP).to receive(:start).at_least(:once).and_return response }
let(:auth) { double(refreshed_access_token?: true, access_token: 'whatever') }
let(:request) { Yt::Request.new host: 'example.com', auth: auth }
let(:response_class) { Net::HTTPUnauthorized }

it { expect{request.run}.to fail }
end


context 'any other non-2XX error code' do
let(:response_class) { Net::HTTPNotFound }

Expand Down

0 comments on commit e986f3e

Please sign in to comment.