-
Notifications
You must be signed in to change notification settings - Fork 1
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
feat: Sync publishes streaming update #371
Changes from 11 commits
ab71501
de60967
e1724a0
a343185
e31492c
8138abc
fa1f288
87b2fde
2ad9e37
56daafb
c254a99
797990a
19cdfda
d341962
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package model | ||
|
||
// Event for individual flag overrides | ||
type UpsertOverrideEvent struct { | ||
FlagKey string | ||
ProjectKey string | ||
FlagState FlagState | ||
} | ||
|
||
// Event for full project sync | ||
type SyncEvent struct { | ||
ProjectKey string | ||
AllFlagsState FlagsState | ||
} | ||
Comment on lines
+1
to
+14
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. Pulled these into a catalog file of events. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ import ( | |
"github.com/launchdarkly/go-sdk-common/v3/ldcontext" | ||
"github.com/launchdarkly/go-sdk-common/v3/ldvalue" | ||
ldclient "github.com/launchdarkly/go-server-sdk/v7" | ||
"github.com/launchdarkly/go-server-sdk/v7/interfaces" | ||
"github.com/launchdarkly/go-server-sdk/v7/interfaces/flagstate" | ||
"github.com/launchdarkly/ldcli/internal/dev_server/adapters/mocks" | ||
"github.com/launchdarkly/ldcli/internal/dev_server/db" | ||
|
@@ -62,7 +63,7 @@ func TestSDKRoutesViaGoSDK(t *testing.T) { | |
AddFlag("jsonFlag", flagstate.FlagState{Value: ldvalue.CopyArbitraryValue(map[string]any{"cat": "hat"})}). | ||
Build() | ||
|
||
sdk.EXPECT().GetAllFlagsState(gomock.Any(), gomock.Any(), testSdkKey).Return(allFlags, nil) | ||
sdk.EXPECT().GetAllFlagsState(gomock.Any(), gomock.Any(), testSdkKey).Return(allFlags, nil).Times(2) | ||
_, err = model.CreateProject(ctx, projectKey, environmentKey, nil) | ||
require.NoError(t, err) | ||
|
||
|
@@ -123,4 +124,43 @@ func TestSDKRoutesViaGoSDK(t *testing.T) { | |
assert.Equal(t, value.AsArbitraryValue(), flagUpdate.NewValue.AsArbitraryValue()) | ||
}) | ||
} | ||
|
||
newUpdates := map[string]ldvalue.Value{ | ||
"boolFlag": ldvalue.Bool(true), | ||
"stringFlag": ldvalue.String("fool"), | ||
"intFlag": ldvalue.Int(567), | ||
"doubleFlag": ldvalue.Float64(99.99), | ||
"jsonFlag": ldvalue.CopyArbitraryValue(map[string]any{"tortoise": "hare"}), | ||
} | ||
|
||
// This test is testing the "put" payload in a roundabout way by verifying each of the flags are in there. | ||
tvarney13 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
t.Run("Sync sends full flag payload for project", func(t *testing.T) { | ||
trackers := make(map[string]*<-chan interfaces.FlagValueChangeEvent, len(updates)) | ||
tvarney13 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
for flagKey, newValue := range newUpdates { | ||
flagUpdateChan := ld.GetFlagTracker().AddFlagValueChangeListener(flagKey, ldContext, ldvalue.String("uh-oh")) | ||
defer ld.GetFlagTracker().RemoveFlagValueChangeListener(flagUpdateChan) | ||
trackers[flagKey] = &flagUpdateChan | ||
|
||
_, err := store.UpsertOverride(ctx, model.Override{ | ||
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. This is probably OK, but it is kinda a hack since we're calling the store directly in what's supposed to be more of an integration test (although not quite since we're mocking out the external stuff anyways). WDYT about a more realistic scenario where we test resync before we test overrides and we change the mock response from the SDK to have different values and higher version numbers? 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. I'm down for that |
||
ProjectKey: projectKey, | ||
FlagKey: flagKey, | ||
Value: newValue, | ||
Active: true, | ||
Version: 1, | ||
}) | ||
require.NoError(t, err) | ||
} | ||
|
||
_, err := model.SyncProject(ctx, projectKey) | ||
require.NoError(t, err) | ||
|
||
for flagKey, value := range newUpdates { | ||
updateTracker, ok := trackers[flagKey] | ||
require.True(t, ok) | ||
|
||
update := <-*updateTracker | ||
assert.Equal(t, value.AsArbitraryValue(), update.NewValue.AsArbitraryValue()) | ||
} | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,11 @@ func StreamClientFlags(w http.ResponseWriter, r *http.Request) { | |
WriteError(ctx, w, errors.Wrap(err, "failed to marshal flag state")) | ||
return | ||
} | ||
updateChan, doneChan := OpenStream(w, r.Context().Done(), Message{"put", jsonBody}) | ||
updateChan, doneChan := OpenStream( | ||
w, | ||
r.Context().Done(), | ||
Message{Event: TYPE_PUT, Data: jsonBody}, | ||
) | ||
defer close(updateChan) | ||
projectKey := GetProjectKeyFromContext(ctx) | ||
observer := clientFlagsObserver{updateChan, projectKey} | ||
|
@@ -50,23 +54,34 @@ func (c clientFlagsObserver) Handle(event interface{}) { | |
log.Printf("clientFlagsObserver: handling flag state event: %v", event) | ||
switch event := event.(type) { | ||
case model.UpsertOverrideEvent: | ||
data, err := json.Marshal(clientSidePatchData{ | ||
err := SendMessage(c.updateChan, TYPE_PATCH, clientFlag{ | ||
Key: event.FlagKey, | ||
Version: event.FlagState.Version, | ||
Value: event.FlagState.Value, | ||
}) | ||
if err != nil { | ||
panic(errors.Wrap(err, "failed to marshal flag state in observer")) | ||
} | ||
c.updateChan <- Message{ | ||
Event: "patch", | ||
Data: data, | ||
case model.SyncEvent: | ||
clientFlags := clientFlags{} | ||
for flagKey, flagState := range event.AllFlagsState { | ||
clientFlags[flagKey] = clientFlag{ | ||
Version: flagState.Version, | ||
Value: flagState.Value, | ||
} | ||
} | ||
|
||
err := SendMessage(c.updateChan, TYPE_PUT, clientFlags) | ||
if err != nil { | ||
panic(errors.Wrap(err, "failed to marshal flag state in observer")) | ||
Comment on lines
+65
to
+76
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. Tried to follow this format as closely as possible - https://launchdarkly.atlassian.net/wiki/spaces/PD/pages/521668284/SDK+streaming+specification#Client-side-%E2%80%9Cput%E2%80%9D-message |
||
} | ||
} | ||
} | ||
|
||
type clientSidePatchData struct { | ||
Key string `json:"key"` | ||
type clientFlag struct { | ||
Key string `json:"key,omitempty"` | ||
Version int `json:"version"` | ||
Value ldvalue.Value `json:"value"` | ||
} | ||
|
||
type clientFlags map[string]clientFlag | ||
Comment on lines
+81
to
+87
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. Declaring a new struct didn't feel super useful at the moment vs using |
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.
This got renamed from
FlagsState
toAllFlagsState
just to add a smidge of clarityThere 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.
Nice! It was very confusing.