Skip to content

Commit 4d4a2d8

Browse files
authored
fix(api): for retry on timeout with service (#5784)
* fix(api): for retry on timeout with service Signed-off-by: Yvonnick Esnault <[email protected]>
1 parent e2e9e35 commit 4d4a2d8

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

engine/api/services/http.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ func (s *defaultServiceClient) DoJSONRequest(ctx context.Context, method, path s
146146
func doJSONRequest(ctx context.Context, srvs []sdk.Service, method, path string, in interface{}, out interface{}, mods ...cdsclient.RequestModifier) (http.Header, int, error) {
147147
var lastErr = sdk.WithStack(errors.New("unable to call service: service not found"))
148148
var lastCode int
149-
for attempt := 0; attempt < 5; attempt++ {
149+
var attempts int64
150+
for attempts = 0; attempts < 5; attempts++ {
150151
for i := range srvs {
151152
srv := &srvs[i]
152153
headers, code, err := _doJSONRequest(ctx, srv, method, path, in, out, mods...)
@@ -162,7 +163,7 @@ func doJSONRequest(ctx context.Context, srvs []sdk.Service, method, path string,
162163
}
163164
}
164165

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

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

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

330-
return nil, resp.Header, resp.StatusCode, sdk.WithStack(fmt.Errorf("request failed"))
334+
return nil, resp.Header, resp.StatusCode, sdk.WithStack(fmt.Errorf("request failed with status code: %d", resp.StatusCode))
331335
}

engine/vcs/bitbucketserver/client_branch.go

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func (b *bitbucketClient) Branches(ctx context.Context, fullname string) ([]sdk.
5757

5858
return branches, nil
5959
}
60+
6061
func (b *bitbucketClient) Branch(ctx context.Context, fullname string, filter string) (*sdk.VCSBranch, error) {
6162
t := strings.Split(fullname, "/")
6263
if len(t) != 2 {

0 commit comments

Comments
 (0)