-
Notifications
You must be signed in to change notification settings - Fork 240
feat: circuit breaker implementation #1929
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
SkArchon
merged 65 commits into
main
from
milinda/eng-7100-simple-circuit-breaker-for-subgraph-requests
Jul 7, 2025
Merged
Changes from 13 commits
Commits
Show all changes
65 commits
Select commit
Hold shift + click to select a range
eae1b57
feat: circuit breakers
SkArchon 0f8e6cd
feat: circuit breakers
SkArchon e6bd119
fix: tests
SkArchon 75af19f
fix: changes
SkArchon 8c3fb84
fix: circuit breaker
SkArchon d1288b2
fix: circuit breaker
SkArchon 3a7317a
fix: cleanup mod
SkArchon 6c1a657
fix: dependencies
SkArchon 6a85b7a
fix: revert testenv
SkArchon 213ef83
fix: linting
SkArchon 6a9449d
fix: reset defaults
SkArchon eb41fff
fix: linter changes
SkArchon 6ecb667
fix: added prometheus tests
SkArchon b18751d
fix: refactoring
SkArchon c5640d2
chore: temporary skip
SkArchon 59905c5
chore: temp skip
SkArchon 49e0cf2
chore: temp skip
SkArchon 64dafb2
fix: temp skips
SkArchon 3810736
fix: add enable
SkArchon 981bfe0
fix: review comments
SkArchon 1da66af
fix: update go.mod
SkArchon d0ff492
fix: tests
SkArchon 5851867
fix: review comments
SkArchon fadc186
fix: review comments
SkArchon c4eefb6
fix: review comments
SkArchon e5ae625
fix: review comments
SkArchon 6892dc5
fix: review comments
SkArchon 4d53170
Merge remote-tracking branch 'origin/main' into milinda/eng-7100-simp…
SkArchon 070875c
fix: review comments
SkArchon bd82262
fix: typo comment
SkArchon b2e966f
Merge branch 'main' into milinda/eng-7100-simple-circuit-breaker-for-…
StarpTech df2da77
fix: reflect optional
SkArchon 0dc59d8
fix: review comments
SkArchon 590af7b
fix: review comments
SkArchon 7006d26
fix: refactoring
SkArchon b0280f7
fix: added context package
SkArchon 6d32f83
fix: review comments
SkArchon 44747e4
fix: subgraph name
SkArchon 841e48e
fix: tests
SkArchon 25e8ee0
fix: configuration
SkArchon 9a2f4ec
fix: tests
SkArchon fa3ad6e
fix: add lock
SkArchon 4f5349d
fix: review comments
SkArchon e467a2d
fix: updates
SkArchon fb990ca
Merge remote-tracking branch 'origin/main' into milinda/eng-7100-simp…
SkArchon f1ad2b5
fix: tests
SkArchon 9d9d193
fix: tests
SkArchon ea9f922
fix: linter
SkArchon 351a65c
fix: update tests
SkArchon da3200f
fix: refactoring
SkArchon b3e972f
fix: tests
SkArchon 43e1292
fix: enable cbs for all tests
SkArchon 737eb02
fix: set the concurrent max requests
SkArchon 2410e15
fix: update configurations
SkArchon 8082187
fix: metric updates
SkArchon 326a2c2
fix: tests
SkArchon ffa5d4f
fix: circuit breaker
SkArchon 004983d
fix: revert commit
SkArchon 08b8624
fix: revert testenv
SkArchon e0a4af0
fix: revert testenv
SkArchon c646ca4
fix: revert
SkArchon fe51c0e
fix: updates
SkArchon cef2aba
fix: enable
SkArchon 003aedf
fix: comments
SkArchon 5145c35
fix: comments
SkArchon 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,65 @@ | ||
| package circuit | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "github.com/wundergraph/cosmo/router/internal/traceclient" | ||
| "go.uber.org/zap" | ||
| "net/http" | ||
| ) | ||
|
|
||
| type Breaker struct { | ||
| roundTripper http.RoundTripper | ||
| loggerFunc func(req *http.Request) *zap.Logger | ||
| circuitBreaker *Manager | ||
| } | ||
|
|
||
| func NewCircuitTripper(roundTripper http.RoundTripper, breaker *Manager, logger func(req *http.Request) *zap.Logger) *Breaker { | ||
| return &Breaker{ | ||
| circuitBreaker: breaker, | ||
| loggerFunc: logger, | ||
| roundTripper: roundTripper, | ||
| } | ||
| } | ||
|
|
||
| func (rt *Breaker) RoundTrip(req *http.Request) (resp *http.Response, err error) { | ||
| ctx := req.Context() | ||
|
|
||
| var subgraph string | ||
| subgraphCtxVal := ctx.Value(traceclient.CurrentSubgraphContextKey{}) | ||
| if subgraphCtxVal != nil { | ||
| subgraph = subgraphCtxVal.(string) | ||
| } | ||
|
|
||
| var prefix string | ||
| prefixCtxVal := ctx.Value(traceclient.CurrentFeatureFlagContextKey{}) | ||
| if prefixCtxVal != nil { | ||
| prefix = prefixCtxVal.(string) | ||
| } | ||
|
|
||
| cbKey := fmt.Sprintf("%s::%s", prefix, subgraph) | ||
|
|
||
| // If there is no circuit defined for this subgraph | ||
| circuit := rt.circuitBreaker.GetCircuitBreaker(cbKey) | ||
| if circuit == nil { | ||
| return rt.roundTripper.RoundTrip(req) | ||
| } | ||
|
|
||
| preRunStatus := circuit.IsOpen() | ||
|
|
||
| err = circuit.Run(context.Background(), func(_ context.Context) error { | ||
|
StarpTech marked this conversation as resolved.
Outdated
|
||
| resp, err = rt.roundTripper.RoundTrip(req) | ||
|
StarpTech marked this conversation as resolved.
|
||
| return err | ||
| }) | ||
|
|
||
| postRunStatus := circuit.IsOpen() | ||
|
|
||
| logger := rt.loggerFunc(req) | ||
| if preRunStatus != postRunStatus { | ||
| logger.Debug("Circuit breaker status changed", zap.String("subgraph", subgraph), zap.Bool("isOpen", postRunStatus)) | ||
|
SkArchon marked this conversation as resolved.
Outdated
|
||
| } else if preRunStatus { | ||
| logger.Debug("Circuit breaker open, request callback did not execute", zap.String("subgraph", subgraph)) | ||
| } | ||
|
|
||
| return resp, err | ||
| } | ||
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.