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

fix(client): make retry loop stop immediately when context is cancelled #386

Merged
merged 1 commit into from
Mar 17, 2024
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
1 change: 1 addition & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ func (rq *defaultRequester) retry(ctx context.Context, method, urlpath string, q
case <-retry.C:
continue
case <-timeout:
case <-ctx.Done():
}
break
}
Expand Down
16 changes: 16 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package client_test

import (
"context"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -107,6 +108,21 @@ func (cs *clientSuite) TestClientDoReportsErrors(c *C) {
}
}

func (cs *clientSuite) TestContextCancellation(c *C) {
cs.err = errors.New("ouchie")
ctx, cancel := context.WithCancel(context.Background())
cancel() // cancel it right away
_, err := cs.cli.Requester().Do(ctx, &client.RequestOptions{
Type: client.SyncRequest,
Method: "GET",
Path: "/",
})
c.Check(err, ErrorMatches, "cannot communicate with server: ouchie")

// This would be 10 if context wasn't respected, due to timeout
c.Assert(cs.doCalls, Equals, 1)
}

func (cs *clientSuite) TestClientWorks(c *C) {
var v []int
cs.rsp = `[1,2]`
Expand Down
Loading