Documentation
¶
Overview ¶
Package cache adds a session-partitioned, in-memory token cache in front of any exchange.TokenExchanger.
Cache decorates a provider (entra.Exchanger today) and implements exchange.Exchanger, so the proxy middleware and the vMCP strategy are unaware of the caching. Cached tokens are partitioned by (hash(userToken), audience): a given user's token for a given audience is reused until it is within a configurable skew of its expiry, at which point the next call re-exchanges. Different user tokens and different audiences never collide.
Typical wiring:
exchanger, err := entra.NewFromTokenURL(tokenURL, clientID, clientSecret, nil)
if err != nil { ... }
var oboExchanger exchange.Exchanger = cache.New(exchanger)
The package links only the standard library and the stdlib-only pkg/auth/obo/exchange contract.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache decorates an exchange.TokenExchanger with a session-partitioned, in-memory token cache and implements exchange.Exchanger. It is safe for concurrent use.
Concurrent misses for the same (userToken, audience) are coalesced by single-flight: only one underlying exchange runs at a time per key, and the other callers wait for and share its result. This keeps a burst of requests for one session — the common case when a cached token reaches its expiry — from stampeding the token endpoint. The shared exchange runs on its own goroutine under a context detached from any single caller (context.WithoutCancel), so one caller's cancellation cannot fail the others; each caller is still released early by its own context. See fetch / runExchange.
func New ¶
func New(delegate exchange.TokenExchanger, opts ...Option) *Cache
New returns a Cache fronting delegate. It panics if delegate is nil or if an option sets a negative skew — both are programming errors that should surface at wiring time, not on the first exchange.
func (*Cache) Exchange ¶
Exchange implements exchange.Exchanger. It returns a cached token for (userToken, audience) when one is present and not within the skew of its expiry; otherwise it delegates to the underlying TokenExchanger, caches the result (when it carries an expiry), and returns it. Concurrent misses for the same key are coalesced into a single underlying exchange.