Skip to content

Commit 6150907

Browse files
authored
fix: don't double-count file uploads in worker pool (#313)
* fix: don't double-count file uploads in worker pool * Remove or consolidate auth setup in tests
1 parent e46713e commit 6150907

File tree

4 files changed

+45
-33
lines changed

4 files changed

+45
-33
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ require (
1616
github.com/pkg/errors v0.9.1
1717
github.com/rsc/goversion v1.2.0
1818
github.com/sirupsen/logrus v1.6.0
19-
github.com/stretchr/testify v1.6.1
19+
github.com/stretchr/testify v1.7.0
2020
golang.org/x/net v0.0.0-20201224014010-6772e930b67b // indirect
2121
)

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ github.com/spf13/viper v1.6.2 h1:7aKfF+e8/k68gda3LOjo5RxiUqddoFxVq4BKBPrxk5E=
401401
github.com/spf13/viper v1.6.2/go.mod h1:t3iDnF5Jlj76alVNuyFBk5oUMCvsrkbvZK0WQdfDi5k=
402402
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
403403
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
404+
github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48=
404405
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
405406
github.com/stretchr/testify v1.1.4/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
406407
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
@@ -410,6 +411,8 @@ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJy
410411
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
411412
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
412413
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
414+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
415+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
413416
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
414417
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
415418
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=

go/porcelain/deploy.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -379,18 +379,18 @@ func (n *Netlify) uploadFiles(ctx context.Context, d *models.Deploy, files *depl
379379
}
380380
}
381381

382-
context.GetLogger(ctx).Infof("Uploading %v files", count)
382+
log := context.GetLogger(ctx)
383+
log.Infof("Uploading %v files", count)
383384

384385
for _, sha := range required {
385386
if files, exist := files.Hashed[sha]; exist {
386387
for _, file := range files {
387388
select {
388389
case sem <- 1:
389-
sem <- 1
390390
wg.Add(1)
391-
392391
go n.uploadFile(ctx, d, file, observer, t, timeout, wg, sem, sharedErr)
393392
case <-ctx.Done():
393+
log.Info("Context terminated, aborting file upload")
394394
return errors.Wrap(ctx.Err(), "aborted file upload early")
395395
}
396396
}

go/porcelain/deploy_test.go

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"github.com/netlify/open-api/v2/go/plumbing/operations"
1818
"github.com/netlify/open-api/v2/go/porcelain/context"
1919

20-
"github.com/go-openapi/runtime"
2120
apiClient "github.com/go-openapi/runtime/client"
2221
"github.com/go-openapi/strfmt"
2322
"github.com/stretchr/testify/assert"
@@ -93,23 +92,15 @@ func TestOpenAPIClientWithWeirdResponse(t *testing.T) {
9392
}))
9493
defer server.Close()
9594

96-
httpClient := http.DefaultClient
97-
authInfo := runtime.ClientAuthInfoWriterFunc(func(r runtime.ClientRequest, _ strfmt.Registry) error {
98-
r.SetHeaderParam("User-Agent", "buildbot")
99-
r.SetHeaderParam("Authorization", "Bearer 1234")
100-
return nil
101-
})
102-
10395
hu, _ := url.Parse(server.URL)
104-
tr := apiClient.NewWithClient(hu.Host, "/api/v1", []string{"http"}, httpClient)
96+
tr := apiClient.NewWithClient(hu.Host, "/api/v1", []string{"http"}, http.DefaultClient)
10597
client := NewRetryable(tr, strfmt.Default, 1)
10698

10799
body := ioutil.NopCloser(bytes.NewReader([]byte("hello world")))
108100
params := operations.NewUploadDeployFileParams().WithDeployID("1234").WithPath("foo/bar/biz").WithFileBody(body)
109-
_, operationError := client.Operations.UploadDeployFile(params, authInfo)
101+
_, operationError := client.Operations.UploadDeployFile(params, nil)
110102
require.Error(t, operationError)
111103
require.Equal(t, "[PUT /deploys/{deploy_id}/files/{path}][408] uploadDeployFile default &{Code:408 Message:a message}", operationError.Error())
112-
113104
}
114105

115106
func TestConcurrentFileUpload(t *testing.T) {
@@ -120,21 +111,14 @@ func TestConcurrentFileUpload(t *testing.T) {
120111
}))
121112
defer server.Close()
122113

