Skip to content

Commit

Permalink
streaming: support X-Cache-Hit header
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dnephin committed Jun 28, 2021
1 parent c783917 commit bc4d349
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
3 changes: 2 additions & 1 deletion agent/rpcclient/health/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions agent/submatview/materializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion agent/submatview/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit bc4d349

Please sign in to comment.