From bc4d349ccf62bee5c3acdf294ce34868aa6c35bd Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Mon, 28 Jun 2021 17:29:23 -0400 Subject: [PATCH] streaming: support X-Cache-Hit header If a value was already available in the local view the request is considered a cache hit. If the materialized had to wait for a value, it is considered a cache miss. --- agent/rpcclient/health/health.go | 3 ++- agent/submatview/materializer.go | 5 +++++ agent/submatview/store.go | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/agent/rpcclient/health/health.go b/agent/rpcclient/health/health.go index 4cd7b0f4d1..9d20f3caa8 100644 --- a/agent/rpcclient/health/health.go +++ b/agent/rpcclient/health/health.go @@ -42,7 +42,8 @@ func (c *Client) ServiceNodes( if err != nil { return structs.IndexedCheckServiceNodes{}, cache.ResultMeta{}, err } - return *result.Value.(*structs.IndexedCheckServiceNodes), cache.ResultMeta{Index: result.Index}, err + meta := cache.ResultMeta{Index: result.Index, Hit: result.Cached} + return *result.Value.(*structs.IndexedCheckServiceNodes), meta, err } out, md, err := c.getServiceNodes(ctx, req) diff --git a/agent/submatview/materializer.go b/agent/submatview/materializer.go index 51402987dc..b830689e69 100644 --- a/agent/submatview/materializer.go +++ b/agent/submatview/materializer.go @@ -215,9 +215,13 @@ func (m *Materializer) notifyUpdateLocked(err error) { m.updateCh = make(chan struct{}) } +// Result returned from the View. type Result struct { Index uint64 Value interface{} + // Cached is true if the requested value was already available locally. If + // the value is false, it indicates that getFromView had to wait for an update, + Cached bool } // getFromView blocks until the index of the View is greater than opts.MinIndex, @@ -237,6 +241,7 @@ func (m *Materializer) getFromView(ctx context.Context, minIndex uint64) (Result // haven't loaded a snapshot at all yet which means we should wait for one on // the update chan. if result.Index > 0 && result.Index > minIndex { + result.Cached = true return result, nil } diff --git a/agent/submatview/store.go b/agent/submatview/store.go index 58acf5db33..07363f7403 100644 --- a/agent/submatview/store.go +++ b/agent/submatview/store.go @@ -171,7 +171,7 @@ func (s *Store) Notify( u := cache.UpdateEvent{ CorrelationID: correlationID, Result: result.Value, - Meta: cache.ResultMeta{Index: result.Index}, + Meta: cache.ResultMeta{Index: result.Index, Hit: result.Cached}, } select { case updateCh <- u: