-
Notifications
You must be signed in to change notification settings - Fork 22
chore: Support flag change listeners in contract tests #349
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
4 commits
Select commit
Hold shift + click to select a range
217eb2f
feat(testservice): add flag change listener servicedef types
aaron-zeisler 6031592
feat(testservice): implement flag change listener command handlers
aaron-zeisler 4c241dc
feat(testservice): advertise flag-change-listeners capability
aaron-zeisler cbb3c53
Responding to code review comments
aaron-zeisler 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "sync" | ||
|
|
||
| "github.com/launchdarkly/go-sdk-common/v3/ldcontext" | ||
| "github.com/launchdarkly/go-sdk-common/v3/ldvalue" | ||
| "github.com/launchdarkly/go-server-sdk/v7/interfaces" | ||
| "github.com/launchdarkly/go-server-sdk/v7/testservice/servicedef" | ||
| ) | ||
|
|
||
| // listenerEntry holds the cancellation handle for one registered listener goroutine. | ||
| type listenerEntry struct { | ||
| cancel context.CancelFunc | ||
| } | ||
|
|
||
| // listenerRegistry manages all active flag change listener registrations for a single | ||
| // SDK client entity. It is safe to use from multiple goroutines. | ||
| type listenerRegistry struct { | ||
| mu sync.Mutex | ||
| listeners map[string]*listenerEntry // keyed by listenerId | ||
| tracker interfaces.FlagTracker | ||
| } | ||
|
|
||
| func newListenerRegistry(tracker interfaces.FlagTracker) *listenerRegistry { | ||
| return &listenerRegistry{ | ||
| listeners: make(map[string]*listenerEntry), | ||
| tracker: tracker, | ||
| } | ||
| } | ||
|
|
||
| // storeListener registers a new listener entry under listenerID, cancelling any | ||
| // previously registered listener with the same ID. Returns the new entry's context. | ||
| func (r *listenerRegistry) storeListener(listenerID string) context.Context { | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| r.mu.Lock() | ||
| if old, exists := r.listeners[listenerID]; exists { | ||
| old.cancel() | ||
| } | ||
| r.listeners[listenerID] = &listenerEntry{cancel: cancel} | ||
| r.mu.Unlock() | ||
| return ctx | ||
| } | ||
|
|
||
| // registerFlagChangeListener subscribes to general flag configuration changes. | ||
| // All flag change events are forwarded to the callback URI. | ||
| func (r *listenerRegistry) registerFlagChangeListener(listenerID, callbackURI string) { | ||
| ch := r.tracker.AddFlagChangeListener() | ||
| ctx := r.storeListener(listenerID) | ||
|
|
||
| svc := callbackService{baseURL: callbackURI} | ||
| go func() { | ||
| defer r.tracker.RemoveFlagChangeListener(ch) | ||
| for { | ||
| select { | ||
| case <-ctx.Done(): | ||
| return | ||
| case event, ok := <-ch: | ||
| if !ok { | ||
| return | ||
| } | ||
| _ = svc.post("", servicedef.ListenerNotification{ | ||
| ListenerID: listenerID, | ||
| FlagKey: event.Key, | ||
| }, nil) | ||
| } | ||
| } | ||
| }() | ||
| } | ||
|
|
||
| // registerFlagValueChangeListener subscribes to value changes for a specific flag and | ||
| // evaluation context. The callback is invoked only when the evaluated value actually | ||
| // changes; configuration changes that leave the value unchanged are suppressed by the SDK. | ||
| func (r *listenerRegistry) registerFlagValueChangeListener( | ||
| listenerID, flagKey string, | ||
| evalCtx ldcontext.Context, | ||
| defaultValue ldvalue.Value, | ||
| callbackURI string, | ||
| ) { | ||
| ch := r.tracker.AddFlagValueChangeListener(flagKey, evalCtx, defaultValue) | ||
| ctx := r.storeListener(listenerID) | ||
|
|
||
| svc := callbackService{baseURL: callbackURI} | ||
| go func() { | ||
| defer r.tracker.RemoveFlagValueChangeListener(ch) | ||
| for { | ||
| select { | ||
| case <-ctx.Done(): | ||
| return | ||
| case event, ok := <-ch: | ||
| if !ok { | ||
| return | ||
| } | ||
| oldVal := event.OldValue | ||
| newVal := event.NewValue | ||
| _ = svc.post("", servicedef.ListenerNotification{ | ||
| ListenerID: listenerID, | ||
| FlagKey: event.Key, | ||
| OldValue: &oldVal, | ||
| NewValue: &newVal, | ||
| }, nil) | ||
| } | ||
| } | ||
| }() | ||
| } | ||
|
|
||
| // unregister stops the listener goroutine for the given ID and removes it from the | ||
| // registry. Returns false if no listener with that ID was found. | ||
| func (r *listenerRegistry) unregister(listenerID string) bool { | ||
| r.mu.Lock() | ||
| entry, ok := r.listeners[listenerID] | ||
| if ok { | ||
| delete(r.listeners, listenerID) | ||
| } | ||
| r.mu.Unlock() | ||
|
|
||
| if ok { | ||
| entry.cancel() | ||
| } | ||
| return ok | ||
| } | ||
|
|
||
| // closeAll stops all active listener goroutines. Called when the SDK client entity closes. | ||
| func (r *listenerRegistry) closeAll() { | ||
| r.mu.Lock() | ||
| listeners := r.listeners | ||
| r.listeners = make(map[string]*listenerEntry) | ||
| r.mu.Unlock() | ||
|
|
||
| for _, entry := range listeners { | ||
| entry.cancel() | ||
| } | ||
| } | ||
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
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.