Skip to content

Commit

Permalink
Merge pull request #34 from drone/FFM-1213_delete_events
Browse files Browse the repository at this point in the history
(FFM-1213) Can handle SSE delete events not to clear the cache
  • Loading branch information
Andrew-Hayes committed Jul 28, 2021
2 parents d121388 + e90da61 commit 873a19f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
6 changes: 6 additions & 0 deletions dto/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,10 @@ const (
KeyFeature = "flag"
// KeySegment identifies segment messages from ff server or stream
KeySegment = "target-segment"
// SsePatchEvent identifies a patch event from the SSE stream
SsePatchEvent = "patch"
// SseDeleteEvent identifies a delete event from the SSE stream
SseDeleteEvent = "delete"
// SseCreateEvent identifies a create event from the SSE stream
SseCreateEvent = "create"
)
40 changes: 26 additions & 14 deletions stream/sse.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,34 @@ func (c *SSEClient) Connect(environment string) error {
case dto.KeyFeature:
// maybe is better to send event on memory bus that we get new message
// and subscribe to that event
go func(env, identifier string) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*60)
response, err := c.api.GetFeatureConfigByIdentifierWithResponse(ctx, env, identifier)
if err != nil {
c.logger.Errorf("error while pulling flag, err: %s", err.Error())
cancel()
return
}
if response.JSON200 != nil {
c.cache.Set(dto.Key{
switch cfMsg.Event {
case dto.SseDeleteEvent:
go func() {
c.cache.Remove(dto.Key{
Type: dto.KeyFeature,
Name: cfMsg.Identifier,
}, *response.JSON200.Convert())
}
cancel()
}(environment, cfMsg.Identifier)
})
}()

case dto.SsePatchEvent, dto.SseCreateEvent:
fallthrough
default:
go func(env, identifier string) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*60)
defer cancel()
response, err := c.api.GetFeatureConfigByIdentifierWithResponse(ctx, env, identifier)
if err != nil {
c.logger.Errorf("error while pulling flag, err: %s", err.Error())
return
}
if response.JSON200 != nil {
c.cache.Set(dto.Key{
Type: dto.KeyFeature,
Name: cfMsg.Identifier,
}, *response.JSON200.Convert())
}
}(environment, cfMsg.Identifier)
}
case dto.KeySegment:
// need open client spec change
}
Expand Down

0 comments on commit 873a19f

Please sign in to comment.