github.com/outbrain/consul@v1.4.5/agent/cache/type.go (about)

     1  package cache
     2  
     3  import (
     4  	"time"
     5  )
     6  
     7  // Type implements the logic to fetch certain types of data.
     8  type Type interface {
     9  	// Fetch fetches a single unique item.
    10  	//
    11  	// The FetchOptions contain the index and timeouts for blocking queries. The
    12  	// MinIndex value on the Request itself should NOT be used as the blocking
    13  	// index since a request may be reused multiple times as part of Refresh
    14  	// behavior.
    15  	//
    16  	// The return value is a FetchResult which contains information about the
    17  	// fetch. If an error is given, the FetchResult is ignored. The cache does not
    18  	// support backends that return partial values. Optional State can be added to
    19  	// the FetchResult which will be stored with the cache entry and provided to
    20  	// the next Fetch call but will not be returned to clients. This allows types
    21  	// to add additional bookkeeping data per cache entry that will still be aged
    22  	// out along with the entry's TTL.
    23  	//
    24  	// On timeout, FetchResult can behave one of two ways. First, it can return
    25  	// the last known value. This is the default behavior of blocking RPC calls in
    26  	// Consul so this allows cache types to be implemented with no extra logic.
    27  	// Second, FetchResult can return an unset value and index. In this case, the
    28  	// cache will reuse the last value automatically. If an unset Value is
    29  	// returned, the State field will still be updated which allows maintaining
    30  	// metadata even when there is no result.
    31  	Fetch(FetchOptions, Request) (FetchResult, error)
    32  
    33  	// SupportsBlocking should return true if the type supports blocking queries.
    34  	// Types that do not support blocking queries will not be able to use
    35  	// background refresh nor will the cache attempt blocking fetches if the
    36  	// client requests them with MinIndex.
    37  	SupportsBlocking() bool
    38  }
    39  
    40  // FetchOptions are various settable options when a Fetch is called.
    41  type FetchOptions struct {
    42  	// MinIndex is the minimum index to be used for blocking queries.
    43  	// If blocking queries aren't supported for data being returned,
    44  	// this value can be ignored.
    45  	MinIndex uint64
    46  
    47  	// Timeout is the maximum time for the query. This must be implemented
    48  	// in the Fetch itself.
    49  	Timeout time.Duration
    50  
    51  	// LastResult is the result from the last successful Fetch and represents the
    52  	// value currently stored in the cache at the time Fetch is invoked. It will
    53  	// be nil on first call where there is no current cache value. There may have
    54  	// been other Fetch attempts that resulted in an error in the mean time. These
    55  	// are not explicitly represented currently. We could add that if needed this
    56  	// was just simpler for now.
    57  	//
    58  	// The FetchResult read-only! It is constructed per Fetch call so modifying
    59  	// the struct directly (e.g. changing it's Index of Value field) will have no
    60  	// effect, however the Value and State fields may be pointers to the actual
    61  	// values stored in the cache entry. It is thread-unsafe to modify the Value
    62  	// or State via pointers since readers may be concurrently inspecting those
    63  	// values under the entry lock (although we guarantee only one Fetch call per
    64  	// entry) and modifying them even if the index doesn't change or the Fetch
    65  	// eventually errors will likely break logical invariants in the cache too!
    66  	LastResult *FetchResult
    67  }
    68  
    69  // FetchResult is the result of a Type Fetch operation and contains the
    70  // data along with metadata gathered from that operation.
    71  type FetchResult struct {
    72  	// Value is the result of the fetch.
    73  	Value interface{}
    74  
    75  	// State is opaque data stored in the cache but not returned to clients. It
    76  	// can be used by Types to maintain any bookkeeping they need between fetches
    77  	// (using FetchOptions.LastResult) in a way that gets automatically cleaned up
    78  	// by TTL expiry etc.
    79  	State interface{}
    80  
    81  	// Index is the corresponding index value for this data.
    82  	Index uint64
    83  }