-
Notifications
You must be signed in to change notification settings - Fork 66
fix(flagd): do not retry for certain status codes (#756) #783
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
Open
alexandraoberaigner
wants to merge
2
commits into
open-feature:feat/flagd-inprocess-eventing-and-grace-period
Choose a base branch
from
open-feature-forking:fix/inifinite-loop-error
base: feat/flagd-inprocess-eventing-and-grace-period
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ import ( | |
| "buf.build/gen/go/open-feature/flagd/grpc/go/flagd/sync/v1/syncv1grpc" | ||
| v1 "buf.build/gen/go/open-feature/flagd/protocolbuffers/go/flagd/sync/v1" | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "github.com/open-feature/flagd/core/pkg/logger" | ||
| "github.com/open-feature/flagd/core/pkg/sync" | ||
|
|
@@ -12,6 +13,8 @@ import ( | |
| "google.golang.org/grpc" | ||
| "google.golang.org/grpc/connectivity" | ||
| "google.golang.org/grpc/keepalive" | ||
| "google.golang.org/grpc/status" | ||
| "strings" | ||
| msync "sync" | ||
| "time" | ||
| ) | ||
|
|
@@ -35,28 +38,25 @@ const ( | |
| "MaxBackoff": "5s", | ||
| "BackoffMultiplier": 2.0, | ||
| "RetryableStatusCodes": [ | ||
| "CANCELLED", | ||
| "UNKNOWN", | ||
| "INVALID_ARGUMENT", | ||
| "NOT_FOUND", | ||
| "ALREADY_EXISTS", | ||
| "PERMISSION_DENIED", | ||
| "RESOURCE_EXHAUSTED", | ||
| "FAILED_PRECONDITION", | ||
| "ABORTED", | ||
| "OUT_OF_RANGE", | ||
| "UNIMPLEMENTED", | ||
| "INTERNAL", | ||
| "UNAVAILABLE", | ||
| "DATA_LOSS", | ||
| "UNAUTHENTICATED" | ||
| "UNAVAILABLE" | ||
| ] | ||
| } | ||
| } | ||
| ] | ||
| }` | ||
|
|
||
| nonRetryableStatusCodes = ` | ||
| [ | ||
| "PermissionDenied", | ||
| "Unauthenticated" | ||
| ] | ||
| ` | ||
| ) | ||
|
|
||
| // Set of non-retryable gRPC status codes for faster lookup | ||
| var nonRetryableCodes map[string]struct{} | ||
|
|
||
| // Type aliases for interfaces required by this component - needed for mock generation with gomock | ||
| type FlagSyncServiceClient interface { | ||
| syncv1grpc.FlagSyncServiceClient | ||
|
|
@@ -78,6 +78,7 @@ type Sync struct { | |
| Selector string | ||
| URI string | ||
| MaxMsgSize int | ||
| RetryGracePeriod int | ||
|
|
||
| // Runtime state | ||
| client FlagSyncServiceClient | ||
|
|
@@ -92,6 +93,7 @@ type Sync struct { | |
| // Init initializes the gRPC connection and starts background monitoring | ||
| func (g *Sync) Init(ctx context.Context) error { | ||
| g.Logger.Info(fmt.Sprintf("initializing gRPC client for %s", g.URI)) | ||
| g.initNonRetryableStatusCodesSet() | ||
|
|
||
| // Initialize channels | ||
| g.shutdownComplete = make(chan struct{}) | ||
|
|
@@ -160,6 +162,20 @@ func (g *Sync) buildDialOptions() ([]grpc.DialOption, error) { | |
| return dialOptions, nil | ||
| } | ||
|
|
||
| // initNonRetryableStatusCodesSet initializes the set of non-retryable gRPC status codes for quick lookup | ||
| func (g *Sync) initNonRetryableStatusCodesSet() { | ||
| var codes []string | ||
| nonRetryableCodes = make(map[string]struct{}) | ||
| trimmed := strings.TrimSpace(nonRetryableStatusCodes) | ||
| if err := json.Unmarshal([]byte(trimmed), &codes); err == nil { | ||
| for _, code := range codes { | ||
| nonRetryableCodes[code] = struct{}{} | ||
| } | ||
| } else { | ||
| g.Logger.Debug("parsing non-retryable status codes failed, retrying on all errors") | ||
| } | ||
| } | ||
|
|
||
| // ReSync performs a one-time fetch of all flags | ||
| func (g *Sync) ReSync(ctx context.Context, dataSync chan<- sync.DataSync) error { | ||
| g.Logger.Debug("performing ReSync - fetching all flags") | ||
|
|
@@ -207,12 +223,31 @@ func (g *Sync) Sync(ctx context.Context, dataSync chan<- sync.DataSync) error { | |
| } | ||
|
|
||
| // Attempt to create sync stream | ||
| if err := g.performSyncCycle(ctx, dataSync); err != nil { | ||
| err := g.performSyncCycle(ctx, dataSync) | ||
| if err != nil { | ||
| if ctx.Err() != nil { | ||
| g.Logger.Info("sync cycle failed due to context cancellation") | ||
| return ctx.Err() | ||
| } | ||
|
|
||
| // Check if error is a gRPC status error and if code is retryable | ||
| st, ok := status.FromError(err) | ||
| if ok { | ||
| codeStr := st.Code().String() | ||
| if _, found := nonRetryableCodes[codeStr]; found { | ||
| errStr := fmt.Sprintf("sync cycle failed with non-retryable status: %v, " + | ||
| "returning provider fatal.", codeStr) | ||
| g.Logger.Error(errStr) | ||
| return &of.ProviderInitError{ | ||
| ErrorCode: of.ProviderFatalCode, | ||
| Message: errStr, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Backoff before retrying | ||
| time.Sleep(time.Duration(g.RetryGracePeriod)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use the |
||
|
|
||
| g.Logger.Warn(fmt.Sprintf("sync cycle failed: %v, retrying...", err)) | ||
| g.sendEvent(ctx, SyncEvent{event: of.ProviderError}) | ||
|
|
||
|
|
||
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
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.
In the java impl, we set this to the maxBackoff param: https://github.com/open-feature/java-sdk-contrib/pull/1590/files#diff-bbef645a236a67bc95a5f8aa30fa5a528c6b2d45b4f4137b4f4b1074af197f26R57
See: https://flagd.dev/providers/rust/?h=backoff#configuration-options
That way, there's consistency between the gRPC RPC-level retries and our stream cycle.
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.
It may be easier to put this in a small util function in another file along with the nonRetryableCodes var if you do.