-
Notifications
You must be signed in to change notification settings - Fork 0
fix: Set explicit upstream dial timeout and retry failed dials #20
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| 0.11.0 | ||
| 0.12.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package common | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| func TestProxyDialTimeout(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| env string | ||
| want time.Duration | ||
| }{ | ||
| // The default must stay comfortably longer than fasthttp's own 3s | ||
| // DefaultDialTimeout, which is what this setting exists to override. | ||
| {name: "unset falls back to the default", env: "", want: 30 * time.Second}, | ||
| {name: "seconds", env: "5s", want: 5 * time.Second}, | ||
| {name: "sub-second", env: "250ms", want: 250 * time.Millisecond}, | ||
| {name: "compound", env: "1m30s", want: 90 * time.Second}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| // Always set it, empty included: an empty value is what the getter | ||
| // treats as unset, and setting it unconditionally stops a value | ||
| // inherited from the caller's environment from deciding the result. | ||
| t.Setenv("PROXY_DIAL_TIMEOUT", tt.env) | ||
| if got := ProxyDialTimeout(); got != tt.want { | ||
| t.Errorf("ProxyDialTimeout() = %v, want %v", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestProxyDialAttempts(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| env string | ||
| want int | ||
| }{ | ||
| {name: "unset falls back to the default", env: "", want: 3}, | ||
| {name: "one disables retrying", env: "1", want: 1}, | ||
| {name: "explicit value", env: "5", want: 5}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Setenv("PROXY_DIAL_ATTEMPTS", tt.env) | ||
| if got := ProxyDialAttempts(); got != tt.want { | ||
| t.Errorf("ProxyDialAttempts() = %v, want %v", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| // (C) 2026 GoodData Corporation | ||
| package proxy | ||
|
|
||
| import ( | ||
| "errors" | ||
| "net" | ||
| "testing" | ||
|
|
||
| "github.com/valyala/fasthttp" | ||
| ) | ||
|
|
||
| // dialCountingClient returns a Client whose dialer always fails with err, plus a | ||
| // pointer to the dial counter, so a test can assert how many dials a single | ||
| // ProxyRequest performed for a given attempt budget. | ||
| func dialCountingClient(attempts int, err error) (*Client, *int) { | ||
| dials := 0 | ||
| return &Client{ | ||
| HTTP: &fasthttp.Client{ | ||
| Dial: func(_ string) (net.Conn, error) { | ||
| dials++ | ||
| return nil, err | ||
| }, | ||
| }, | ||
| Attempts: attempts, | ||
| }, &dials | ||
| } | ||
|
|
||
| // TestProxyRequestDialAttempts pins the Client.Attempts contract: the value is a | ||
| // budget of total dial attempts, initial dial included. | ||
| // | ||
| // The budget is set on the Client directly rather than through the environment. | ||
| // That it comes from PROXY_DIAL_ATTEMPTS is common.ProxyDialAttempts's contract | ||
| // (covered in common_test.go) and NewClient's (TestNewClientAttempts below); | ||
| // ProxyRequest only has to honour whatever budget it was handed. | ||
| // | ||
| // The method is varied on purpose. fasthttp's own HostClient retries idempotent | ||
| // requests (GET/HEAD/PUT) internally up to DefaultMaxIdemponentCallAttempts, so | ||
| // this also guards against that stacking on top of our loop and multiplying the | ||
| // dial count. | ||
| func TestProxyRequestDialAttempts(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| method string | ||
| attempts int | ||
| wantDials int | ||
| }{ | ||
| {name: "default budget, POST", method: "POST", attempts: 3, wantDials: 3}, | ||
| {name: "default budget, GET", method: "GET", attempts: 3, wantDials: 3}, | ||
| {name: "retrying disabled, POST", method: "POST", attempts: 1, wantDials: 1}, | ||
| {name: "retrying disabled, GET", method: "GET", attempts: 1, wantDials: 1}, | ||
| {name: "explicit budget, POST", method: "POST", attempts: 4, wantDials: 4}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| client, dials := dialCountingClient(tt.attempts, fasthttp.ErrDialTimeout) | ||
|
|
||
| ctx := &fasthttp.RequestCtx{} | ||
| ctx.Request.Header.SetMethod(tt.method) | ||
| ctx.Request.SetRequestURI("/api/thing") | ||
|
|
||
| _, _, _, err := ProxyRequest(client, "http://upstream.invalid", ctx) | ||
| if !errors.Is(err, fasthttp.ErrDialTimeout) { | ||
| t.Fatalf("ProxyRequest() error = %v, want it to wrap fasthttp.ErrDialTimeout", err) | ||
| } | ||
| if *dials != tt.wantDials { | ||
| t.Errorf("dial count = %d, want %d", *dials, tt.wantDials) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestNewClientAttempts covers the wiring the table above no longer exercises: | ||
| // NewClient resolves PROXY_DIAL_ATTEMPTS once, at construction. That is what | ||
| // keeps an invalid value fatal at startup instead of on the first proxied | ||
| // request, and what keeps the attempt budget on the same snapshot as the dial | ||
| // timeout baked into the dialer. | ||
| func TestNewClientAttempts(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| env string | ||
| want int | ||
| }{ | ||
| {name: "default when unset", env: "", want: 3}, | ||
| {name: "value from environment", env: "5", want: 5}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Setenv("PROXY_DIAL_ATTEMPTS", tt.env) | ||
|
|
||
| if got := NewClient().Attempts; got != tt.want { | ||
| t.Errorf("NewClient().Attempts = %d, want %d", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestProxyRequestDoesNotRetryNonDialErrors makes sure the retry is scoped to | ||
| // dial failures. Once a connection exists we cannot know whether the upstream | ||
| // applied the request, so anything else must surface on the first attempt. | ||
| func TestProxyRequestDoesNotRetryNonDialErrors(t *testing.T) { | ||
| sentinel := errors.New("connection refused by test") | ||
| client, dials := dialCountingClient(3, sentinel) | ||
|
|
||
| ctx := &fasthttp.RequestCtx{} | ||
| ctx.Request.Header.SetMethod("POST") | ||
| ctx.Request.SetRequestURI("/api/thing") | ||
|
|
||
| _, _, _, err := ProxyRequest(client, "http://upstream.invalid", ctx) | ||
| if err == nil { | ||
| t.Fatal("ProxyRequest() error = nil, want the dialer's error") | ||
| } | ||
| if errors.Is(err, fasthttp.ErrDialTimeout) { | ||
| t.Fatalf("ProxyRequest() error = %v, should not be classified as a dial timeout", err) | ||
| } | ||
| if *dials != 1 { | ||
| t.Errorf("dial count = %d, want 1 — a non-dial-timeout error must not be retried", *dials) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.