Skip to content

Commit

Permalink
fix(api): for retry on timeout with service (#5784)
Browse files Browse the repository at this point in the history
* fix(api): for retry on timeout with service

Signed-off-by: Yvonnick Esnault <[email protected]>
  • Loading branch information
yesnault authored Mar 31, 2021
1 parent e2e9e35 commit 4d4a2d8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
12 changes: 8 additions & 4 deletions engine/api/services/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ func (s *defaultServiceClient) DoJSONRequest(ctx context.Context, method, path s
func doJSONRequest(ctx context.Context, srvs []sdk.Service, method, path string, in interface{}, out interface{}, mods ...cdsclient.RequestModifier) (http.Header, int, error) {
var lastErr = sdk.WithStack(errors.New("unable to call service: service not found"))
var lastCode int
for attempt := 0; attempt < 5; attempt++ {
var attempts int64
for attempts = 0; attempts < 5; attempts++ {
for i := range srvs {
srv := &srvs[i]
headers, code, err := _doJSONRequest(ctx, srv, method, path, in, out, mods...)
Expand All @@ -162,7 +163,7 @@ func doJSONRequest(ctx context.Context, srvs []sdk.Service, method, path string,
}
}

log.Error(ctx, "unable to call service: maximum attempt exceed : %+v", lastErr)
log.Error(ctx, "unable to call service: maximum attempt exceed: %+v lastCode:%d attempts:%d", lastErr, lastCode, attempts)
return nil, lastCode, sdk.WithStack(lastErr)
}

Expand Down Expand Up @@ -305,7 +306,10 @@ func doRequestFromURL(ctx context.Context, method string, callURL *url.URL, read
//Do the request
resp, err := HTTPClient.Do(req)
if err != nil {
return nil, nil, 0, sdk.WrapError(err, "request failed")
if resp != nil && resp.StatusCode > 0 {
return nil, nil, resp.StatusCode, sdk.WrapError(err, "request failed with resp status code: %d", resp.StatusCode)
}
return nil, nil, 500, sdk.WrapError(err, "request failed - use code 500")
}
defer resp.Body.Close()

Expand All @@ -327,5 +331,5 @@ func doRequestFromURL(ctx context.Context, method string, callURL *url.URL, read
return nil, resp.Header, resp.StatusCode, cdserr
}

return nil, resp.Header, resp.StatusCode, sdk.WithStack(fmt.Errorf("request failed"))
return nil, resp.Header, resp.StatusCode, sdk.WithStack(fmt.Errorf("request failed with status code: %d", resp.StatusCode))
}
1 change: 1 addition & 0 deletions engine/vcs/bitbucketserver/client_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func (b *bitbucketClient) Branches(ctx context.Context, fullname string) ([]sdk.

return branches, nil
}

func (b *bitbucketClient) Branch(ctx context.Context, fullname string, filter string) (*sdk.VCSBranch, error) {
t := strings.Split(fullname, "/")
if len(t) != 2 {
Expand Down

0 comments on commit 4d4a2d8

Please sign in to comment.