Source File
csc_integration.go
Belonging Package
github.com/redis/go-redis/v9
package redisimport ()// cscRegisterCleanups arranges for a client dropped without Close to stop its// background CSC drainer. The drainer's exit path revokes its pool's cache// coverage; the runtime cleanup itself stays non-blocking and never captures// *Client, so the wrapper remains collectible.func cscRegisterCleanups( *Client) {:= .baseClient.cscDrainHandleif == nil {return}// The weak back-reference (cscClientWeak) is published by the caller BEFORE// attachCSC starts the drainer (see NewClient), so the push-handler adapter's// canonical close reads it race-free. It is deliberately NOT set here: a write// after the drainer is already live would race that read.// Capture cscActive (a standalone *atomic.Bool, not *Client) so the cleanup// also stops clones from serving once the drainer is gone. Capture the miss// coalescer too (set during attach; nil when off): its sessions wait on// their own stop channel, so a client dropped WITHOUT Close would leak them// and everything they retain (cache, pools). stopWorkers is idempotent and// signal-only, keeping the cleanup non-blocking; the drain-cancel then// settles any queued request whose reservation would otherwise stay// IN_PROGRESS and block a cache-sharing client until StaleTimeout (channel// receive gives each request exactly one consumer).:= .baseClient.cscActive:= .baseClient.cscMissCoalescer.Load()// Capture the refresh handle too (set during attach; nil when refresh is off):// runCSCRefresher parks on its ticker/queue and holds *baseClient, so a client// dropped WITHOUT Close would leak the refresher goroutine and everything it// retains (cache, pools) — the coalescer/drainer stops below would not reach it.// signalStop is idempotent and non-blocking, matching the other stops here. The// handle is channels only (no *Client), so capturing it keeps the wrapper// collectible.:= .baseClient.cscRefreshHandle// Capture this client's refresh queue too: signalStop stops the refresher// goroutine, but with a SHARED cache+processor the invalidate handler still// holds this queue as (possibly) the active refresh binding. If a sibling// client survives, invalidations would keep feeding a stopped queue and the// shared cache's refresh-on-invalidate would silently degrade to plain// eviction. clearRefreshQueue unbinds it and restores the sibling's binding,// mirroring stopCSCRefresher on the clean Close path. A queue is only ever// created inside attachSharedTrackingCSC, which also builds the drain handle// (startBackgroundDrainer), so h.invalidateHandler is set whenever q is// non-nil; a Conn() clone bails before startCSCRefresher and never has one.:= .baseClient.cscRefreshQueueruntime.AddCleanup(, func( *cscDrainHandle) {// Order mirrors stopBackgroundDrainer (the clean Close path) but stays// fully NON-BLOCKING — a GC finalizer must never wait. Unbind this queue and// signal the refresher stop while serving is STILL ON (active==true), then// deactivate: refreshInvalidatedBatch bails once cscActive is false, so// deactivating first would make the refresher's stop-drain flush a no-op and// drop in-window hot keys for a surviving sibling on a shared cache.// signalStop only closes a channel, so the final flush runs ASYNCHRONOUSLY;// active.Store(false) lands nanoseconds later and the flush may still miss// the window — the guaranteed final-flush is the Close path, which joins the// refresher before deactivating. This reorder is the free, strictly-better// best effort for the drop-without-Close case.//// clearRefreshQueue detaches+signals the batcher but the finalizer DISCARDS// it (no join): the batcher's own run() drains and exits asynchronously. The// clean Close/self-disable paths join it instead.//// This deliberately DIVERGES from stopCSCRefresherAndCoalescer's canonical// order (coalescer-conn-release before the refresher flush): the finalizer is// signal-only and never joins, so it never waits on a refresher flush and the// connection-release ordering is moot here — the pool may already be gone. The// blocking, correctly-ordered teardown is the Close / self-disable path.if != nil && .invalidateHandler != nil {.invalidateHandler.clearRefreshQueue()}if != nil {.signalStop()}if != nil {.Store(false)}if != nil {.stopWorkers()// Retry-uncached, matching every other stop path: a clone still// alive re-runs the read on the (possibly still open) pool instead// of surfacing a spurious ErrClosed..drainQueueErr(errCSCRetryUncached)}.signalStop()}, )}// ClientSideCacheConfig configures the built-in client-side cache. Pass a// non-nil value to Options.ClientSideCacheConfig to enable caching on a RESP3// client.//// Experimental: this API may change in a minor release.type ClientSideCacheConfig = CacheConfigconst (invalidatePushName = "invalidate"// cscNamespaceSep separates fixed-width/logically-delimited namespace parts// from the command or Redis key.cscNamespaceSep = "\x00")// cscNamespacePrefix scopes a shared cache by database and fixed ACL identity.// Password rotation does not change identity; provider-backed identities are// rejected before attachment.func cscNamespacePrefix( int, string) string {return strconv.Itoa() + cscNamespaceSep +strconv.Itoa(len()) + ":" + + cscNamespaceSep}func cscNamespacedKey(, string) string {return +}// cscMissKeys returns the namespaced redis keys in args [lo, hi] for Reserve.// processCached has already checked the span with cscKeySpan and// cscKeysRenderable.//// This is called on the MISS path only. It used to run before the cache-hit// check, which meant a hit -- the 94-99% case -- allocated a raw key slice,// this slice, and one string per key, then discarded all three.func cscMissKeys( Cmder, string, , int) []string {:= make([]string, 0, -+1)for := ; <= ; ++ {= append(, cscNamespacedKey(, .stringArg()))}return}// invalidateHandler propagates RESP3 "invalidate" push notifications into the// shared client-side cache. keyPrefix scopes incoming key names so a shared// cache cannot collide across databases or fixed ACL identities.//// The binding (cache, keyPrefix) is mutable under mu: the owning client's teardown// RELEASES it (cache=nil) instead of unregistering the handler, so the handler// can stay registered protected — application code holding the processor// cannot silently unregister invalidation out from under a live client — while// a successor client on the same processor can still rebind it (see// registerInvalidateHandler).type invalidateHandler struct {mu sync.RWMutexcache CachekeyPrefix stringusers int// refresh, when set, receives evicted-but-hot entries for immediate refetch.// Feeding it must never block the invalidation-delivery path. It is the TOP// of refreshStack: clients sharing this handler each attach their own queue// and the newest attachment is active; when it clears, the next-newest live// binding is RESTORED (closing the newest owner must not sever an older// sibling's still-running refresher).refresh *cscRefreshQueue// refreshStack holds every live refresh binding in attach order (older// first). Guarded by mu. Small: one entry per client sharing the handler.refreshStack []*cscRefreshQueue// batcher offloads invalidation cache-deletes to a windowed background// goroutine (Options.ClientSideCacheInvalidationBatchWindow). Lazily started// (ensureBatcher) and nil when disabled; guarded by mu. Stopped and cleared// when the last user releases (releaseLocked), so its goroutine does not live// past the binding re-arming its timer forever; a later re-acquire starts a// fresh one (picking up the successor's window).batcher *cscInvalBatcher// invalBatchWindow is the EFFECTIVE coalescing window for the batcher above:// the strictest window folded in across attached clients (see// setInvalBatchWindow — 0/inline strictest, then smaller nonzero). 0// (default) deletes inline. invalBatchWindowSet distinguishes "no client has// attached a window yet" from an explicit 0 (inline) that must win over any// later nonzero window. Read under mu alongside cache/keyPrefix/refresh.invalBatchWindow time.DurationinvalBatchWindowSet bool}// setInvalBatchWindow folds one client's window into the shared handler's// effective window at attach time. Clients sharing a handler may configure// different windows but the handler runs ONE batcher, so the effective window// is the STRICTEST attached: smallest nonzero, with explicit zero (inline// deletes) strictest of all — tightening can never violate a client's staleness// bound, while taking the latest as-is could loosen an earlier stricter one.// Not re-loosened on close (bindings carry no identity; staying stricter costs// only efficiency). On a tighten the running batcher (window fixed at creation)// is stopped — stop() flushes, so no queued delete is lost — and the next// invalidation rebuilds via ensureBatcher.func ( *invalidateHandler) ( time.Duration) {.mu.Lock()if .invalBatchWindowSet {:= .invalBatchWindow// Strictness: 0 (inline) is strictest; among nonzero, smaller is stricter.:= ( == 0 && != 0) || ( != 0 && != 0 && < )if ! {.mu.Unlock()return}}.invalBatchWindowSet = true.invalBatchWindow =// On a tighten drop the running batcher (window fixed at creation): the next// invalidation rebuilds under the NEW (stricter) window. Detach+stop under the// lock, then join OUTSIDE it. Joining under h.mu would stall a sibling in// ensureBatcher/HandlePushNotification; the join keeps the old worker's// stop-drain from applying after the rebuild — a late apply under the looser// contract could evict an entry the stricter-window batcher just repopulated.:= .detachBatcherLocked().mu.Unlock()if != nil {.join()}}// detachBatcherLocked removes the running batcher from the handler and SIGNALS it// to stop, under h.mu; returns the detached batcher (nil when none). It never// JOINS: join() waits on the batcher's stop-drain, which must not run while h.mu is// held (it would stall a sibling on the hot path — ensureBatcher/HandlePushNotification)// and must never block the GC finalizer. Callers needing a SYNCHRONOUS teardown// (the Close path) join the returned batcher AFTER releasing h.mu; the GC finalizer// discards it (signal-only). A repointing caller (set/clearRefreshQueue) stores the// new refresh binding on the batcher BEFORE calling this, so the stop-drain offers// hot keys to the surviving refresher.func ( *invalidateHandler) () *cscInvalBatcher {:= .batcherif == nil {return nil}.batcher = nil.stop()return}// ensureBatcher lazily starts the windowed invalidation batcher. The common// case (already started) is a shared RLock; only first-start takes the write// lock, so the hot invalidation path stays cheap.func ( *invalidateHandler) () *cscInvalBatcher {.mu.RLock():= .batcher.mu.RUnlock()if != nil {return}.mu.Lock()defer .mu.Unlock()// Do not start a batcher for a released binding. releaseLocked stops+nils the// batcher under this same lock when users hits 0, so a push racing that last// release must NOT resurrect a goroutine that nothing would ever stop (once// users is 0, release() no longer runs). The caller falls back to the inline// delete path when this returns nil.if .users == 0 {return nil}// Read the window under this lock (not a caller snapshot a concurrent// tighten could supersede). 0 = batching off: return nil, caller deletes// inline.:= .invalBatchWindowif <= 0 {return nil}if .batcher == nil {.batcher = &cscInvalBatcher{window: ,// Snapshot the cache for the batcher's lifetime (⊂ the binding's:// releaseLocked stops it before clearing it) so the release-time// stop-drain still applies queued deletes after h.cache is nilled.cache: .cache,ch: make(chan cscInvalItem, 8192),wake: make(chan struct{}, 1),stopCh: make(chan struct{}),done: make(chan struct{}),}// refresh is atomic so set/clearRefreshQueue can repoint it before stop;// seed it with the current binding before the worker starts..batcher.refresh.Store(.refresh)go .batcher.run()}return .batcher}// clearRefreshQueue clears the handler's refresh binding ONLY while it still// points at q: two clients sharing one cache and push processor each attach// their own queue (last attach wins), and a closing client must not clobber// the binding of a live sibling that re-attached after it — invalidations// would keep deleting entries but silently stop feeding the survivor's// refresher.//// It returns the detached batcher (nil when none) so the caller can join() it// OUTSIDE h.mu for a synchronous teardown; the GC finalizer discards it (must not// block). See detachBatcherLocked.func ( *invalidateHandler) ( *cscRefreshQueue) *cscInvalBatcher {if == nil {return nil}.mu.Lock()defer .mu.Unlock()for , := range .refreshStack {if == {.refreshStack = append(.refreshStack[:], .refreshStack[+1:]...)break}}if .refresh != {return nil // a newer sibling owns the active binding; nothing else changes}// Restore the next-newest live binding (nil when none): the surviving// sibling's refresher keeps getting fed after the newest owner closes..refresh = nilif := len(.refreshStack); > 0 {.refresh = .refreshStack[-1]}// Repoint the batcher at the surviving binding (nil if none) BEFORE stopping it,// so the stop-drain offers evicted-hot keys to the live survivor's refresher, not// the closing owner's — whose drainer is already gone, so its offers would just be// dropped. An in-flight apply already holding the old pointer feeds its current// batch to the old queue: a bounded, benign residual.if .batcher != nil {.batcher.refresh.Store(.refresh)}return .detachBatcherLocked()}// setRefreshQueue binds q as the active refresh queue and returns the detached// batcher (nil when none) so the caller can join() it OUTSIDE h.mu — the attach// stays synchronous without holding the join under the handler lock.func ( *invalidateHandler) ( *cscRefreshQueue) *cscInvalBatcher {.mu.Lock()defer .mu.Unlock()if != nil {:= falsefor , := range .refreshStack {if == {= truebreak}}if ! {.refreshStack = append(.refreshStack, )}}if .refresh == {return nil}.refresh =// A running batcher was created with the previous binding; drop it so the next// invalidation rebuilds with the new one (stop() flushes, so no queued delete// is lost). Repoint it at the new binding first, so the stop-drain feeds hot// keys to q rather than the superseded refresher.if .batcher != nil {.batcher.refresh.Store(.refresh)}return .detachBatcherLocked()}// HandlePushNotification decodes ["invalidate", <keys>] notifications. A nil// <keys> payload is emitted on FLUSHDB/FLUSHALL and triggers a full cache flush.func ( *invalidateHandler) (context.Context, push.NotificationHandlerContext, []interface{},) error {.mu.RLock(), , := .cache, .keyPrefix, .refresh:= .invalBatchWindow.mu.RUnlock()if == nil || len() < 2 {return nil}switch payload := [1].(type) {case nil:// FLUSHDB/FLUSHALL: supersede the batcher's queued per-key deletes, then wipe// the snapshotted cache. See fullFlush for the drop/flush ordering and the// binding-pairing invariant..fullFlush()case []interface{}:// Count incoming invalidations at the choke point: one per key named in the// push, BEFORE batching/dedup/spill. Applied deletes are counted separately// (DeleteByRedisKey / deleteByRedisKeyCollectingHot) and diverge from this// under dedup — that gap is the signal (see CSCRefreshStats).if , := .(*LocalCache); {var uint64for , := range {switch .(type) {case string, []byte:++}}.invalidations.Add()}// Offload path: enqueue keys to the windowed background batcher instead of// deleting inline, so invalidation work does not steal time from the// coalescer's miss-reply reader (the low-concurrency churn p99 tail).if > 0 {if , := .(*LocalCache); {// nil when the binding was just released (users==0) or when a// concurrent window change turned batching off: fall through to// the inline delete path below rather than enqueue on a nil// batcher (which would panic).// Pair the batcher with the SNAPSHOT cache: after a last-user release +// rebind to a different cache (A->B) between the entry snapshot and here,// ensureBatcher returns B's batcher, which would delete B's entries for a// push meant for A (A would keep serving stale). fullFlush guards the same// A->B pairing. sameCache also returns false for a non-comparable cache// type, so such caches fall through to the inline delete path (correct,// just not batched).if := .ensureBatcher(); != nil && sameCache(.cache, ) {// Snapshot fetch-order ONCE for the whole push (mirrors the inline delete// path below and cscInvalItem.fetchSnap): a per-key load inside enqueue// would include a fetch reserved AFTER this push was observed but before// the loop reached that key, so apply would not treat it as newer and// could evict the fresh value / cancel its in-progress reservation.:= cscFetchSeq.Load()for , := range {var stringswitch v := .(type) {case string:=case []byte:= string()default:continue}.enqueueAt(cscNamespacedKey(, ), )}return nil}}}var []cscRefreshTarget, := .(*LocalCache)= && != nil// Snapshot the fetch-order sequence ONCE, at notification-observe time, and reuse// it for every key in this push. A live per-key load would include a fetch// reserved AFTER this push was observed but before the loop reached that key, so// collectHotAndDelete would not treat it as newer and would evict the fresh value// / cancel its in-progress reservation (mirrors cscInvalItem.fetchSnap, taken at// enqueue; see cacheEntry.fetchSeq).:= cscFetchSeq.Load()for , := range {var stringswitch v := .(type) {case string:=case []byte:= string()default:continue}:= cscNamespacedKey(, )if ! {if != nil {// *LocalCache with refresh OFF: still honor the fetch-order guard// (mirrors the batcher's !canRefresh branch), or a delayed invalidation// would delete/cancel a miss reserved AFTER this push was observed// (fetchSeq > fetchSnap) and wake coalesced waiters as duplicate misses.// cscInvalNoHorizon = no refresh horizon; discard collected targets._ = .deleteByRedisKeyCollectingHot(, cscInvalNoHorizon, , nil)} else {// Non-*LocalCache: no per-entry fetch sequence to compare; plain delete..DeleteByRedisKey()}continue}// Inline (window==0) path: observe and delete synchronously. An entry with a// fetchSeq greater than the observe-time snapshot was reserved by a refetch// issued after this push and is correctly kept (see cacheEntry.fetchSeq).= .deleteByRedisKeyCollectingHot(, .sinceToken.Load(), , [:0])for := range {.offer([])}}}return nil}// fullFlush wipes the snapshotted cache for a FLUSHDB/FLUSHALL (nil-payload)// invalidation and supersedes the batcher's queued per-key deletes.//// drop() before Flush() bumps the batcher epoch, so anything already enqueued// (pre-flush, redundant by the flush) is skipped at apply, while an invalidation// racing in from another tracked connection AFTER this point carries the new epoch// and still applies — its post-flush delete must not be lost.//// Drop the batcher ONLY when it still belongs to the cache being flushed// (sameCache(h.cache, cache)). A same-cache batcher rebuild (setInvalBatchWindow /// set/clearRefreshQueue, all under h.mu.Lock) must have its FRESH batcher dropped, or// the new batcher's queued deletes survive the flush and evict post-flush// repopulations. But a last-user release + rebind to a DIFFERENT cache (A->B) between// the caller's entry snapshot and this RLock leaves h.batcher = B's while cache = A;// dropping B's batcher would bump B's epoch and skip B's queued deletes while only A is// flushed, so B would serve stale (#3989). The sameCache guard drops the batcher only// when the binding is unchanged. sameCache (not ==) also avoids a panic when the cache's// dynamic type is non-comparable; for such a type it returns false, so the batcher is// not dropped on flush — a bounded spurious miss, correct versus a crash.//// Flush the CACHE SNAPSHOT, not the live h.cache: a last-user releaseLocked can nil// h.cache, and a guarded `if h.cache != nil` would then silently skip the wipe. RLock// (not Lock) suffices — it blocks the write-locked rebuilds — and drop()/Flush() take// their own locks, not h.mu, so there is no lock-order cycle.func ( *invalidateHandler) ( Cache) {.mu.RLock()defer .mu.RUnlock()if sameCache(.cache, ) && .batcher != nil {.batcher.drop()}.Flush()}func ( *invalidateHandler) () {.mu.Lock()var *cscInvalBatcherif .users > 0 {= .releaseLocked()}.mu.Unlock()// Join OUTSIDE h.mu: the batcher's stop-drain must not run under the handler// lock (it would stall a sibling on the hot path). release() is reached only from// the drainer goroutine's exit (Close or self-disable), never the GC finalizer, so// blocking here is fine and gives Close a synchronous, no-straggler teardown.if != nil {.join()}}// releaseLocked drops one handler user and, on the last release, tears the binding// down. It DETACHES and signals the batcher but does not join it (see// detachBatcherLocked); it returns the detached batcher so release() can join// OUTSIDE h.mu. Returns nil while other users remain.func ( *invalidateHandler) () *cscInvalBatcher {.users--if .users != 0 {return nil}.cache = nil.keyPrefix = ""// Stop the windowed batcher so its goroutine does not outlive the binding.// Detached+signalled here; release() joins it outside the lock, so the last user// closing gets a synchronous teardown (on a shared injected cache a late apply// could otherwise evict an entry a successor client just repopulated). A later// re-acquire starts a fresh batcher via ensureBatcher.:= .detachBatcherLocked()// A fresh binding folds in its own window; do not inherit this one's..invalBatchWindow = 0.invalBatchWindowSet = false// Clear the refresh bindings with the binding itself: a client dropped// without Close never runs clearRefreshQueue, and a successor reusing// this handler must not inherit (or later restore) a dead queue whose// consumer is gone — hot entries offered there would vanish silently..refresh = nil.refreshStack = nilreturn}// sameCache compares Cache interface values without panicking when an// implementation uses a non-comparable value type.func sameCache(, Cache) bool {if == nil || == nil {return == nil && == nil}:= reflect.TypeOf()return == reflect.TypeOf() && .Comparable() && ==}func isNilCache( Cache) bool {if == nil {return true}:= reflect.ValueOf()switch .Kind() {case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer, reflect.Slice:return .IsNil()default:return false}}// errInvalidateHandlerBound: piggybacking on a handler bound to a live// different cache would leave the new cache uninvalidated.var errInvalidateHandlerBound = errors.New(`csc: a different "invalidate" push handler is already registered`)// bindTo binds the handler to (cache, keyPrefix). Success when that is already the// binding (a derived Client.Conn sharing the parent's processor and cache) or// when the handler was released by a previous owner's teardown (rebind);// errInvalidateHandlerBound otherwise.func ( *invalidateHandler) ( Cache, string) error {.mu.Lock()defer .mu.Unlock()switch {case sameCache(.cache, ) && .keyPrefix == :.users++return nilcase .cache == nil:.cache, .keyPrefix = ,.users = 1return nildefault:return errInvalidateHandlerBound}}// lookupInvalidateHandler returns the processor's CSC invalidate handler, nil// when absent or foreign.func lookupInvalidateHandler( push.NotificationProcessor) *invalidateHandler {if == nil {return nil}, := .GetHandler(invalidatePushName).(*invalidateHandler)return}func registerInvalidateHandler( push.NotificationProcessor, Cache, string) error {if == nil || == nil {return nil}if := .GetHandler(invalidatePushName); != nil {, := .(*invalidateHandler)if ! {return errInvalidateHandlerBound}return .bindTo(, )}// VoidProcessor (RESP2) returns an error here; the caller treats it as// "CSC not available" rather than fatal. Registered PROTECTED: application// code holding the processor must not be able to unregister invalidation// under a live client (that would serve unbounded-stale hits with no// signal); owner teardown releases the BINDING instead of the handler.:= .RegisterHandler(invalidatePushName, &invalidateHandler{cache: ,keyPrefix: ,users: 1,}, true)if == nil {return nil}// Another client can register the same protected handler between GetHandler// and RegisterHandler. Re-read it and accept the compatible binding.if := .GetHandler(invalidatePushName); != nil {, := .(*invalidateHandler)if ! {return errInvalidateHandlerBound}return .bindTo(, )}return}// attachCSC dispatches to the invalidation strategy in// Options.ClientSideCacheStrategy. Safe with a nil cache; on failure c.csc stays// nil and commands fall back to normal round-trips. Adding a strategy: a new// CSCStrategy constant plus cases in Options.init and here.func ( *baseClient) ( context.Context, Cache) {if isNilCache() || .opt.Protocol != 3 {return}// Credential providers may return a different ACL identity over the// client's lifetime (or per context/connection), while the cache namespace// is fixed when the client is created. Fixed credentials remain safe because// the ACL username is included in the length-delimited namespace below.if .opt.StreamingCredentialsProvider != nil ||.opt.CredentialsProviderContext != nil ||.opt.CredentialsProvider != nil {internal.Logger.Printf(,"redis: client-side caching is disabled with credential providers")return}.cscKeyPrefix = cscNamespacePrefix(.opt.DB, .opt.Username)switch .opt.ClientSideCacheStrategy {case CSCStrategySharedTracking:.attachSharedTrackingCSC(, )default:// Options.init clamps unknown strategies to SharedTracking; delegate anyway..attachSharedTrackingCSC(, )}}// attachSharedTrackingCSC wires SharedTracking: one shared cache, per-conn CLIENT// TRACKING, a background drainer, and the owning-conn eviction hook. DB-0 only:// tracking is bound to the conn's DB and a runtime SELECT does not re-key it.func ( *baseClient) ( context.Context, Cache) {if .opt.DB != 0 {internal.Logger.Printf(,"csc: client-side caching is restricted to DB 0; disabling CSC for client configured with DB=%d. "+"Use one client per DB if you need caching against non-zero databases.", .opt.DB)return}// A pooler without idle-conn draining (e.g. Client.Conn's StickyConnPool)// can't apply buffered invalidations, so stay uncached.if , := .connPool.(idleConnDrainer); ! {return}// The lifecycle hook serializes cache publication with connection removal// and socket replacement. Without it, a reply can become visible after its// tracking coverage is gone., := .connPool.(poolHookSupport)if ! || !.SupportsPoolHooks() {return}if := registerInvalidateHandler(.pushProcessor, , .cscKeyPrefix); != nil {internal.Logger.Printf(, "csc: failed to register invalidate handler: %v", )return}// Thread the invalidation-batch window from Options before any push can// arrive, so the batcher (if enabled) sees the configured window on the very// first invalidation rather than a zero default.if := lookupInvalidateHandler(.pushProcessor); != nil {.setInvalBatchWindow(.opt.ClientSideCacheInvalidationBatchWindow)}.csc =.registerConnEvictHook(, ).startBackgroundDrainer().startCSCRefresher().startCSCMissCoalescer()}// cscHook returns the shared evict-on-remove hook, nil when CSC is off.func ( *baseClient) () *cscEvictOnRemoveHook {, := .cscPoolHook.(*cscEvictOnRemoveHook)return}// cscInstallConnCloseHook evicts cn's owned entries on any close — including the// ConnMaxLifetime/idle retirement path (CloseConn) that bypasses the OnRemove// hook — so entries don't outlive the server tracking dropped at close. Uses the// onCscClose slot so it doesn't clobber streaming-credentials cleanup.func ( *baseClient) ( *pool.Conn) {.SetOnCscClose(func() error {.cscOnConnClose(.GetID())return nil})}// cscInstallConnReinitHook invalidates the old socket's cache coverage before// SetNetConnAndInitConn replaces it. The later init can then safely enable// tracking for the new socket without a post-swap publication window.func ( *baseClient) ( *pool.Conn) {.SetOnCscReinit(func() {.cscEvictOwnedEntries(.GetID())})}// cscOnConnClose evicts a closing conn's entries: via the shared hook (which// records the removed-ring, closing the close-before-fulfill race), else scoped// EvictByConn on the owning cache.func ( *baseClient) ( uint64) {if := .cscHook(); != nil {.markRemoved()return}if .csc != nil {.csc.EvictByConn()}}// poolHookSupport is the pool capability SharedTracking needs to serialize// cache publication with connection removal and reinitialization.type poolHookSupport interface {AddPoolHook(hook pool.PoolHook)RemovePoolHook(hook pool.PoolHook)SupportsPoolHooks() bool}// cscEvictOnRemoveHook evicts a connection's owned entries when the pool removes// it (the server stops delivering their invalidations — Window 2), and tracks// per-conn init generations so fulfillCached can catch a value whose owning// conn was removed or re-initialized mid-fetch.type cscEvictOnRemoveHook struct {evictor Cachemu sync.Mutex// initGen counts a live conn's socket (re)initializations: bumped by// cscEvictOwnedEntries before its eviction (first init included, so every// serving conn has gen >= 1), deleted on removal/close. fulfillCached// compares it with the generation captured at reply time.initGen map[uint64]uint64}func ( *cscEvictOnRemoveHook) ( context.Context, *pool.Conn, bool) (bool, error) {return true, nil}func ( *cscEvictOnRemoveHook) ( context.Context, *pool.Conn) (, bool, error) {return true, false, nil}func ( *cscEvictOnRemoveHook) ( context.Context, *pool.Conn, error) {if == nil {return}.markRemoved(.GetID())}// markRemoved forgets connID's generation, then evicts. Forgetting before// evicting lets a racing fulfillCached see the change (a served conn's captured// generation is >= 1, an absent entry reads 0) and drop an entry created after// the eviction — closing the close-before-fulfill race.func ( *cscEvictOnRemoveHook) ( uint64) {.forgetConn().evictor.EvictByConn()}// bumpInitGen advances connID's coverage generation. On reinit it is called by// the pre-swap hook, before the old socket and its server-side tracking table// are replaced.func ( *cscEvictOnRemoveHook) ( uint64) {.mu.Lock()if .initGen == nil {.initGen = make(map[uint64]uint64)}.initGen[]++.mu.Unlock()}// invalidateConnCoverage revokes all cache coverage associated with connID.// Bumping before eviction also rejects an in-flight fetch that completed on the// connection just before it left the parent's invalidation drainer.func ( *cscEvictOnRemoveHook) ( uint64) {.bumpInitGen().evictor.EvictByConn()}// initGenOf returns connID's current init generation (0 if never bumped).func ( *cscEvictOnRemoveHook) ( uint64) uint64 {.mu.Lock()defer .mu.Unlock()return .initGen[]}// forgetConn drops connID's init-generation entry: the conn was removed/closed,// or its init failed before ever serving (the pubsub path would otherwise leak// the entry — no OnRemove hook, close hook not yet installed).func ( *cscEvictOnRemoveHook) ( uint64) {.mu.Lock()delete(.initGen, ).mu.Unlock()}// fulfillOwnedIfCovered linearizes the final coverage check with connection// removal/re-init generation changes. Holding h.mu through FulfillOwned means// either the old generation is rejected before the placeholder becomes valid,// or publication wins first and the subsequent lifecycle path evicts it before// closing/replacing the tracked socket.func ( *cscEvictOnRemoveHook) (string,, , uint64,[]byte,) bool {.mu.Lock()defer .mu.Unlock()if .initGen[] != {return false}return .evictor.FulfillOwned(, , , )}// invalidateAllCoverage revokes every connection generation known to this// client's pool and evicts the entries those connections own. Incrementing// instead of deleting keeps in-flight fetches that captured an old generation// from publishing after a drainer stops.func ( *cscEvictOnRemoveHook) () {.mu.Lock():= make([]uint64, 0, len(.initGen))for := range .initGen {.initGen[]++= append(, )}.mu.Unlock()for , := range {.evictor.EvictByConn()}}// registerConnEvictHook wires the required OnRemove eviction hook.func ( *baseClient) ( Cache, poolHookSupport) {:= &cscEvictOnRemoveHook{evictor: , initGen: make(map[uint64]uint64)}.AddPoolHook().cscPoolHook =}// cscEvictOwnedEntries evicts connID's entries on first init or immediately// before a reinit/handoff replaces the socket and its tracking table. It// prefers the shared hook (so Conn/Tx, which carry it but have a nil csc, still// evict from the parent cache). Scoped only — no removed-ring (the conn keeps// serving, and the ring never ages out); the fulfill-vs-re-init race is closed// by the init-generation bump instead. No custom-cache flush (this also runs on// first init).func ( *baseClient) ( uint64) {if := .cscHook(); != nil {.invalidateConnCoverage()return}if .csc == nil {return}.csc.EvictByConn()}// newStickyConnPool creates a derived sticky pool and revokes the claimed// connection's parent-cache ownership before it becomes unreachable to the// parent's idle-connection drainer.func ( *baseClient) () *pool.StickyConnPool {:= pool.NewStickyConnPool(.connPool)if := .cscHook(); != nil {.SetOnFirstConn(func( *pool.Conn) {if != nil {.invalidateConnCoverage(.GetID())}})}return}// cscFetchCapture receives, from the successful attempt's reply read — while// the serving connection is still held — everything the CSC fetch path needs to// attribute the cached entry: the raw RESP reply, the conn id, and the conn's// CSC init generation. The generation must be captured before the conn is// released: a handoff queued at Put can re-init the socket (bumping the// generation) before fulfillCached runs.type cscFetchCapture struct {raw []byteconnID uint64initGen uint64// key/token: the reservation a background refetch (refresh or miss-coalesce)// must fulfil. Unused by the single-command path, which passes them to// fulfillCached explicitly.key stringtoken uint64}// cscConnInitGen returns connID's CSC init generation, captured by _process at// reply time (while the conn is still held) and compared by fulfillCached via// fulfillOwnedIfCovered. Zero without an active evict-on-remove hook.func ( *baseClient) ( uint64) uint64 {if := .cscHook(); != nil {return .initGenOf()}return 0}// cscForgetConn drops connID's init-generation entry when initialization does// not establish tracked coverage, either because init failed or tracking was// rejected and CSC was disabled.func ( *baseClient) ( uint64) {if := .cscHook(); != nil {.forgetConn()}}// errClientTrackingWithCSC rejects CLIENT TRACKING on clients with built-in CSC// (see the guards in baseClient.process and generalProcessPipeline). The raw// escape hatches — Do(ctx, "client", "tracking", ...) with string or []byte// args, and pipelines — are also caught: the guard matches on the command's// leading args, not the typed method.var errClientTrackingWithCSC = errors.New("redis: CLIENT TRACKING is not allowed when client-side caching is enabled",)// errSelectWithCSC rejects runtime SELECT on clients with built-in CSC. Cache// keys use Options.DB, while SELECT mutates only the chosen pool connection.var errSelectWithCSC = errors.New("redis: SELECT is not allowed when client-side caching is enabled",)// errAuthWithCSC rejects runtime authentication because it can change one// connection's ACL identity without changing the client's fixed cache namespace.var errAuthWithCSC = errors.New("redis: AUTH is not allowed when client-side caching is enabled",)// errHelloWithCSC rejects HELLO with arguments because it can switch a tracked// connection out of RESP3 (and can also change authentication).var errHelloWithCSC = errors.New("redis: HELLO with arguments is not allowed when client-side caching is enabled",)// errResetWithCSC rejects RESET because it disables tracking and switches the// connection to RESP2.var errResetWithCSC = errors.New("redis: RESET is not allowed when client-side caching is enabled",)// errSubscribeWithCSC rejects raw subscriptions on the ordinary pool. The// typed Subscribe methods use dedicated PubSub connections and remain allowed.var errSubscribeWithCSC = errors.New("redis: SUBSCRIBE is not allowed on pooled connections when client-side caching is enabled",)// cscCommandError rejects commands that can make a pooled connection's state// diverge from the assumptions used by CSC.func ( *baseClient) ( Cmder) error {// The successful attachment signal is shared with derived clients.// initConn's internal command wrapper is exempt during library setup.if !.cscTrackingRequested() || .allowClientTracking {return nil}switch {case isClientTrackingCmd():return errClientTrackingWithCSCcase isSelectCmd():return errSelectWithCSCcase isAuthCmd():return errAuthWithCSCcase isProtocolChangingHelloCmd():return errHelloWithCSCcase isResetCmd():return errResetWithCSCcase isSubscribeCmd():return errSubscribeWithCSCdefault:return nil}}// cscDrainHandle owns the drainer lifecycle and serializes client teardown.// stop signals shutdown; done is closed on exit so Close can join.type cscDrainHandle struct {stop chan struct{}done chan struct{}stopOnce sync.OnceteardownOnce sync.OnceworkersStopOnce sync.OncehandlerCloseOnce sync.OncecloseOnce sync.OncecloseErr errorinvalidateHandler *invalidateHandler// workersMu/workersTornDown close the race between// stopCSCRefresherAndCoalescer's one-time teardown and// startCSCRefresher/startCSCMissCoalescer's publish step (a bot review// flagged this: async conn init can call disableCSCServing, which the// drainer goroutine observes on ITS OWN tick and reacts to with the same// teardown Close uses — and that tick can in principle land concurrently// with construction). workersStopOnce alone is not enough: it only// guarantees the teardown body runs once, not that it runs AFTER a// worker exists to be stopped. A start function checks workersTornDown// under this lock immediately before publishing its queue/handle; if// teardown already ran, it declines to start instead of creating a// goroutine workersStopOnce can never be Do'd again to stop. See both// start functions and stopCSCRefresherAndCoalescer's body.//// startBackgroundDrainer (attachSharedTrackingCSC's first call, before// either start function) needs no matching guard: it is what allocates// this struct and assigns it to c.cscDrainHandle, so no teardown path can// observe a non-nil handle — let alone race this lock — until after it// returns. Every teardown entry point (Close, the drainer's own// self-disable) reads c.cscDrainHandle first and no-ops on nil.workersMu sync.MutexworkersTornDown bool}// signalStop closes stop at most once (so Close and the AddCleanup safety net// can't double-close) and does not join — a GC cleanup must not block.func ( *cscDrainHandle) () {.stopOnce.Do(func() { close(.stop) })}// cscHandlerClient is exposed only through the background drainer's handler// context. Close must return before the handler does, otherwise it would wait// for the drainer goroutine that is currently invoking the handler.type cscHandlerClient struct {*baseClient}// closeCanonical closes through the canonical *Client wrapper while it is// still alive — Client.Close also stops the cached autopipeliners, which// baseClient.Close does not, so closing only the baseClient would leave flush// goroutines running against closed pools. Falls back to baseClient.Close if// the wrapper was already collected (weak ref: see baseClient.cscClientWeak).func ( cscHandlerClient) () error {if := .cscClientWeak.Value(); != nil {return .Close()}return .baseClient.Close()}func ( cscHandlerClient) () error {:= .cscDrainHandleif == nil {return .closeCanonical()}.handlerCloseOnce.Do(func() {// Do NOT deactivate serving or signal the drainer stop here. This runs on// the drainer goroutine (a custom push handler called Close), so the// canonical close MUST be async — but let IT drive the teardown so// stopBackgroundDrainer stops the coalescer and refresher in the canonical// order with cscActive STILL TRUE, and the refresher's stop-drain flush// re-fetches the in-window invalidated keys. Preemptively storing// cscActive=false here made that flush a no-op (refreshInvalidatedBatch// bails on the inactive check), dropping the last collection window — the// same class the GC finalizer path was reordered to fix. The async close// reaches deactivate itself, after the flush; the drainer keeps running the// harmless idle drain until stopBackgroundDrainer signals its stop.go func() {if := .closeCanonical(); != nil {internal.Logger.Printf(context.Background(), "csc: deferred client close failed: %v", )}}()})return nil}// cscMinDrainInterval floors a user-supplied DrainInterval: sub-millisecond// timers are unreliable (https://github.com/golang/go/issues/53824).const cscMinDrainInterval = time.Millisecond// cscDrainInterval returns DrainInterval clamped to cscMinDrainInterval, or the// default (cscDrainSkipWindow) when unset.func ( *baseClient) () time.Duration {if := .opt.ClientSideCacheConfig; != nil && .DrainInterval > 0 {if .DrainInterval < cscMinDrainInterval {return cscMinDrainInterval}return .DrainInterval}return cscDrainSkipWindow}// idleConnDrainer is the pooler capability the drainer needs (*pool.ConnPool has// it). attachSharedTrackingCSC leaves a pooler without it uncached, rather than// serve entries nothing would invalidate.type idleConnDrainer interface {DrainIdleConns(ctx context.Context, st *pool.DrainState, fn func(cn *pool.Conn) error)}// startBackgroundDrainer launches the per-client invalidation drainer: each tick// runs one pool.DrainIdleConns pass, draining idle conns' buffered push frames.// No-op for poolers that don't implement idleConnDrainer.func ( *baseClient) () {, := .connPool.(idleConnDrainer)if ! {return}if .cscDrainHandle != nil {return // already running (startBackgroundDrainer runs once, in NewClient)}:= &cscDrainHandle{stop: make(chan struct{}),done: make(chan struct{}),invalidateHandler: lookupInvalidateHandler(.pushProcessor),}.cscDrainHandle =:= &atomic.Bool{}.Store(true).cscActive =:= .cscDrainInterval()// Custom-processor drain errors are connection-fatal (drainPushNotifications),// so a PERSISTENTLY failing custom processor would turn every tick into a// conn removal + redial — a sustained dial storm. Damping: after// cscDrainCustomErrCap consecutive fatal custom-processor drains, disable// CSC serving and stop the drainer (with one log line) instead of churning.// Built-in processor errors are real conn desyncs and are never damped., := .pushProcessor.(*push.Processor)go func() {defer func() {// Self-disable exits (custom-processor damping, or a RESP3/tracking// downgrade flipping cscActive that a tick then observes) run the SAME// refresher+coalescer teardown as the clean Close path, so a client that// KEEPS RUNNING after turning CSC off does not leak those goroutines for// its life — the refresher parked on its window, and a coalescer session// still holding a pool connection (F1). stopCSCRefresherAndCoalescer is// idempotent with the Close path (workersStopOnce), and it joins the// refresher and coalescer goroutines — never THIS drainer — so running it// here cannot self-block. It also deactivates serving and UNBINDS this// client's refresh queue (stopCSCRefresher -> clearRefreshQueue), which on// a shared cache+processor restores a surviving sibling's binding, so this// defer no longer needs its own active.Store(false)/clearRefreshQueue.// Both self-disable paths reach here with cscActive already false// (disableCSCServing set it; the damping branch above deactivates before// returning), so the helper's refresher flush no-ops here — the ordering is// kept only because it is free and uniform with the Close path..stopCSCRefresherAndCoalescer()if .cscPoolHook != nil {if , := .connPool.(poolHookSupport); {.RemovePoolHook(.cscPoolHook)}}if := .cscHook(); != nil {.invalidateAllCoverage()}if .invalidateHandler != nil {.invalidateHandler.release()}close(.done)}():= time.NewTicker()defer .Stop()// st persists round/visited across ticks; single-goroutine, no lock.var pool.DrainState:= 0:= func( *pool.Conn) error {, := .drainPushNotifications()switch {case != nil:++case :// A successful processor invocation resets consecutive// failures. A conn skipped without invoking the processor —// including a clean replacement after a fatal drain — does// not reset the counter.= 0}return}for {select {case <-.stop:returncase <-.C:if !.Load() {return}// ctx bounds the whole pass; the drain read has its own hard deadline., := context.WithTimeout(context.Background(), /2).DrainIdleConns(, &, )()if ! && >= cscDrainCustomErrCap {internal.Logger.Printf(context.Background(),"csc: disabling client-side caching: the custom push notification processor failed %d consecutive drains "+"(each failure removes a connection because the reader may be mid-frame); "+"caching cannot be kept fresh safely with this processor", )// Deactivate BEFORE the defer's teardown so nothing else (ordinary reads// via processCached, a normal in-flight flush's refreshInvalidatedBatch// bail) keeps treating a persistently-broken custom processor as still// serving. The refresher's OWN stop-drain flush no longer depends on this// timing either way: it now unconditionally skips its refetch when// stopping (see runCSCRefresher's h.stop case), so the exit stays prompt// regardless. The defer still stops and joins both workers (F1)..Store(false)return}}}}()}// disableCSCServing atomically stops cache hits and revokes all tracked// connection coverage. The owner drainer observes the shared active flag on its// next tick, including when a derived Conn or Tx discovered the incompatibility.func ( *baseClient) ( context.Context, string) {:= .cscActiveif == nil || !.CompareAndSwap(true, false) {return}if := .cscHook(); != nil {.invalidateAllCoverage()}internal.Logger.Printf(, "csc: disabling client-side caching: %s", )}// stopBackgroundDrainer joins the drainer goroutine and flushes an owned cache.// The drainer's exit path releases its handler binding and pool hook, including// when it stops itself. Owner-only: clones have no handle and return early.// The fields are never cleared here — fulfillCached reads cscPoolHook on the hot// path, so niling under a concurrent Close would race; teardownOnce makes repeat// Close idempotent instead.func ( *baseClient) () {:= .cscDrainHandleif == nil {return}.teardownOnce.Do(func() {// Stop the coalescer and refresher in the shared canonical order (coalescer// connection released first, then the refresher's final flush runs while// serving is still active, then deactivate). Idempotent with the drainer's// own self-disable defer via workersStopOnce. The pool is still open here// (closeResources tears it down after this), so the final refetch can run..stopCSCRefresherAndCoalescer().signalStop()<-.done// The drainer's exit defer revoked and evicted this pool's coverage// before closing done, including for injected caches shared elsewhere.if .cscOwnsCache && .csc != nil {.csc.Flush()}})}// stopCSCRefresherAndCoalescer tears down the reader-miss coalescer and the// refresh-on-invalidate goroutine in the one order that satisfies every teardown// constraint, exactly once. It is shared by the clean Close path// (stopBackgroundDrainer) and the SELF-DISABLE exit (the drainer goroutine's// defer), so a client that turns CSC off itself stops these goroutines instead of// leaking them for its lifetime (F1). workersStopOnce makes the two callers// idempotent and race-free — whichever reaches it first runs the body, the other// is a no-op — so cscRefreshHandle/cscMissCoalescer are never torn down twice// concurrently. Neither caller is the refresher or a coalescer goroutine, so the// joins below never self-block.//// Order:// 1. Stop the coalescer FIRST, while cscActive is still TRUE. stopCSCMissCoalescer// swaps its pointer to nil (new misses take the ordinary pooled path), signals// its sessions, and JOINS them — RELEASING the held pool connection. On a small// pool (PoolSize 1) under continuous miss traffic the coalescer can otherwise// hold the only connection, so the refresher's final flush in step 2 would wait// its full per-chunk deadline for every chunk (~160s Close stall) and lose the// warming (F3). Stopping the coalescer with serving STILL ACTIVE is safe: every// fetch stop path returns errCSCRetryUncached and every session stop path settles// the same (settleErr tags all other failures cscSessionError), so processCached// re-runs each read on the still-open pool — a caller never sees a raw ErrClosed.// A teardown-window miss then takes the ordinary pooled path and contends with// the step-2 flush for the connection, which is strictly better than the coalescer// holding it (bounded by one RTT per miss, not the session's idle/recycle hold).// This is why the earlier "deactivate before stopping the coalescer" step is gone:// the errCSCRetryUncached backstop it relied on already makes deactivate-first// unnecessary, and it conflicted with releasing the connection before the flush.// 2. Stop the refresher. Its stop-drain flush no longer re-fetches the in-window// invalidated keys over the network at all (runCSCRefresher's h.stop case):// the drainer defer's invalidateAllCoverage revokes this pool's coverage and// evicts every entry attributed to this client's refresh connection right// after (pre-existing; see TestStopBackgroundDrainerEvictsSharedCacheCoverage),// so a refetch here would only be discarded a moment later — it used to run// anyway and could cost thousands of pointless round trips against a healthy// server (codex #3989 P1). The targets are abandoned instead: counted into// RefreshFailed, left evicted for a reader to repopulate. Because the flush no// longer touches the network, the coalescer-first order in step 1 (freeing the// pool connection) is no longer LOAD-BEARING for this step, but stays: it is// still the one canonical order for Close and the self-disable defer, and// still never serves a stale value.// 3. Deactivate serving.//// STARTUP WINDOW: attachSharedTrackingCSC launches the drainer before it assigns// cscRefreshHandle/cscMissCoalescer, so this can in principle run (via the// drainer's self-disable defer, reacting to a HELLO/tracking downgrade an// async conn init observed) WHILE startCSCRefresher/startCSCMissCoalescer are// still constructing. workersStopOnce alone only guarantees body runs once —// it does not guarantee a worker exists yet to be stopped by it. The// workersMu/workersTornDown flag below (set FIRST, before either stop call)// closes that gap: a start function checks the flag under the same lock// immediately before publishing, so either it sees teardown hasn't happened// yet and publishes (and this body's later stop call correctly joins it), or// it sees the flag already set and declines to start at all — never leaving// a worker workersStopOnce can no longer reach.func ( *baseClient) () {// Owner-only. Every worker (drainer, refresher, coalescer) is created together// under a cscDrainHandle; a caller without one is a clone (clone() copies neither// the handle nor the workers, see redis.go) or a client where CSC never attached,// so it has nothing of its own to stop. Running body() here would call// stopCSCRefresher, whose read-then-nil of cscRefreshHandle is UNSYNCHRONIZED// (single-owner by design), racing the real owner's teardown.:= .cscDrainHandleif == nil {return}:= func() {.workersMu.Lock().workersTornDown = true.workersMu.Unlock().stopCSCMissCoalescer().stopCSCRefresher()if .cscActive != nil {.cscActive.Store(false)}}.workersStopOnce.Do()}// cscGet reads a cached value, skipping the defensive copy when the cache is// the built-in one.//// LocalCache.Get copies because Cache is a public interface whose []byte// return a third-party implementation's caller may retain. The built-in read// path does not retain it: every caller hands the bytes straight to// applyCachedReply, which decodes and drops them. Pairing the built-in cache// with the built-in read path is therefore the one place the copy is provably// unnecessary -- the same "only the built-in *LocalCache" gate miss coalescing// already uses.//// The returned slice must not be retained or mutated.func ( *baseClient) ( context.Context, string) ([]byte, bool) {if , := .csc.(*LocalCache); {return .getShared(, )}return .csc.Get(, )}// applyCachedReply populates cmd from a previously captured raw RESP reply by// replaying it through the command's own readReply.//// The generic path allocates a bytes.Reader and a proto.Reader (with its bufio// buffer) per hit, then re-parses. applyCachedFast skips both for the two// reply shapes that dominate the cacheable command set.func applyCachedReply( Cmder, []byte) error {if , := applyCachedFast(, ); {return}return .readReply(proto.NewReaderSize(bytes.NewReader(), len()+1))}// applyCachedFast decodes the simple single-frame replies directly into the// command, returning done=false for anything it does not fully recognise so// the caller falls back to the generic reader.//// Only shapes whose decoded value is an immutable Go string are handled:// a blob string into *StringCmd (get, getrange, hget, ...) and a status into// *StatusCmd (type). Container replies -- mget, hgetall, smembers, zrange and// the rest of the allow-list -- keep re-parsing, deliberately: their decoded// form is a slice or map, and handing the same one to two callers would let// either mutate what the other reads, and the cached entry with it. For those,// re-parsing IS the defensive copy.//// raw must not be retained: it aliases the cache entry's value (see// LocalCache.getShared). Everything produced here is a fresh string.func applyCachedFast( Cmder, []byte) ( error, bool) {if len() < 3 || [len()-2] != '\r' || [len()-1] != '\n' {return nil, false}switch [0] {case proto.RespNil: // RESP3 null: "_\r\n"if len() != 3 {return nil, false}switch c := .(type) {case *StringCmd:.SetVal("")return Nil, truecase *StatusCmd:.SetVal("")return Nil, true}return nil, falsecase proto.RespStatus: // "+OK\r\n", := .(*StatusCmd)if ! {return nil, false}// Single frame only: a CR or a bare LF before the terminator would mean// more than one line, which this path does not handle. The reader stops// at the first LF and rejects the frame, and that rejection is what// makes processCached drop the bad entry and refetch.if bytes.ContainsAny([:len()-2], "\r\n") {return nil, false}.SetVal(string([1 : len()-2]))return nil, truecase proto.RespString: // "$5\r\nhello\r\n", or "$-1\r\n" for RESP2 null, := .(*StringCmd)if ! {return nil, false}:= bytes.IndexByte(, '\r')if < 1 || +1 >= len() || [+1] != '\n' {return nil, false}, := util.ParseInt([1:], 10, 64)if != nil {return nil, false}if < 0 {// Only -1 is the RESP2 null bulk string. The reader rejects any// other negative length, so decline it: the generic path then// fails and processCached drops the bad entry and refetches.if != -1 || +2 != len() {return nil, false}.SetVal("")return Nil, true}// Payload must be exactly n bytes followed by the trailing CRLF.:= + 2if int64(len()) != int64()++2 {return nil, false}.SetVal(string([ : +int()]))return nil, true}return nil, false}// classifyCachedReply reports the same error applyCachedReply would, without a// caller command to populate. The miss coalescer uses it on the abandoned path// (the caller returned and owns its Cmder again) to decide cache-vs-cancel: a// value or Nil is cacheable, a top-level RESP error is not. It reads the frame// generically, so it can only diverge from a concrete cmd's readReply on a// well-formed reply of an unexpected shape — which the next reader re-parses and// drops (see processCached), so a rare mis-cache self-heals.func classifyCachedReply( []byte) error {, := proto.NewReaderSize(bytes.NewReader(), len()+1).ReadReply()return}// isCacheableReplyResult reports whether a fully read Redis reply can be// cached. redis.Nil is a normal negative lookup, not a transport/protocol// failure; tracking will invalidate it if the key is later created.func isCacheableReplyResult( error) bool {return == nil || == Nil}// cscDrainSkipWindow is the default SharedTracking drain period (overridable via// ClientSideCacheConfig.DrainInterval). A buffered invalidation is picked up within// roughly one round; MaxStaleness, when configured, is the hard time-based backstop.const cscDrainSkipWindow = 5 * time.Millisecond// cscDrainHardReadCap is the hard socket read deadline the drainer applies via// Conn.WithReaderHardDeadline. It bounds only a rare partial-frame mid-read. A// var (not const) so the tuning harness can sweep it.var cscDrainHardReadCap = 50 * time.Millisecond// cscDrainProbeReadCap bounds the non-consuming one-byte probe used only when// an opaque transport may hold data that the socket readiness check cannot see.const cscDrainProbeReadCap = 50 * time.Microsecond// cscDrainCustomErrCap is the number of CONSECUTIVE fatal custom-processor// drain errors after which the drainer disables CSC instead of removing (and// redialing) a connection per tick indefinitely.const cscDrainCustomErrCap = 8// processCached runs the Get-Reserve-Fulfill lifecycle for a cacheable command.// The caller (process) first makes sure that CSC is active and that cmd is// eligible.//// startAttempt is the number of attempts already spent before this call. It is// not zero only on the full-duplex divert (retryOnNormalConn) of a cacheable// command. Such a command spent its first attempt on the FD socket and got a// retryable reply. Give startAttempt to every processWithRetry fallback and to// the fetch. If you do not, a diverted cache miss runs MaxRetries+1 retries// after the FD attempt. That is one attempt too many. A cache HIT does no network// attempt and returns before the retry loop, so startAttempt does not affect its// retry BUDGET — but it still seeds the reported attempt COUNT (see below) so a// diverted hit reports the FD attempt it already spent, not zero.func ( *baseClient) ( context.Context, Cmder, *processState, int) error {if := .Err(); != nil {return}// Seed the reported attempt count for the OTel duration metric. A cache hit// returns below without entering processWithRetry's loop (which is what sets// state.attempts), so without this a diverted hit (startAttempt>0) would report// zero attempts, hiding the FD socket attempt it already spent. A miss falls// through to processWithRetry, whose loop overwrites this.if != nil {.attempts =}// Once the drainer has stopped (owner Close, or the owner dropped without// Close), no invalidations flow — a surviving clone must not serve stale hits.if := .cscActive; != nil && !.Load() {return .processWithRetry(, , nil, , )}// Read the namespace BEFORE building the key: the key is now built with// the namespace already in it, in one allocation, so the prefix has to be// known first.:= .cscKeyPrefixif == "" {// A successfully attached client always has a namespace. Fail closed if// an incomplete custom baseClient reaches this path.return .processWithRetry(, , nil, , )}// Check the key arguments BEFORE building the cache key. Building it// encodes every argument, which calls MarshalBinary on a marshaler key; an// uncacheable command would then marshal its key twice, once here and once// for the request. The check allocates nothing., , := cscKeySpan()if ! || !cscKeysRenderable(, , ) {// Without a key list we cannot react to invalidations for this// command, so it must not be cached.return .processWithRetry(, , nil, , )}, := buildCacheKeyNS(, )if ! {return .processWithRetry(, , nil, , )}// The key strings are deliberately NOT built here. Only Reserve needs// them, and Reserve is on the miss path -- so at a 94-99% hit rate this// used to allocate a key slice, a namespaced slice and one string per key// on every read and discard all of it. By object count that was ~18% of// everything the client allocated. They are built in cscMissKeys below,// after the hit check.// Serve hits straight from the cache.if , := .cscGet(, ); {if := .Err(); != nil {return}if := applyCachedReply(, ); isCacheableReplyResult() {return}.csc.DeleteByCacheKey()}// Demand trigger: a miss for a key still in the refresher's collection window// flushes that window now (no-op when refresh/coalescing is off). Scope: this// signals THIS client's own refresh queue. With a shared cache+processor across// clients the active refresh binding is the last-attached client's queue, so a// miss on one client does not early-flush a sibling's window — the sibling's// window timer is the backstop. Correctness is unaffected (the key still// refreshes); only the early-flush latency is client-local..cscRefreshQueue.signalDemand()// Miss path: now the key list is actually needed.:= cscMissKeys(, , , ), := .csc.Reserve(, )if ! {// Another goroutine is fetching; Get below waits until it completes.if , := .cscGet(, ); {if := .Err(); != nil {return}if := applyCachedReply(, ); isCacheableReplyResult() {return}.csc.DeleteByCacheKey()}// Original fetcher cancelled or its value was invalidated; try to take// over so later waiters still benefit from the cache. This is the 2x-RTT// path under churn: we waited a round trip and still must fetch ourselves., = .csc.Reserve(, )}// Reader-miss coalescing: hand the reserved miss to the batcher (no-op when// off). Load the pointer ONCE: a concurrent Close swaps it to nil, and a// second load after the nil-check would call fetch on a nil receiver.if := .cscMissCoalescer.Load(); && != nil {, := .fetch(, , , )// Re-run on the normal path when the coalescer bailed (errCSCRetryUncached)// or hit a session or transport failure (tagged cscSessionError by// settleErr). Then a coalesced miss gets the same MaxRetries and backoff as// any other command, instead of a raw io.EOF or ErrClosed to the caller (Ofek// review #3989). The coalescer always cancels the reservation on failure, so// the re-run starts clean, and processWithRetry handles a cancelled caller// context itself. A command result is not tagged (for example redis.Nil,// WRONGTYPE, or a retryable reply such as LOADING). It is the command's// answer, already applied to cmd, so it is returned as-is. This keeps the// coalesced path's documented tradeoff: no per-command retry for reply-level// errors.var cscSessionErrorif == errCSCRetryUncached || errors.As(, &) {// The coalescer bailed and cancelled its reservation. Re-reserve and FALL// THROUGH to the capture path below so a successful retry repopulates the// cache (a nil-capture processWithRetry returned the value but stored// nothing, so connection blips and wire-budget sheds left the key uncached// and later readers missed).//// Carry the coalesced attempt into the re-run's accounting. served is// non-nil exactly when the request reached a session connection (the// writer attributes every request of a batch to its conn before the// write; a pre-queue shed or a session that never acquired a conn leaves// it nil), which is the same "one attempt on that conn" rule the success// branch below applies. processWithRetry seeds both its reported attempt// count and its retry-budget position from startAttempt, so the failed// coalesced attempt shows up in retry_attempts and the error callback, and// the re-run does not get MaxRetries+1 fresh attempts on top of the one// already spent (codex on #3989; needs the explicit-start plumbing from the// full-duplex autopipeline branch, now merged).if != nil {++// Record it on state too, not only through processWithRetry: two exits// below never reach that loop — the exhausted-budget return here and// the takeover-hit return after the re-Reserve — and without this they// reported the attempts seeded before the fetch and a nil connection// for a request that did reach one (codex on #4002). processWithRetry// overwrites both on its first iteration, and keeps lastConn when the// re-run never acquires a connection.if != nil {.attempts =.lastConn =}// Budget check. The command has now spent startAttempt of its// MaxRetries+1 attempts; when that was the last one (an FD attempt plus// this coalesced one with MaxRetries=1, or any coalesced session failure// with retries disabled), a re-run would execute it once more:// processWithRetry clamps startAttempt back into its loop so a caller can// never disable execution, which here is an attempt over budget (codex on// #4002). Stop with the coalescer's cause instead — the same raw error// processWithRetry returns when its own loop runs out — and emit the error// metric the skipped re-run would have (settleErr leaves that to the// re-run so a re-run that succeeds is not flagged).if > .opt.MaxRetries {:=if .err != nil {= .err}.SetErr()recordCommandError(, , , -1)return}}, = .csc.Reserve(, )if ! {// Another waiter won the re-Reserve race and is fetching. WAIT on it// (Get parks on the in-progress entry) instead of falling through to an// independent pooled request: a session failure wakes every same-key// waiter at once, and each running its own processWithRetry would turn// one hot key into a pool stampede mid-recovery — defeating the// coalescing this path exists for. Same shape (and 2x-RTT churn// tradeoff) as the first-Reserve loser path above.if , := .cscGet(, ); {if := .Err(); != nil {return}if := applyCachedReply(, ); isCacheableReplyResult() {return}.csc.DeleteByCacheKey()}// The new owner cancelled or its value was invalidated; try to take// over. A second loss falls through to the plain pooled path — after// two waits, forward progress outranks another round of parking., = .csc.Reserve(, )}} else {// Native-recorder attribution: the coalesced fetch contacted Redis on// the session's held connection — report it as one attempt on that conn// so operation-duration metrics carry a real server.address instead of// zero attempts and a nil connection.if != nil && != nil {.attempts++.lastConn =}return}}// With shouldFetch still false, this caller lost the reservation twice and// reads the server directly, uncached. That read can return a newer value// than a fetch another caller reserved earlier and is still running. That// fetch may store its older value afterwards, and a later hit returns it// until the key's invalidation arrives, so reads are not monotonic per// caller. This stays within the staleness every hit already has until its// invalidation arrives. Dropping the older fetch would cost each of its// waiters a miss, under the churn that leads here, so it is documented// instead (README, client-side caching).var cscFetchCapturevar *cscFetchCaptureif {= &// Release the placeholder if processWithRetry panics; Cancel on a// stale token is a no-op.defer func() {if != nil {.csc.Cancel(, )}}()}:= .processWithRetry(, , , , )if {= nil // disarm the deferred Cancelif isCacheableReplyResult() {.fulfillCached(, , &)} else {.csc.Cancel(, )}}return}// fulfillCached stores a fetched value, attributing it to its serving conn when// an evict-on-remove hook is active so EvictByConn can drop it if that conn is// removed. It also closes the attribute-vs-coverage races: the conn is released// before this runs, so its OnRemove eviction — or a handoff re-init's scoped// eviction — may fire before the entry exists. Publication is serialized with// the hook's init-generation changes, so a reply whose invalidation coverage// was already lost never becomes visible and never wakes waiters with stale// data.func ( *baseClient) ( string, uint64, *cscFetchCapture) bool {if := .cscActive; != nil && !.Load() {.csc.Cancel(, )return false}if := .cscHook(); != nil {if .connID == 0 {// Invariant: an active hook always gets a real conn id (>=1). A zero id// would leave the entry unattributed and un-evictable, so fail closed..csc.Cancel(, )return false}if !.fulfillOwnedIfCovered(, , .connID, .initGen, .raw) {// A coverage mismatch leaves the reservation IN_PROGRESS because// FulfillOwned was deliberately skipped. Cancel wakes its waiters// as misses so one can safely refetch on a covered connection..csc.Cancel(, )return false}return true}return .csc.FulfillOwned(, , 0, .raw)}
![]() |
The pages are generated with Golds v0.8.7. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |