Skip to content

Commit 2954343

Browse files
authored
Merge pull request #230 from netlify/fix-op-client-race
Fix race in RetryableTransport
2 parents d822ba7 + c065ece commit 2954343

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

go/porcelain/deploy_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,32 @@ func TestOpenAPIClientWithWeirdResponse(t *testing.T) {
101101
_, operationError := client.Operations.UploadDeployFile(params, authInfo)
102102
require.Error(t, operationError)
103103
require.Equal(t, "[PUT /deploys/{deploy_id}/files/{path}][408] uploadDeployFile default &{Code:408 Message:a message}", operationError.Error())
104+
105+
}
106+
107+
func TestConcurrentFileUpload(t *testing.T) {
108+
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
109+
rw.Header().Set("Content-Type", "application/json; charset=utf-8")
110+
rw.WriteHeader(408)
111+
rw.Write([]byte(`{ "foo": "bar", "message": "a message", "code": 408 }`))
112+
}))
113+
defer server.Close()
114+
115+
httpClient := http.DefaultClient
116+
authInfo := runtime.ClientAuthInfoWriterFunc(func(r runtime.ClientRequest, _ strfmt.Registry) error {
117+
r.SetHeaderParam("User-Agent", "buildbot")
118+
r.SetHeaderParam("Authorization", "Bearer 1234")
119+
return nil
120+
})
121+
122+
hu, _ := url.Parse(server.URL)
123+
tr := apiClient.NewWithClient(hu.Host, "/api/v1", []string{"http"}, httpClient)
124+
client := NewRetryable(tr, strfmt.Default, 1)
125+
for i := 0; i < 30; i++ {
126+
go func() {
127+
body := ioutil.NopCloser(bytes.NewReader([]byte("hello world")))
128+
params := operations.NewUploadDeployFileParams().WithDeployID("1234").WithPath("foo/bar/biz").WithFileBody(body)
129+
_, _ = client.Operations.UploadDeployFile(params, authInfo)
130+
}()
131+
}
104132
}

go/porcelain/http/http.go

+6-9
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ func NewRetryableTransport(tr runtime.ClientTransport, attempts int) *RetryableT
3131
}
3232

3333
func (t *RetryableTransport) Submit(op *runtime.ClientOperation) (interface{}, error) {
34-
client := op.Client
35-
36-
if client == nil {
37-
client = http.DefaultClient
34+
var client http.Client
35+
if op.Client == nil {
36+
client = *http.DefaultClient
37+
} else {
38+
client = *op.Client
3839
}
3940

4041
transport := client.Transport
@@ -46,13 +47,9 @@ func (t *RetryableTransport) Submit(op *runtime.ClientOperation) (interface{}, e
4647
attempts: t.attempts,
4748
}
4849

49-
op.Client = client
50-
50+
op.Client = &client
5151
res, err := t.tr.Submit(op)
5252

53-
// restore original transport
54-
op.Client.Transport = transport
55-
5653
return res, err
5754
}
5855

0 commit comments

Comments
 (0)