diff --git a/CHANGELOG.md b/CHANGELOG.md index d50ee81..e53cc45 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,13 @@ follows [semantic versioning](https://semver.org). ## Unreleased +### Fixed + +- A request that fails because the HTTPS proxy cannot be reached is now + retried like any other failure to connect, as the package documentation + says. Before, a call that is not repeated once it may have reached the API, + such as `CreateKey`, failed after one attempt. + ## v0.1.0 The first release, written against version 0.15.0 of the rypt API. diff --git a/request.go b/request.go index 5e419e9..e9dd44a 100644 --- a/request.go +++ b/request.go @@ -299,8 +299,10 @@ func retryableStatus(status int, code ErrorCode, policy retryPolicy) bool { // retryableErr reports whether a request whose exchange failed with err may // be sent again. A failure to connect never sent the request, so it may // always be repeated; any other failure may have happened after the API -// received the request. A certificate the client does not trust, or a -// server that does not speak TLS, fails the same way every time. +// received the request. A failure to reach a proxy is a failure to connect +// too: net/http reports it as a "proxyconnect" error around the dial error. +// A certificate the client does not trust, or a server that does not speak +// TLS, fails the same way every time. func retryableErr(err error, policy retryPolicy) bool { var ( verifyErr *tls.CertificateVerificationError @@ -318,7 +320,7 @@ func retryableErr(err error, policy retryPolicy) bool { errors.As(err, &invalidErr), errors.Is(err, http.ErrSchemeMismatch): return false - case errors.As(err, &opErr) && opErr.Op == "dial": + case errors.As(err, &opErr) && (opErr.Op == "dial" || opErr.Op == "proxyconnect"): return true } return policy == retryIfTransient diff --git a/request_test.go b/request_test.go index 1882efb..4b99038 100644 --- a/request_test.go +++ b/request_test.go @@ -7,6 +7,7 @@ import ( "io" "net/http" "net/http/httptest" + "net/url" "strings" "testing" "time" @@ -214,6 +215,25 @@ func TestDialErrorRetried(t *testing.T) { assertPrintable(t, e.Error()) } +// TestProxyDialErrorRetried: a proxy that cannot be reached was sent +// nothing either. +func TestProxyDialErrorRetried(t *testing.T) { + proxy, err := url.Parse(closedURL(t)) + if err != nil { + t.Fatal(err) + } + hc := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxy)}} + c, err := NewClient(testToken, WithHTTPClient(hc), WithBackoff(noBackoff)) + if err != nil { + t.Fatal(err) + } + _, err = c.CreateKey(t.Context(), &CreateKeyParams{Name: "k", Tier: TierFree}) + e := asError(t, err) + if e.Kind != KindTransport || e.Attempts != DefaultMaxAttempts { + t.Errorf("got %s", sprint(e)) + } +} + func TestTLSErrorNotRetried(t *testing.T) { api := newFakeAPI(t) // The fake API speaks plain HTTP, so a TLS client gets a record header