2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2020-10-19 23:32:39 +00:00
|
|
|
package submatview
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-04-26 16:20:33 +00:00
|
|
|
"errors"
|
2020-10-19 23:32:39 +00:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2021-02-23 20:28:43 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
|
2020-10-19 23:32:39 +00:00
|
|
|
"github.com/hashicorp/consul/agent/cache"
|
|
|
|
"github.com/hashicorp/consul/lib/ttlcache"
|
|
|
|
)
|
|
|
|
|
2021-04-27 19:39:26 +00:00
|
|
|
// Store of Materializers. Store implements an interface similar to
|
2021-04-28 16:01:04 +00:00
|
|
|
// agent/cache.Cache, and allows a single Materializer to fulfil multiple requests
|
2021-04-27 19:39:26 +00:00
|
|
|
// as long as the requests are identical.
|
|
|
|
// Store is used in place of agent/cache.Cache because with the streaming
|
|
|
|
// backend there is no longer any need to run a background goroutine to refresh
|
|
|
|
// stored values.
|
2020-10-19 23:32:39 +00:00
|
|
|
type Store struct {
|
2021-04-28 16:01:04 +00:00
|
|
|
logger hclog.Logger
|
|
|
|
lock sync.RWMutex
|
|
|
|
byKey map[string]entry
|
|
|
|
|
|
|
|
// expiryHeap tracks entries with 0 remaining requests. Entries are ordered
|
|
|
|
// by most recent expiry first.
|
2020-10-19 23:32:39 +00:00
|
|
|
expiryHeap *ttlcache.ExpiryHeap
|
2021-04-22 18:08:09 +00:00
|
|
|
|
|
|
|
// idleTTL is the duration of time an entry should remain in the Store after the
|
|
|
|
// last request for that entry has been terminated. It is a field on the struct
|
2021-06-01 21:49:31 +00:00
|
|
|
// so that it can be patched in tests without needing a global lock.
|
2021-04-22 18:08:09 +00:00
|
|
|
idleTTL time.Duration
|
2020-10-19 23:32:39 +00:00
|
|
|
}
|
|
|
|
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
// A Materializer maintains a materialized view of a subscription on an event stream.
|
|
|
|
type Materializer interface {
|
|
|
|
Run(ctx context.Context)
|
|
|
|
Query(ctx context.Context, minIndex uint64) (Result, error)
|
|
|
|
}
|
|
|
|
|
2021-02-22 19:29:45 +00:00
|
|
|
type entry struct {
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
materializer Materializer
|
2021-02-22 19:29:45 +00:00
|
|
|
expiry *ttlcache.Entry
|
2021-02-22 21:27:18 +00:00
|
|
|
stop func()
|
2021-02-23 17:18:44 +00:00
|
|
|
// requests is the count of active requests using this entry. This entry will
|
2021-02-22 22:43:52 +00:00
|
|
|
// remain in the store as long as this count remains > 0.
|
2021-02-23 17:18:44 +00:00
|
|
|
requests int
|
2022-08-11 09:19:36 +00:00
|
|
|
// evicting is used to mark an entry that will be evicted when the current in-
|
|
|
|
// flight requests finish.
|
|
|
|
evicting bool
|
2021-02-22 19:29:45 +00:00
|
|
|
}
|
|
|
|
|
2021-04-27 19:39:26 +00:00
|
|
|
// NewStore creates and returns a Store that is ready for use. The caller must
|
|
|
|
// call Store.Run (likely in a separate goroutine) to start the expiration loop.
|
2021-02-23 20:28:43 +00:00
|
|
|
func NewStore(logger hclog.Logger) *Store {
|
2020-10-19 23:32:39 +00:00
|
|
|
return &Store{
|
2021-02-23 20:28:43 +00:00
|
|
|
logger: logger,
|
2021-02-22 19:29:45 +00:00
|
|
|
byKey: make(map[string]entry),
|
2020-10-19 23:32:39 +00:00
|
|
|
expiryHeap: ttlcache.NewExpiryHeap(),
|
2021-04-22 18:08:09 +00:00
|
|
|
idleTTL: 20 * time.Minute,
|
2020-10-19 23:32:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-22 21:27:18 +00:00
|
|
|
// Run the expiration loop until the context is cancelled.
|
|
|
|
func (s *Store) Run(ctx context.Context) {
|
|
|
|
for {
|
|
|
|
s.lock.RLock()
|
|
|
|
timer := s.expiryHeap.Next()
|
|
|
|
s.lock.RUnlock()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
timer.Stop()
|
|
|
|
return
|
2021-04-27 19:39:26 +00:00
|
|
|
|
|
|
|
// the first item in the heap has changed, restart the timer with the
|
|
|
|
// new TTL.
|
2021-02-22 21:27:18 +00:00
|
|
|
case <-s.expiryHeap.NotifyCh:
|
|
|
|
timer.Stop()
|
|
|
|
continue
|
|
|
|
|
2021-04-27 19:39:26 +00:00
|
|
|
// the TTL for the first item has been reached, attempt an expiration.
|
2021-02-22 21:27:18 +00:00
|
|
|
case <-timer.Wait():
|
|
|
|
s.lock.Lock()
|
|
|
|
|
|
|
|
he := timer.Entry
|
|
|
|
s.expiryHeap.Remove(he.Index())
|
|
|
|
|
|
|
|
e := s.byKey[he.Key()]
|
2021-02-22 22:43:52 +00:00
|
|
|
|
2021-02-23 17:18:44 +00:00
|
|
|
// Only stop the materializer if there are no active requests.
|
|
|
|
if e.requests == 0 {
|
2022-08-11 09:19:36 +00:00
|
|
|
s.logger.Trace("evicting item from store", "key", he.Key())
|
2021-02-22 22:43:52 +00:00
|
|
|
e.stop()
|
|
|
|
delete(s.byKey, he.Key())
|
|
|
|
}
|
2021-02-22 21:27:18 +00:00
|
|
|
|
|
|
|
s.lock.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-27 19:39:26 +00:00
|
|
|
// Request is used to request data from the Store.
|
|
|
|
// Note that cache.Request is required, but some of the fields cache.RequestInfo
|
|
|
|
// fields are ignored (ex: MaxAge, and MustRevalidate).
|
2021-02-23 17:18:44 +00:00
|
|
|
type Request interface {
|
|
|
|
cache.Request
|
2021-04-27 19:39:26 +00:00
|
|
|
// NewMaterializer will be called if there is no active materializer to fulfil
|
|
|
|
// the request. It should return a Materializer appropriate for streaming
|
|
|
|
// data to fulfil this request.
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
NewMaterializer() (Materializer, error)
|
2021-04-27 19:39:26 +00:00
|
|
|
// Type should return a string which uniquely identifies this type of request.
|
|
|
|
// The returned value is used as the prefix of the key used to index
|
|
|
|
// entries in the Store.
|
2021-02-23 17:18:44 +00:00
|
|
|
Type() string
|
|
|
|
}
|
2021-02-22 19:29:45 +00:00
|
|
|
|
2020-10-19 23:32:39 +00:00
|
|
|
// Get a value from the store, blocking if the store has not yet seen the
|
|
|
|
// req.Index value.
|
|
|
|
// See agent/cache.Cache.Get for complete documentation.
|
2021-02-23 20:02:28 +00:00
|
|
|
func (s *Store) Get(ctx context.Context, req Request) (Result, error) {
|
2021-02-22 19:29:45 +00:00
|
|
|
info := req.CacheInfo()
|
2021-04-27 21:01:12 +00:00
|
|
|
key, materializer, err := s.readEntry(req)
|
2021-04-19 22:47:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return Result{}, err
|
|
|
|
}
|
2021-02-23 17:18:44 +00:00
|
|
|
defer s.releaseEntry(key)
|
2021-02-22 19:29:45 +00:00
|
|
|
|
2021-06-03 21:48:44 +00:00
|
|
|
if info.Timeout > 0 {
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
ctx, cancel = context.WithTimeout(ctx, info.Timeout)
|
|
|
|
defer cancel()
|
|
|
|
}
|
2021-02-23 19:27:24 +00:00
|
|
|
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
result, err := materializer.Query(ctx, info.MinIndex)
|
2021-06-01 21:49:31 +00:00
|
|
|
// context.DeadlineExceeded is translated to nil to match the timeout
|
|
|
|
// behaviour of agent/cache.Cache.Get.
|
2021-04-26 16:20:33 +00:00
|
|
|
if err == nil || errors.Is(err, context.DeadlineExceeded) {
|
|
|
|
return result, nil
|
|
|
|
}
|
2021-02-23 20:02:28 +00:00
|
|
|
return result, err
|
2020-10-19 23:32:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Notify the updateCh when there are updates to the entry identified by req.
|
|
|
|
// See agent/cache.Cache.Notify for complete documentation.
|
2021-02-23 19:27:24 +00:00
|
|
|
//
|
|
|
|
// Request.CacheInfo().Timeout is ignored because it is not really relevant in
|
|
|
|
// this case. Instead set a deadline on the context.
|
2020-10-19 23:32:39 +00:00
|
|
|
func (s *Store) Notify(
|
|
|
|
ctx context.Context,
|
2021-02-22 19:29:45 +00:00
|
|
|
req Request,
|
2020-10-19 23:32:39 +00:00
|
|
|
correlationID string,
|
|
|
|
updateCh chan<- cache.UpdateEvent,
|
2022-05-20 14:47:40 +00:00
|
|
|
) error {
|
|
|
|
return s.NotifyCallback(ctx, req, correlationID, func(ctx context.Context, event cache.UpdateEvent) {
|
|
|
|
select {
|
|
|
|
case updateCh <- event:
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// NotifyCallback subscribes to updates of the entry identified by req in the
|
|
|
|
// same way as Notify, but accepts a callback function instead of a channel.
|
|
|
|
func (s *Store) NotifyCallback(
|
|
|
|
ctx context.Context,
|
|
|
|
req Request,
|
|
|
|
correlationID string,
|
|
|
|
cb cache.Callback,
|
2020-10-19 23:32:39 +00:00
|
|
|
) error {
|
2021-02-22 19:29:45 +00:00
|
|
|
info := req.CacheInfo()
|
2021-04-27 21:01:12 +00:00
|
|
|
key, materializer, err := s.readEntry(req)
|
2021-04-19 22:47:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-22 19:29:45 +00:00
|
|
|
|
|
|
|
go func() {
|
2021-02-23 17:18:44 +00:00
|
|
|
defer s.releaseEntry(key)
|
2021-02-22 22:43:52 +00:00
|
|
|
|
2021-02-23 17:18:44 +00:00
|
|
|
index := info.MinIndex
|
2021-02-22 19:29:45 +00:00
|
|
|
for {
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
result, err := materializer.Query(ctx, index)
|
2021-02-22 19:29:45 +00:00
|
|
|
switch {
|
|
|
|
case ctx.Err() != nil:
|
|
|
|
return
|
|
|
|
case err != nil:
|
2021-02-23 20:28:43 +00:00
|
|
|
s.logger.Warn("handling error in Store.Notify",
|
|
|
|
"error", err,
|
|
|
|
"request-type", req.Type(),
|
|
|
|
"index", index)
|
2021-02-22 19:29:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
index = result.Index
|
2022-05-20 14:47:40 +00:00
|
|
|
cb(ctx, cache.UpdateEvent{
|
2021-02-22 19:29:45 +00:00
|
|
|
CorrelationID: correlationID,
|
|
|
|
Result: result.Value,
|
2022-08-11 09:19:36 +00:00
|
|
|
Err: err,
|
2021-06-28 21:29:23 +00:00
|
|
|
Meta: cache.ResultMeta{Index: result.Index, Hit: result.Cached},
|
2022-05-20 14:47:40 +00:00
|
|
|
})
|
2021-02-22 19:29:45 +00:00
|
|
|
}
|
|
|
|
}()
|
2020-10-19 23:32:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-27 21:01:12 +00:00
|
|
|
// readEntry from the store, and increment the requests counter. releaseEntry
|
2021-02-23 17:18:44 +00:00
|
|
|
// must be called when the request is finished to decrement the counter.
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
func (s *Store) readEntry(req Request) (string, Materializer, error) {
|
2021-02-23 17:18:44 +00:00
|
|
|
info := req.CacheInfo()
|
|
|
|
key := makeEntryKey(req.Type(), info)
|
2020-10-19 23:32:39 +00:00
|
|
|
|
|
|
|
s.lock.Lock()
|
2021-02-22 19:29:45 +00:00
|
|
|
defer s.lock.Unlock()
|
2021-02-22 22:43:52 +00:00
|
|
|
e, ok := s.byKey[key]
|
2021-02-22 19:29:45 +00:00
|
|
|
if ok {
|
2022-08-11 09:19:36 +00:00
|
|
|
if e.evicting {
|
|
|
|
return "", nil, errors.New("item is marked for eviction")
|
|
|
|
}
|
2021-02-23 17:18:44 +00:00
|
|
|
e.requests++
|
|
|
|
s.byKey[key] = e
|
2021-04-27 21:01:12 +00:00
|
|
|
return key, e.materializer, nil
|
2021-04-19 22:47:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mat, err := req.NewMaterializer()
|
|
|
|
if err != nil {
|
2021-04-27 21:01:12 +00:00
|
|
|
return "", nil, err
|
2020-10-19 23:32:39 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 21:27:18 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2022-08-11 09:19:36 +00:00
|
|
|
go func() {
|
|
|
|
mat.Run(ctx)
|
|
|
|
|
|
|
|
// Materializers run until they either reach their TTL and are evicted (which
|
|
|
|
// cancels the given context) or encounter an irrecoverable error.
|
|
|
|
//
|
|
|
|
// If the context hasn't been canceled, we know it's the error case so we
|
|
|
|
// trigger an immediate eviction.
|
|
|
|
if ctx.Err() == nil {
|
|
|
|
s.evictNow(key)
|
|
|
|
}
|
|
|
|
}()
|
2021-02-22 21:27:18 +00:00
|
|
|
|
2021-02-22 22:43:52 +00:00
|
|
|
e = entry{
|
|
|
|
materializer: mat,
|
|
|
|
stop: cancel,
|
2021-02-23 17:18:44 +00:00
|
|
|
requests: 1,
|
2021-02-22 22:43:52 +00:00
|
|
|
}
|
2021-02-22 19:29:45 +00:00
|
|
|
s.byKey[key] = e
|
2021-04-27 21:01:12 +00:00
|
|
|
return key, e.materializer, nil
|
2020-10-19 23:32:39 +00:00
|
|
|
}
|
|
|
|
|
2022-08-11 09:19:36 +00:00
|
|
|
// evictNow causes the item with the given key to be evicted immediately.
|
|
|
|
//
|
|
|
|
// If there are requests in-flight, the item is marked for eviction such that
|
|
|
|
// once the requests have been served releaseEntry will move it to the top of
|
|
|
|
// the expiry heap. If there are no requests in-flight, evictNow will move the
|
|
|
|
// item to the top of the expiry heap itself.
|
|
|
|
//
|
|
|
|
// In either case, the entry's evicting flag prevents it from being served by
|
|
|
|
// readEntry (and thereby gaining new in-flight requests).
|
|
|
|
func (s *Store) evictNow(key string) {
|
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
|
|
|
|
e := s.byKey[key]
|
|
|
|
e.evicting = true
|
|
|
|
s.byKey[key] = e
|
|
|
|
|
|
|
|
if e.requests == 0 {
|
|
|
|
s.expireNowLocked(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-23 17:18:44 +00:00
|
|
|
// releaseEntry decrements the request count and starts an expiry timer if the
|
2021-04-27 21:01:12 +00:00
|
|
|
// count has reached 0. Must be called once for every call to readEntry.
|
2021-02-23 17:18:44 +00:00
|
|
|
func (s *Store) releaseEntry(key string) {
|
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
e := s.byKey[key]
|
|
|
|
e.requests--
|
|
|
|
s.byKey[key] = e
|
|
|
|
|
|
|
|
if e.requests > 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-11 09:19:36 +00:00
|
|
|
if e.evicting {
|
|
|
|
s.expireNowLocked(key)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-23 17:18:44 +00:00
|
|
|
if e.expiry.Index() == ttlcache.NotIndexed {
|
2021-04-22 18:08:09 +00:00
|
|
|
e.expiry = s.expiryHeap.Add(key, s.idleTTL)
|
2021-02-23 17:18:44 +00:00
|
|
|
s.byKey[key] = e
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-22 18:08:09 +00:00
|
|
|
s.expiryHeap.Update(e.expiry.Index(), s.idleTTL)
|
2020-10-19 23:32:39 +00:00
|
|
|
}
|
2021-02-22 22:43:52 +00:00
|
|
|
|
2022-08-11 09:19:36 +00:00
|
|
|
// expireNowLocked moves the item with the given key to the top of the expiry
|
|
|
|
// heap, causing it to be picked up by the expiry loop and evicted immediately.
|
|
|
|
func (s *Store) expireNowLocked(key string) {
|
|
|
|
e := s.byKey[key]
|
|
|
|
if idx := e.expiry.Index(); idx != ttlcache.NotIndexed {
|
|
|
|
s.expiryHeap.Remove(idx)
|
|
|
|
}
|
|
|
|
e.expiry = s.expiryHeap.Add(key, time.Duration(0))
|
|
|
|
s.byKey[key] = e
|
|
|
|
}
|
|
|
|
|
2021-02-23 17:18:44 +00:00
|
|
|
// makeEntryKey matches agent/cache.makeEntryKey, but may change in the future.
|
|
|
|
func makeEntryKey(typ string, r cache.RequestInfo) string {
|
|
|
|
return fmt.Sprintf("%s/%s/%s/%s", typ, r.Datacenter, r.Token, r.Key)
|
2021-02-22 22:43:52 +00:00
|
|
|
}
|