From b57c7afcbb0dd5930429acffbd28870a65391e1a Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Wed, 21 Oct 2020 16:33:46 -0400 Subject: [PATCH] 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 --- agent/consul/stream/event_publisher.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/agent/consul/stream/event_publisher.go b/agent/consul/stream/event_publisher.go index 53a1bf8dd..379bfdfa8 100644 --- a/agent/consul/stream/event_publisher.go +++ b/agent/consul/stream/event_publisher.go @@ -267,7 +267,7 @@ func (e *EventPublisher) getCachedSnapshotLocked(req *SubscribeRequest) *eventSn e.snapCache[req.Topic] = topicSnaps } - snap, ok := topicSnaps[req.Key] + snap, ok := topicSnaps[snapCacheKey(req)] if ok && snap.err() == nil { return snap } @@ -279,12 +279,16 @@ func (e *EventPublisher) setCachedSnapshotLocked(req *SubscribeRequest, snap *ev if e.snapCacheTTL == 0 { return } - e.snapCache[req.Topic][req.Key] = snap + e.snapCache[req.Topic][snapCacheKey(req)] = snap // Setup a cache eviction time.AfterFunc(e.snapCacheTTL, func() { e.lock.Lock() 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) +}