Repository navigation
fix(circuitbreaker): requests no route matched neither count nor close the breaker #1292
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
6 commits
Select commit
Hold shift + click to select a range
c58ed9d
fix(circuitbreaker): requests no route matched neither count nor clos…
FumingPower3925 6768fa8
fix(circuitbreaker): Config.CountUnmatched opts unmatched requests ba…
FumingPower3925 83b2bda
Merge branch 'main' into fix/888-circuitbreaker-unmatched
FumingPower3925 fdfe25d
test(circuitbreaker): wait for the probe result in the 888 test
FumingPower3925 95f8f5f
Merge branch 'main' into fix/888-circuitbreaker-unmatched
FumingPower3925 2d33708
Merge branch 'main' into fix/888-circuitbreaker-unmatched
FumingPower3925 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
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,157 @@ | ||
| package circuitbreaker | ||
|
|
||
| import ( | ||
| "context" | ||
| "io" | ||
| "net" | ||
| "net/http" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/goceleris/celeris" | ||
| ) | ||
|
|
||
| // TestUnmatchedRequestsDoNotCountAgainstTheBreaker888: a breaker mounted with | ||
| // Server.Use also sees the requests no route matched (a crawler asking for | ||
| // /favicon.ico, a method a path does not take, the automatic OPTIONS answer). | ||
| // They reach no downstream, and a 404 is not a failure under the default | ||
| // IsError, so while the breaker was HalfOpen (or Open past its cooldown) the | ||
| // first of them was taken for the probe, counted a success, and closed a | ||
| // breaker whose downstream had not been asked. | ||
| func TestUnmatchedRequestsDoNotCountAgainstTheBreaker888(t *testing.T) { | ||
| mw, brk := NewWithBreaker(Config{ | ||
| Threshold: 0.5, | ||
| MinRequests: 2, | ||
| WindowSize: time.Minute, | ||
| CooldownPeriod: 50 * time.Millisecond, | ||
| HalfOpenMax: 1, | ||
| }) | ||
| s := celeris.New(celeris.Config{Engine: celeris.Std}) | ||
| s.Use(mw) | ||
| s.GET("/boom", func(*celeris.Context) error { return celeris.NewHTTPError(500, "down") }) | ||
| s.GET("/ok", func(c *celeris.Context) error { return c.String(200, "ok") }) | ||
| ln, err := net.Listen("tcp", "127.0.0.1:0") | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| done := make(chan error, 1) | ||
| go func() { done <- s.StartWithListenerAndContext(ctx, ln) }() | ||
| defer func() { | ||
| cancel() | ||
| select { | ||
| case err := <-done: | ||
| if err != nil { | ||
| t.Logf("server stopped: %v", err) | ||
| } | ||
| case <-time.After(10 * time.Second): | ||
| t.Error("server did not stop within 10s") | ||
| } | ||
| }() | ||
| base := "http://" + ln.Addr().String() | ||
| cl := &http.Client{Timeout: 5 * time.Second} | ||
| do := func(method, path string) int { | ||
| t.Helper() | ||
| var resp *http.Response | ||
| var err error | ||
| for deadline := time.Now().Add(5 * time.Second); ; { | ||
| req, _ := http.NewRequest(method, base+path, nil) | ||
| resp, err = cl.Do(req) | ||
| if err == nil || time.Now().After(deadline) { | ||
| break | ||
| } | ||
| time.Sleep(20 * time.Millisecond) | ||
| } | ||
| if err != nil { | ||
| t.Fatalf("%s %s: %v", method, path, err) | ||
| } | ||
| _, _ = io.Copy(io.Discard, resp.Body) | ||
| _ = resp.Body.Close() | ||
| return resp.StatusCode | ||
| } | ||
|
|
||
| if st := do("GET", "/ok"); st != 200 { | ||
| t.Fatalf("GET /ok: %d", st) | ||
| } | ||
| do("GET", "/boom") | ||
| do("GET", "/boom") | ||
| if brk.State() != Open { | ||
| t.Fatalf("after two failures: %s, want open", brk.State()) | ||
| } | ||
| time.Sleep(80 * time.Millisecond) // past the cooldown | ||
|
|
||
| for _, rq := range []struct { | ||
| method, path string | ||
| want int | ||
| }{ | ||
| {"GET", "/favicon.ico", 404}, // <unmatched> | ||
| {"DELETE", "/ok", 405}, // <method-not-allowed> | ||
| {"OPTIONS", "/ok", 200}, // <options> | ||
| } { | ||
| if st := do(rq.method, rq.path); st != rq.want { | ||
| t.Errorf("%s %s: %d, want %d", rq.method, rq.path, st, rq.want) | ||
| } | ||
| if got := brk.State(); got != Open { | ||
| t.Fatalf("after %s %s: breaker %s, want it still open (the request reached no downstream)", rq.method, rq.path, got) | ||
| } | ||
| } | ||
| // A request the downstream answers is the probe. | ||
| if st := do("GET", "/ok"); st != 200 { | ||
| t.Errorf("probe GET /ok: %d, want 200", st) | ||
| } | ||
| // The std engine flushes a c.String reply to the client from inside the | ||
| // handler, before the breaker middleware regains control after Next and | ||
| // records the probe's result, so the client can see the 200 and read the | ||
| // state a moment before the HalfOpen -> Closed transition. Wait for it. | ||
| // (A failed probe is not racy this way: the error is returned up the chain | ||
| // and answered after the breaker has settled.) The checks inside the loop | ||
| // above stay immediate: an unmatched request is never counted, so it | ||
| // neither starts nor settles a probe, and the Open state only changes when | ||
| // a counted request arrives. | ||
| deadline := time.Now().Add(5 * time.Second) | ||
| for brk.State() != Closed && time.Now().Before(deadline) { | ||
| time.Sleep(5 * time.Millisecond) | ||
| } | ||
| if got := brk.State(); got != Closed { | ||
| t.Errorf("after a successful probe: %s, want closed", got) | ||
| } | ||
| } | ||
|
|
||
| // TestCountUnmatchedRefusesFallback888: with CountUnmatched a NotFound handler | ||
| // that forwards to the protected downstream is refused while Open. | ||
| func TestCountUnmatchedRefusesFallback888(t *testing.T) { | ||
| mw, brk := NewWithBreaker(Config{CountUnmatched: true, MinRequests: 1, Threshold: 0.5, CooldownPeriod: time.Hour}) | ||
| s := celeris.New(celeris.Config{Engine: celeris.Std}) | ||
| s.Use(mw) | ||
| s.NotFound(func(c *celeris.Context) error { return celeris.NewHTTPError(502, "upstream down") }) | ||
| ln, err := net.Listen("tcp", "127.0.0.1:0") | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| defer cancel() | ||
| go func() { _ = s.StartWithListenerAndContext(ctx, ln) }() | ||
| get := func() int { | ||
| for deadline := time.Now().Add(5 * time.Second); ; { | ||
| resp, err := http.Get("http://" + ln.Addr().String() + "/legacy") | ||
| if err == nil { | ||
| _, _ = io.Copy(io.Discard, resp.Body) | ||
| _ = resp.Body.Close() | ||
| return resp.StatusCode | ||
| } | ||
| if time.Now().After(deadline) { | ||
| t.Fatal(err) | ||
| } | ||
| time.Sleep(20 * time.Millisecond) | ||
| } | ||
| } | ||
| if st := get(); st != 502 { | ||
| t.Fatalf("first: %d", st) | ||
| } | ||
| if brk.State() != Open { | ||
| t.Fatalf("state %s, want open", brk.State()) | ||
| } | ||
| if st := get(); st != 503 { | ||
| t.Errorf("while open: %d, want 503", st) | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
Repository: goceleris/celeris
Length of output: 4062
Script executed:
Repository: goceleris/celeris
Length of output: 8937
Poll the cooldown condition instead of sleeping.
The fixed sleep synchronizes with the cooldown, which the test rules prohibit.
Replace the sleep