Skip to content

Fix race in RetryableTransport #230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}()
}
}
15 changes: 6 additions & 9 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
var client http.Client
if op.Client == nil {
client = *http.DefaultClient
} else {
client = *op.Client
}

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

op.Client = client

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

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

return res, err
}

Expand Down