Skip to content

Commit 33e1129

Browse files
committed
Fix race in RetryableTransport
1 parent 29814e0 commit 33e1129

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

go/porcelain/deploy_test.go

Lines changed: 28 additions & 0 deletions
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

Lines changed: 5 additions & 8 deletions
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+
client := &http.Client{}
35+
if op.Client == nil {
36+
*client = *http.DefaultClient
37+
} else {
38+
*client = *op.Client
3839
}
3940

4041
transport := client.Transport
@@ -47,12 +48,8 @@ func (t *RetryableTransport) Submit(op *runtime.ClientOperation) (interface{}, e
4748
}
4849

4950
op.Client = client
50-
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)