stream: include the namespace in the snap cache key

Otherwise the wrong snapshot could be returned when the same key is used in different namespaces
This commit is contained in:
Daniel Nephin 2020-10-21 16:33:46 -04:00
parent 8da30fcb9a
commit b57c7afcbb
1 changed files with 7 additions and 3 deletions

View File

@ -267,7 +267,7 @@ func (e *EventPublisher) getCachedSnapshotLocked(req *SubscribeRequest) *eventSn
e.snapCache[req.Topic] = topicSnaps e.snapCache[req.Topic] = topicSnaps
} }
snap, ok := topicSnaps[req.Key] snap, ok := topicSnaps[snapCacheKey(req)]
if ok && snap.err() == nil { if ok && snap.err() == nil {
return snap return snap
} }
@ -279,12 +279,16 @@ func (e *EventPublisher) setCachedSnapshotLocked(req *SubscribeRequest, snap *ev
if e.snapCacheTTL == 0 { if e.snapCacheTTL == 0 {
return return
} }
e.snapCache[req.Topic][req.Key] = snap e.snapCache[req.Topic][snapCacheKey(req)] = snap
// Setup a cache eviction // Setup a cache eviction
time.AfterFunc(e.snapCacheTTL, func() { time.AfterFunc(e.snapCacheTTL, func() {
e.lock.Lock() e.lock.Lock()
defer e.lock.Unlock() defer e.lock.Unlock()
delete(e.snapCache[req.Topic], req.Key) delete(e.snapCache[req.Topic], snapCacheKey(req))
}) })
} }
func snapCacheKey(req *SubscribeRequest) string {
return fmt.Sprintf(req.Namespace + "/" + req.Key)
}