Skip to content

Disable H2 for retryable requests. #72

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 1 commit into from
Jan 4, 2018
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
25 changes: 24 additions & 1 deletion go/porcelain/http/http.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package http

import (
"crypto/tls"
"net"
"net/http"
"strconv"
"time"
Expand All @@ -9,6 +11,8 @@ import (
"github.com/go-openapi/runtime"
)

var DefaultTransport = httpTransport()

type RetryableTransport struct {
tr runtime.ClientTransport
attempts int
Expand All @@ -35,7 +39,7 @@ func (t *RetryableTransport) Submit(op *runtime.ClientOperation) (interface{}, e

transport := client.Transport
if transport == nil {
transport = http.DefaultTransport
transport = DefaultTransport
}
client.Transport = &retryableRoundTripper{
tr: transport,
Expand Down Expand Up @@ -95,3 +99,22 @@ func delayWithRateLimit(resp *http.Response, cancel <-chan struct{}) bool {
return false
}
}

func httpTransport() *http.Transport {
protoUpgrade := map[string]func(string, *tls.Conn) http.RoundTripper{
"ignore-h2": func(string, *tls.Conn) http.RoundTripper { return nil },
}

return &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSNextProto: protoUpgrade,
}
}
44 changes: 40 additions & 4 deletions go/porcelain/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ func TestRetryableTransport(t *testing.T) {
reset := fmt.Sprintf("%d", time.Now().Add(1*time.Second).Unix())
rw.Header().Set("X-RateLimit-Reset", reset)
rw.WriteHeader(http.StatusTooManyRequests)
_, _ = rw.Write([]byte("rate limited"))
rw.Write([]byte("rate limited"))
} else {
rw.WriteHeader(http.StatusOK)
_, _ = rw.Write([]byte("ok"))
rw.Write([]byte("ok"))
}
}))
defer server.Close()
Expand Down Expand Up @@ -72,7 +72,7 @@ func TestRetryableTransportExceedsMaxAttempts(t *testing.T) {
reset := fmt.Sprintf("%d", time.Now().Add(1*time.Second).Unix())
rw.Header().Set("X-RateLimit-Reset", reset)
rw.WriteHeader(http.StatusTooManyRequests)
_, _ = rw.Write([]byte("rate limited"))
rw.Write([]byte("rate limited"))
}))
defer server.Close()

Expand Down Expand Up @@ -111,7 +111,7 @@ func TestRetryableWithDifferentError(t *testing.T) {
attempts++

rw.WriteHeader(http.StatusNotFound)
_, _ = rw.Write([]byte("not found"))
rw.Write([]byte("not found"))
}))
defer server.Close()

Expand Down Expand Up @@ -142,3 +142,39 @@ func TestRetryableWithDifferentError(t *testing.T) {
require.Error(t, err)
require.Equal(t, 1, attempts)
}

func TestRetryableTransport_POST(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusCreated)
rw.Write([]byte("test result"))
}))
defer server.Close()

rwrtr := runtime.ClientRequestWriterFunc(func(req runtime.ClientRequest, _ strfmt.Registry) error {
return req.SetBodyParam("test result")
})

hu, _ := url.Parse(server.URL)
rt := NewRetryableTransport(httptransport.New(hu.Host, "/", []string{"http"}), 2)

result, err := rt.Submit(&runtime.ClientOperation{
ID: "createSite",
Method: "POST",
PathPattern: "/",
Params: rwrtr,
Reader: runtime.ClientResponseReaderFunc(func(response runtime.ClientResponse, consumer runtime.Consumer) (interface{}, error) {
if response.Code() == 201 {
var result string
if err := consumer.Consume(response.Body(), &result); err != nil {
return nil, err
}
return result, nil
}
return nil, errors.New("Generic error")
}),
})

require.NoError(t, err)
actual := result.(string)
require.EqualValues(t, "test result", actual)
}