Skip to content

Commit d2e4528

Browse files
authored
fix: do not retry on context done (#156)
1 parent 8653a54 commit d2e4528

4 files changed

Lines changed: 34 additions & 11 deletions

File tree

.github/workflows/typos.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: Typos
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
8+
jobs:
9+
check:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v4
13+
- name: Check spelling
14+
env:
15+
CLICOLOR: 1
16+
uses: crate-ci/typos@master

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.7.1
1+
0.7.2

apierrors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
var (
2525
ProvisionWarehouseTimeout = "ProvisionWarehouseTimeout"
2626

27-
ErrDoRequest = errors.New("DoReqeustFailed")
27+
ErrDoRequest = errors.New("DoRequestFailed")
2828
ErrReadResponse = errors.New("ReadResponseFailed")
2929
)
3030

client.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,11 @@ func (c *APIClient) doRequest(ctx context.Context, method, path string, req inte
239239
if err != nil {
240240
return errors.Wrap(err, "failed to create http request")
241241
}
242+
242243
httpReq = httpReq.WithContext(ctx)
243244

244-
maxRetries := 2
245-
for i := 1; i <= maxRetries; i++ {
245+
authRetryLimit := 2
246+
for i := 1; i <= authRetryLimit; i++ {
246247
headers, err := c.makeHeaders(ctx)
247248
if needSticky && len(c.NodeID) != 0 {
248249
headers.Set(DatabendQueryStickyNode, c.NodeID)
@@ -261,6 +262,12 @@ func (c *APIClient) doRequest(ctx context.Context, method, path string, req inte
261262
httpReq.Host = c.host
262263
}
263264

265+
select {
266+
case <-ctx.Done():
267+
return errors.Wrap(ctx.Err(), "context done")
268+
default:
269+
}
270+
264271
httpResp, err := c.cli.Do(httpReq)
265272
if err != nil {
266273
return errors.Wrap(ErrDoRequest, err.Error())
@@ -290,7 +297,7 @@ func (c *APIClient) doRequest(ctx context.Context, method, path string, req inte
290297
}
291298

292299
if httpResp.StatusCode == http.StatusUnauthorized {
293-
if c.authMethod() == AuthMethodAccessToken && i < maxRetries {
300+
if c.authMethod() == AuthMethodAccessToken && i < authRetryLimit {
294301
// retry with a rotated access token
295302
_, _ = c.accessTokenLoader.LoadAccessToken(context.Background(), true)
296303
continue
@@ -306,7 +313,7 @@ func (c *APIClient) doRequest(ctx context.Context, method, path string, req inte
306313

307314
return unmarshalErr
308315
}
309-
return errors.Errorf("failed to do request after %d retries", maxRetries)
316+
return errors.Errorf("failed to do request after %d retries", authRetryLimit)
310317
}
311318

312319
func (c *APIClient) trackStats(resp *QueryResponse) {
@@ -385,7 +392,7 @@ var databendInsecureTransport = &http.Transport{
385392
}).DialContext,
386393
}
387394

388-
func (c *APIClient) getPagenationConfig() *PaginationConfig {
395+
func (c *APIClient) getPaginationConfig() *PaginationConfig {
389396
if c.MaxRowsPerPage == 0 && c.MaxRowsInBuffer == 0 && c.WaitTimeSeconds == 0 {
390397
return nil
391398
}
@@ -425,7 +432,7 @@ func (c *APIClient) PollUntilQueryEnd(ctx context.Context, resp *QueryResponse)
425432
if errors.Is(err, context.Canceled) {
426433
// context might be canceled due to timeout or canceled. if it's canceled, we need call
427434
// the kill url to tell the backend it's killed.
428-
fmt.Printf("query canceled, kill query:%s", resp.ID)
435+
fmt.Printf("query canceled, kill query: %s", resp.ID)
429436
_ = c.KillQuery(context.Background(), resp)
430437
}
431438
return nil, err
@@ -478,7 +485,7 @@ func (c *APIClient) doRetry(f retry.RetryableFunc, t RequestType) error {
478485
if err == nil {
479486
return false
480487
}
481-
if errors.Is(err, context.Canceled) {
488+
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
482489
return false
483490
}
484491
if errors.Is(err, ErrDoRequest) || errors.Is(err, ErrReadResponse) || IsProxyErr(err) {
@@ -539,7 +546,7 @@ func (c *APIClient) StartQuery(ctx context.Context, query string, args []driver.
539546
}
540547
request := QueryRequest{
541548
SQL: q,
542-
Pagination: c.getPagenationConfig(),
549+
Pagination: c.getPaginationConfig(),
543550
Session: c.getSessionStateRaw(),
544551
}
545552
return c.startQueryRequest(ctx, &request)
@@ -601,7 +608,7 @@ func (c *APIClient) InsertWithStage(ctx context.Context, sql string, stage *Stag
601608
}
602609
request := QueryRequest{
603610
SQL: sql,
604-
Pagination: c.getPagenationConfig(),
611+
Pagination: c.getPaginationConfig(),
605612
Session: c.getSessionStateRaw(),
606613
StageAttachment: &StageAttachmentConfig{
607614
Location: stage.String(),

0 commit comments

Comments
 (0)