protondrive: retry a token refresh that failed with a server error or timeout
When the access token expired and the refresh request got a 5xx, 429 or 408, the operation failed instead of being retried, and a 408 even ended the session as if the refresh token had been rejected. Such failures are now retried like any other call. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
634a2423eb
commit
30cfcac416
2 files changed
+20
-3
No files matched your search
@@ -213,7 +213,9 @@ func (s *session) call(ctx context.Context, opts *rest.Opts, request, response a
|
||||
return resp, err
|
||||
}
|
||||
if refreshErr := s.refresh(ctx, creds.AccessToken); refreshErr != nil {
|
||||
return resp, refreshErr
|
||||
// Not the 401's response, so that a retry is decided by the
|
||||
// refresh's error.
|
||||
return nil, refreshErr
|
||||
}
|
||||
if opts.Body != nil {
|
||||
seeker, ok := opts.Body.(io.Seeker)
|
||||
@@ -351,9 +353,10 @@ func (s *session) refresh(ctx context.Context, rejected string) error {
|
||||
}, &out, &cur)
|
||||
if err != nil {
|
||||
// As the SDK does, treat a client error other than rate
|
||||
// limiting as the refresh token being rejected.
|
||||
// limiting, or a timeout, as the refresh token being rejected.
|
||||
var apiErr *api.Error
|
||||
if errors.As(err, &apiErr) && apiErr.Status >= 400 && apiErr.Status < 500 && apiErr.Status != http.StatusTooManyRequests {
|
||||
if errors.As(err, &apiErr) && apiErr.Status >= 400 && apiErr.Status < 500 &&
|
||||
apiErr.Status != http.StatusTooManyRequests && apiErr.Status != http.StatusRequestTimeout {
|
||||
// Another user of the store may have spent the same refresh
|
||||
// token and not saved its replacement yet.
|
||||
if stored, ok := s.waitForStoredRefresh(ctx, cur.RefreshToken); ok {
|
||||
|
||||
@@ -580,3 +580,17 @@ func TestRefreshDuringLogout(t *testing.T) {
|
||||
assert.Equal(t, credentials{}, store.Load(), "tokens saved after signing out")
|
||||
assert.Equal(t, credentials{}, s.current())
|
||||
}
|
||||
|
||||
func TestRefreshTransientErrorRetried(t *testing.T) {
|
||||
for _, status := range []int{http.StatusServiceUnavailable, http.StatusRequestTimeout} {
|
||||
t.Run(http.StatusText(status), func(t *testing.T) {
|
||||
c, srv, _ := newTestClient(t)
|
||||
ctx := context.Background()
|
||||
srv.ExpireAccessToken(c.s.current().UID)
|
||||
srv.Inject(protontest.Fault{Method: "POST", Pattern: "/auth/v4/refresh", Status: status, Times: 1})
|
||||
_, _, err := c.about(ctx)
|
||||
require.NoError(t, err, "a %d on the refresh wasn't retried", status)
|
||||
assert.Equal(t, 2, srv.Requests()["POST /auth/v4/refresh"])
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user