Skip to content
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

Conversation

tvarney13
Copy link
Member

Requirements

  • I have added test coverage for new or changed functionality
  • I have followed the repository's pull request submission guidelines
  • I have validated my changes against all supported platform versions

Related issues

Provide links to any issues in this repository or elsewhere relating to this pull request.

Describe the solution you've provided

Syncing a project triggers a put SSE with all flags in the project. The code currently assumes only one project.

Describe alternatives you've considered

Provide a clear and concise description of any alternative solutions or features you've considered.

Additional context

Add any other context about the pull request here.

@tvarney13 tvarney13 self-assigned this Jul 8, 2024
@tvarney13 tvarney13 changed the base branch from main to moonshot-ld-dev-server July 8, 2024 18:17
Comment on lines +1 to +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
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pulled these into a catalog file of events.

// Event for full project sync
type SyncEvent struct {
ProjectKey string
AllFlagsState FlagsState
Copy link
Member Author

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 to AllFlagsState just to add a smidge of clarity

Copy link
Contributor

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.

internal/dev_server/model/project.go Outdated Show resolved Hide resolved
Comment on lines +61 to +72
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"))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +77 to +83
type clientFlag struct {
Key string `json:"key,omitempty"`
Version int `json:"version"`
Value ldvalue.Value `json:"value"`
}

type clientFlags map[string]clientFlag
Copy link
Member Author

Choose a reason for hiding this comment

The 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 omitempty. If we see significant delta between different event bodies some bifurcation would become necessary but for now 🤷

Comment on lines +79 to +95
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
}
Copy link
Member Author

Choose a reason for hiding this comment

The 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 json.Marshal feels beyond superfluous

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

The 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

Copy link
Contributor

@mike-zorn mike-zorn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Up to you re: the additional unit test & integration test scenario. Feel free to re-request a review if you do those and want feedback.

@tvarney13 tvarney13 requested a review from mike-zorn July 8, 2024 21:54
Copy link
Contributor

@mike-zorn mike-zorn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tvarney13 tvarney13 requested a review from mike-zorn July 9, 2024 14:25
defer ld.GetFlagTracker().RemoveFlagValueChangeListener(flagUpdateChan)
trackers[flagKey] = &flagUpdateChan

_, err := store.UpsertOverride(ctx, model.Override{
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm down for that

@tvarney13 tvarney13 requested a review from mike-zorn July 9, 2024 17:13
@tvarney13 tvarney13 merged commit 786c548 into moonshot-ld-dev-server Jul 9, 2024
1 check passed
@tvarney13 tvarney13 deleted the tomvarney/sc-248980/sync-publishes-streaming-update branch July 9, 2024 17:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants