Skip to content

Commit

Permalink
Fix race in RetryableTransport
Browse files Browse the repository at this point in the history
  • Loading branch information
vbrown608 committed May 7, 2020
1 parent 29814e0 commit 33e1129
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
28 changes: 28 additions & 0 deletions go/porcelain/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,32 @@ func TestOpenAPIClientWithWeirdResponse(t *testing.T) {
_, operationError := client.Operations.UploadDeployFile(params, authInfo)
require.Error(t, operationError)
require.Equal(t, "[PUT /deploys/{deploy_id}/files/{path}][408] uploadDeployFile default &{Code:408 Message:a message}", operationError.Error())

}

func TestConcurrentFileUpload(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.Header().Set("Content-Type", "application/json; charset=utf-8")
rw.WriteHeader(408)
rw.Write([]byte(`{ "foo": "bar", "message": "a message", "code": 408 }`))
}))
defer server.Close()

httpClient := http.DefaultClient
authInfo := runtime.ClientAuthInfoWriterFunc(func(r runtime.ClientRequest, _ strfmt.Registry) error {
r.SetHeaderParam("User-Agent", "buildbot")
r.SetHeaderParam("Authorization", "Bearer 1234")
return nil
})

hu, _ := url.Parse(server.URL)
tr := apiClient.NewWithClient(hu.Host, "/api/v1", []string{"http"}, httpClient)
client := NewRetryable(tr, strfmt.Default, 1)
for i := 0; i < 30; i++ {
go func() {
body := ioutil.NopCloser(bytes.NewReader([]byte("hello world")))
params := operations.NewUploadDeployFileParams().WithDeployID("1234").WithPath("foo/bar/biz").WithFileBody(body)
_, _ = client.Operations.UploadDeployFile(params, authInfo)
}()
}
}
13 changes: 5 additions & 8 deletions go/porcelain/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ func NewRetryableTransport(tr runtime.ClientTransport, attempts int) *RetryableT
}

func (t *RetryableTransport) Submit(op *runtime.ClientOperation) (interface{}, error) {
client := op.Client

if client == nil {
client = http.DefaultClient
client := &http.Client{}
if op.Client == nil {
*client = *http.DefaultClient
} else {
*client = *op.Client
}

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

op.Client = client

res, err := t.tr.Submit(op)

// restore original transport
op.Client.Transport = transport

return res, err
}

Expand Down

0 comments on commit 33e1129

Please sign in to comment.