Skip to content

Commit 102dc5b

Browse files
feat: add retry count field (#385)
1 parent 55ef270 commit 102dc5b

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed

go/plumbing/operations/upload_deploy_function_parameters.go

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/porcelain/deploy.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,8 @@ func (n *Netlify) uploadFile(ctx context.Context, d *models.Deploy, f *FileBundl
455455
}
456456
}
457457

458+
var retryCount int64 = 0
459+
458460
err := backoff.Retry(func() error {
459461
sharedErr.mutex.Lock()
460462

@@ -485,6 +487,11 @@ func (n *Netlify) uploadFile(ctx context.Context, d *models.Deploy, f *FileBundl
485487
}
486488
case functionUpload:
487489
params := operations.NewUploadDeployFunctionParams().WithDeployID(d.ID).WithName(f.Name).WithFileBody(f).WithRuntime(&f.Runtime)
490+
491+
if retryCount > 0 {
492+
params = params.WithXNfRetryCount(&retryCount)
493+
}
494+
488495
if timeout != 0 {
489496
params.SetTimeout(timeout)
490497
}
@@ -504,6 +511,9 @@ func (n *Netlify) uploadFile(ctx context.Context, d *models.Deploy, f *FileBundl
504511
sharedErr.mutex.Unlock()
505512
}
506513
}
514+
515+
retryCount++
516+
507517
return operationError
508518
}, b)
509519

go/porcelain/deploy_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,60 @@ func TestUploadFiles_Cancelation(t *testing.T) {
262262
require.ErrorIs(t, err, gocontext.Canceled)
263263
}
264264

265+
func TestUploadFunctions_RetryCountHeader(t *testing.T) {
266+
attempts := 0
267+
ctx, cancel := gocontext.WithCancel(gocontext.Background())
268+
t.Cleanup(cancel)
269+
270+
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
271+
defer func() {
272+
attempts++
273+
}()
274+
275+
rw.Header().Set("Content-Type", "application/json; charset=utf-8")
276+
277+
retryCount := req.Header.Get("X-Nf-Retry-Count")
278+
279+
if attempts == 0 {
280+
require.Empty(t, retryCount)
281+
} else {
282+
require.Equal(t, fmt.Sprint(attempts), retryCount)
283+
}
284+
285+
if attempts <= 2 {
286+
rw.WriteHeader(http.StatusInternalServerError)
287+
288+
return
289+
}
290+
291+
rw.WriteHeader(http.StatusOK)
292+
rw.Write([]byte(`{ "name": "foo" }`))
293+
}))
294+
defer server.Close()
295+
296+
hu, _ := url.Parse(server.URL)
297+
tr := apiClient.NewWithClient(hu.Host, "/api/v1", []string{"http"}, http.DefaultClient)
298+
client := NewRetryable(tr, strfmt.Default, 1)
299+
client.uploadLimit = 1
300+
apiCtx := context.WithAuthInfo(ctx, apiClient.BearerToken("token"))
301+
302+
dir, err := ioutil.TempDir("", "deploy")
303+
functionsPath := filepath.Join(dir, ".netlify", "functions")
304+
os.MkdirAll(functionsPath, os.ModePerm)
305+
require.NoError(t, err)
306+
defer os.RemoveAll(dir)
307+
require.NoError(t, ioutil.WriteFile(filepath.Join(functionsPath, "foo.js"), []byte("module.exports = () => {}"), 0644))
308+
309+
files, _, err := bundle(ctx, functionsPath, mockObserver{})
310+
require.NoError(t, err)
311+
d := &models.Deploy{}
312+
for _, bundle := range files.Files {
313+
d.RequiredFunctions = append(d.RequiredFunctions, bundle.Sum)
314+
}
315+
316+
require.NoError(t, client.uploadFiles(apiCtx, d, files, nil, functionUpload, time.Minute))
317+
}
318+
265319
func TestBundle(t *testing.T) {
266320
functions, schedules, err := bundle(gocontext.Background(), "../internal/data", mockObserver{})
267321

swagger.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,7 @@ paths:
12481248
type: string
12491249
format: binary
12501250
required: true
1251+
- $ref: '#/parameters/retryCount'
12511252
responses:
12521253
'200':
12531254
description: OK
@@ -3378,6 +3379,10 @@ parameters:
33783379
name: per_page
33793380
required: false
33803381
in: query
3382+
retryCount:
3383+
name: X-Nf-Retry-Count
3384+
type: integer
3385+
in: header
33813386
x-tagGroups:
33823387
- name: OAuth
33833388
tags:

0 commit comments

Comments
 (0)