Skip to content

Commit e90da61

Browse files
committed
(FFM-1213) Can handle SSE delete events not to clear the cache
1 parent d121388 commit e90da61

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

dto/key.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,10 @@ const (
1111
KeyFeature = "flag"
1212
// KeySegment identifies segment messages from ff server or stream
1313
KeySegment = "target-segment"
14+
// SsePatchEvent identifies a patch event from the SSE stream
15+
SsePatchEvent = "patch"
16+
// SseDeleteEvent identifies a delete event from the SSE stream
17+
SseDeleteEvent = "delete"
18+
// SseCreateEvent identifies a create event from the SSE stream
19+
SseCreateEvent = "create"
1420
)

stream/sse.go

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,34 @@ func (c *SSEClient) Connect(environment string) error {
6363
case dto.KeyFeature:
6464
// maybe is better to send event on memory bus that we get new message
6565
// and subscribe to that event
66-
go func(env, identifier string) {
67-
ctx, cancel := context.WithTimeout(context.Background(), time.Second*60)
68-
response, err := c.api.GetFeatureConfigByIdentifierWithResponse(ctx, env, identifier)
69-
if err != nil {
70-
c.logger.Errorf("error while pulling flag, err: %s", err.Error())
71-
cancel()
72-
return
73-
}
74-
if response.JSON200 != nil {
75-
c.cache.Set(dto.Key{
66+
switch cfMsg.Event {
67+
case dto.SseDeleteEvent:
68+
go func() {
69+
c.cache.Remove(dto.Key{
7670
Type: dto.KeyFeature,
7771
Name: cfMsg.Identifier,
78-
}, *response.JSON200.Convert())
79-
}
80-
cancel()
81-
}(environment, cfMsg.Identifier)
72+
})
73+
}()
74+
75+
case dto.SsePatchEvent, dto.SseCreateEvent:
76+
fallthrough
77+
default:
78+
go func(env, identifier string) {
79+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*60)
80+
defer cancel()
81+
response, err := c.api.GetFeatureConfigByIdentifierWithResponse(ctx, env, identifier)
82+
if err != nil {
83+
c.logger.Errorf("error while pulling flag, err: %s", err.Error())
84+
return
85+
}
86+
if response.JSON200 != nil {
87+
c.cache.Set(dto.Key{
88+
Type: dto.KeyFeature,
89+
Name: cfMsg.Identifier,
90+
}, *response.JSON200.Convert())
91+
}
92+
}(environment, cfMsg.Identifier)
93+
}
8294
case dto.KeySegment:
8395
// need open client spec change
8496
}

0 commit comments

Comments
 (0)