-
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 all 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 |
---|---|---|
|
@@ -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.