-
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 4 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 |
---|---|---|
|
@@ -50,23 +50,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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,23 @@ | ||
package sdk | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
type MessageType string | ||
|
||
const ( | ||
TYPE_PUT MessageType = "put" | ||
TYPE_PATCH MessageType = "patch" | ||
) | ||
|
||
type Message struct { | ||
Event string | ||
Event MessageType | ||
Data []byte | ||
} | ||
|
||
|
@@ -67,3 +75,21 @@ func OpenStream(w http.ResponseWriter, done <-chan struct{}, initialMessage Mess | |
}() | ||
return updateChan, errChan | ||
} | ||
|
||
func SendMessage( | ||
updateChan chan<- Message, | ||
msgType MessageType, | ||
data interface{}, | ||
) error { | ||
payload, err := json.Marshal(data) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
updateChan <- Message{ | ||
Event: msgType, | ||
Data: payload, | ||
} | ||
|
||
return nil | ||
} | ||
Comment on lines
+79
to
+95
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. Not really sure how / if to unit test this - I've used Go channels only sparingly. On the other hand - emitting messages worked fine before this PR, and testing 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. Might be good to add to the integration tests for syncing the project. RE: unit testing, you could do something like the following. ...
func TestSendMessage(t *testing.T) {
chan := make(chan Message, 1) // needs to be buffered or else you'll deadlock
err := SendMessage(chan, PUT_TYPE, "cat")
require.NoError(t, err)
message := <-chan
...do assertions
} I don't think it's super important to unit test this function: it doesn't really have any logic & it is already being exercised through the integration tests. 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 decided to implement the integration test for anyone reading this after the fact |
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.