From 3990a58c457c9125858a458a07dbd815772c6bb0 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 28 Oct 2026 01:38:31 +0000 Subject: [PATCH] Retry a request whose proxy cannot be reached net/http reports a failed dial to a proxy as a "proxyconnect" *net.OpError wrapping the dial error, and errors.As stopped at the outer one, so the request was treated as possibly sent. Calls that are retried only when the request certainly was not sent, such as CreateKey, then failed after one attempt, against the package documentation's promise that a failure to connect is always retried. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01NYjhaGqZtskW52B5CMxFQS --- CHANGELOG.md | 7 +++++++ request.go | 8 +++++--- request_test.go | 20 ++++++++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) 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