123-
httpClient := http.DefaultClient
124-
authInfo := runtime.ClientAuthInfoWriterFunc(func(r runtime.ClientRequest, _ strfmt.Registry) error {
125-
r.SetHeaderParam("User-Agent", "buildbot")
126-
r.SetHeaderParam("Authorization", "Bearer 1234")
127-
return nil
128-
})
129-
130114
hu, _ := url.Parse(server.URL)
131-
tr := apiClient.NewWithClient(hu.Host, "/api/v1", []string{"http"}, httpClient)
115+
tr := apiClient.NewWithClient(hu.Host, "/api/v1", []string{"http"}, http.DefaultClient)
132116
client := NewRetryable(tr, strfmt.Default, 1)
133117
for i := 0; i < 30; i++ {
134118
go func() {
135119
body := ioutil.NopCloser(bytes.NewReader([]byte("hello world")))
136120
params := operations.NewUploadDeployFileParams().WithDeployID("1234").WithPath("foo/bar/biz").WithFileBody(body)
137-
_, _ = client.Operations.UploadDeployFile(params, authInfo)
121+
_, _ = client.Operations.UploadDeployFile(params, nil)
138122
}()
139123
}
140124
}
@@ -146,18 +130,11 @@ func TestWaitUntilDeployLive_Timeout(t *testing.T) {
146130
}))
147131
defer server.Close()
148132

149-
httpClient := http.DefaultClient
150-
authInfo := runtime.ClientAuthInfoWriterFunc(func(r runtime.ClientRequest, _ strfmt.Registry) error {
151-
r.SetHeaderParam("User-Agent", "buildbot")
152-
r.SetHeaderParam("Authorization", "Bearer 1234")
153-
return nil
154-
})
155-
156133
hu, _ := url.Parse(server.URL)
157-
tr := apiClient.NewWithClient(hu.Host, "/api/v1", []string{"http"}, httpClient)
134+
tr := apiClient.NewWithClient(hu.Host, "/api/v1", []string{"http"}, http.DefaultClient)
158135
client := NewRetryable(tr, strfmt.Default, 1)
159136

160-
ctx := context.WithAuthInfo(gocontext.Background(), authInfo)
137+
ctx := context.WithAuthInfo(gocontext.Background(), apiClient.BearerToken("token"))
161138
ctx, _ = gocontext.WithTimeout(ctx, 50*time.Millisecond)
162139
_, err := client.WaitUntilDeployLive(ctx, &models.Deploy{})
163140
assert.Error(t, err)
@@ -191,6 +168,38 @@ func TestWalk_IgnoreNodeModulesInRoot(t *testing.T) {
191168
assert.NotNil(t, files.Files["more/node_modules/inner-package"])
192169
}
193170

171+
func TestUploadFiles_Cancelation(t *testing.T) {
172+
ctx, cancel := gocontext.WithCancel(gocontext.Background())
173+
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
174+
cancel() // Cancel deploy as soon as first file upload is attempted.
175+
rw.Header().Set("Content-Type", "application/json; charset=utf-8")
176+
rw.Write([]byte(`{ "state": "canceled" }`))
177+
}))
178+
defer server.Close()
179+
180+
hu, _ := url.Parse(server.URL)
181+
tr := apiClient.NewWithClient(hu.Host, "/api/v1", []string{"http"}, http.DefaultClient)
182+
client := NewRetryable(tr, strfmt.Default, 1)
183+
client.uploadLimit = 1
184+
ctx = context.WithAuthInfo(ctx, apiClient.BearerToken("token"))
185+
186+
// Create some files to deploy
187+
dir, err := ioutil.TempDir("", "deploy")
188+
require.NoError(t, err)
189+
defer os.RemoveAll(dir)
190+
require.NoError(t, ioutil.WriteFile(filepath.Join(dir, "foo.html"), []byte("Hello"), 0644))
191+
require.NoError(t, ioutil.WriteFile(filepath.Join(dir, "bar.html"), []byte("World"), 0644))
192+
193+
files, err := walk(dir, nil, false, false)
194+
require.NoError(t, err)
195+
d := &models.Deploy{}
196+
for _, bundle := range files.Files {
197+
d.Required = append(d.Required, bundle.Sum)
198+
}
199+
err = client.uploadFiles(ctx, d, files, nil, fileUpload, time.Minute)
200+
require.ErrorIs(t, err, gocontext.Canceled)
201+
}
202+
194203
func TestReadZipRuntime(t *testing.T) {
195204
runtime, err := readZipRuntime("../internal/data/hello-rs-function-test.zip")
196205
if err != nil {

0 commit comments

Comments
 (0)