Skip to content

Commit 1b486ee

Browse files
authored
fix: retry for verify on 502 (#164)
1 parent d692f13 commit 1b486ee

File tree

3 files changed

+43
-21
lines changed

3 files changed

+43
-21
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.8.0
1+
0.8.1

apierrors.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ func IsAuthFailed(err error) bool {
7979
return errors.As(err, &apiErr) && apiErr.StatusCode == 401
8080
}
8181

82+
func isBadGateway(err error) bool {
83+
var apiErr APIError
84+
return errors.As(err, &apiErr) && apiErr.StatusCode == 502
85+
}
86+
8287
func RespBody(err error) APIErrorResponseBody {
8388
var apiErr APIError
8489
if !errors.As(err, &apiErr) {

client.go

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -242,26 +242,26 @@ func (c *APIClient) doRequest(ctx context.Context, method, path string, req inte
242242

243243
httpReq = httpReq.WithContext(ctx)
244244

245-
authRetryLimit := 2
246-
for i := 1; i <= authRetryLimit; i++ {
247-
headers, err := c.makeHeaders(ctx)
248-
if needSticky && len(c.NodeID) != 0 {
249-
headers.Set(DatabendQueryStickyNode, c.NodeID)
250-
}
251-
if err != nil {
252-
return errors.Wrap(err, "failed to make request headers")
253-
}
254-
if method == "GET" && len(c.NodeID) != 0 {
255-
headers.Set(DatabendQueryIDNode, c.NodeID)
256-
}
257-
headers.Set(contentType, jsonContentType)
258-
headers.Set(accept, jsonContentType)
259-
httpReq.Header = headers
245+
headers, err := c.makeHeaders(ctx)
246+
if needSticky && len(c.NodeID) != 0 {
247+
headers.Set(DatabendQueryStickyNode, c.NodeID)
248+
}
249+
if err != nil {
250+
return errors.Wrap(err, "failed to make request headers")
251+
}
252+
if method == "GET" && len(c.NodeID) != 0 {
253+
headers.Set(DatabendQueryIDNode, c.NodeID)
254+
}
255+
headers.Set(contentType, jsonContentType)
256+
headers.Set(accept, jsonContentType)
257+
httpReq.Header = headers
260258

261-
if len(c.host) > 0 {
262-
httpReq.Host = c.host
263-
}
259+
if len(c.host) > 0 {
260+
httpReq.Host = c.host
261+
}
264262

263+
authRetryLimit := 2
264+
for i := 1; i <= authRetryLimit; i++ {
265265
select {
266266
case <-ctx.Done():
267267
return errors.Wrap(ctx.Err(), "context done")
@@ -303,8 +303,10 @@ func (c *APIClient) doRequest(ctx context.Context, method, path string, req inte
303303
continue
304304
}
305305
return NewAPIError("authorization failed", httpResp.StatusCode, httpRespBody)
306-
} else if httpResp.StatusCode >= 500 {
306+
} else if httpResp.StatusCode > 500 {
307307
return NewAPIError("please retry again later", httpResp.StatusCode, httpRespBody)
308+
} else if httpResp.StatusCode == 500 {
309+
return NewAPIError("internal server error", httpResp.StatusCode, httpRespBody)
308310
} else if httpResp.StatusCode >= 400 {
309311
return NewAPIError("please check your arguments", httpResp.StatusCode, httpRespBody)
310312
} else if httpResp.StatusCode != 200 {
@@ -763,10 +765,25 @@ func (c *APIClient) UploadToStageByAPI(ctx context.Context, stage *StageLocation
763765

764766
func (c *APIClient) Verify(ctx context.Context) error {
765767
var response VerifyResponse
766-
err := c.doRequest(ctx, "POST", "/v1/verify", nil, false, &response, nil)
768+
var err error
769+
770+
maxRetries := 2
771+
for attempt := 0; attempt <= maxRetries; attempt++ {
772+
err = c.doRequest(ctx, "POST", "/v1/verify", nil, false, &response, nil)
773+
if err == nil {
774+
break
775+
}
776+
if attempt < maxRetries && isBadGateway(err) {
777+
time.Sleep(time.Millisecond * 200)
778+
continue
779+
}
780+
break
781+
}
782+
767783
if err != nil {
768784
return errors.Wrap(err, "verify failed")
769785
}
786+
770787
if c.tenant != "" && response.Tenant != c.tenant {
771788
return errors.Errorf("verify tenant mismatch, expected: %s, got: %s", c.tenant, response.Tenant)
772789
}

0 commit comments

Comments
 (0)