Source File
conn.go
Belonging Package
github.com/redis/go-redis/v9/internal/pool
// Package pool implements the pool managementpackage poolimport (uberatomic)var noDeadline = time.Time{}// Preallocated errors for hot paths to avoid allocationsvar (errAlreadyMarkedForHandoff = errors.New("connection is already marked for handoff")errNotMarkedForHandoff = errors.New("connection was not marked for handoff")errHandoffStateChanged = errors.New("handoff state changed during marking")errConnectionNotAvailable = errors.New("redis: connection not available")errConnNotAvailableForWrite = errors.New("redis: connection not available for write operation"))// getCachedTimeNs returns the current time in nanoseconds.// This function previously used a global cache updated by a background goroutine,// but that caused unnecessary CPU usage when the client was idle (ticker waking up// the scheduler every 50ms). We now use time.Now() directly, which is fast enough// on modern systems (vDSO on Linux) and only adds ~1-2% overhead in extreme// high-concurrency benchmarks while eliminating idle CPU usage.func getCachedTimeNs() int64 {return time.Now().UnixNano()}// GetCachedTimeNs returns the current time in nanoseconds.// Exported for use by other packages that need fast time access.func () int64 {return getCachedTimeNs()}// Global atomic counter for connection IDsvar connIDCounter atomic.Uint64// HandoffState represents the atomic state for connection handoffs// This struct is stored atomically to prevent race conditions between// checking handoff status and reading handoff parameterstype HandoffState struct {ShouldHandoff bool // Whether connection should be handed offEndpoint string // New endpoint for handoffSeqID int64 // Sequence ID from MOVING notification}// atomicNetConn is a wrapper to ensure consistent typing in atomic.Value.// It is always stored and accessed by pointer, so the embedded atomic.Bool is// never copied.type atomicNetConn struct {conn net.Conn// closed claims teardown of this specific transport generation. Close// CAS-claims it before calling conn.Close, so a given socket is closed// exactly once. A handoff installs a fresh wrapper (closed=false) via// setNetConn, so a replacement socket is a new generation claimed by the// next Close rather than skipped under a stale flag. The wrapper is left in// place on Close (not swapped out) so getNetConn keeps returning the closed// conn, preserving RemoteAddr/LocalAddr and the connCheck health path.closed atomic.Bool}// generateConnID generates a fast unique identifier for a connection with zero allocationsfunc generateConnID() uint64 {return connIDCounter.Add(1)}// relaxedState is a snapshot of one connection's relaxed-timeout window. Conn// publishes it through the relaxed atomic pointer. Do not change a stored// relaxedState. Each mutator copies the current snapshot, changes the copy, and// installs the copy with a compare-and-swap. A reader that holds an older pointer// still sees a consistent window.//// Two sources use this state (see the relaxed-timeout methods):// - Maintenance notifications. SetRelaxedTimeout adds a holder.// ClearRelaxedTimeout removes it. There is no deadline.// - Handoff. SetRelaxedTimeoutWithDeadline sets a deadline that expires the// window. Handoff never calls Clear.//// count is the number of current holders. A deadline window holds one slot only,// even after re-arms, because there is one deadlineNs.type relaxedState struct {readNs int64 // relaxed read timeout, nanosecondswriteNs int64 // relaxed write timeout, nanosecondsdeadlineNs int64 // auto-expiry, unix nanos; 0 = no deadlinecount int32 // number of holders; the window clears when count reaches 0}type Conn struct {// Connection identifier for unique trackingid uint64usedAt atomic.Int64lastPutAt atomic.Int64dialStartNs atomic.Int64 // Time when dial started (for connection create time metric)// Lock-free netConn access using atomic.Value// Contains *atomicNetConn wrapper, accessed atomically for better performancenetConnAtomic atomic.Value // stores *atomicNetConnrd *proto.Readerbw *bufio.Writerwr *proto.Writer// Lightweight mutex to protect reader operations during handoff and health checks// Used during:// - SetNetConn (write lock for resetting reader state)// - HasBufferedData/PeekReplyTypeSafe (read lock for safe concurrent peek operations)readerMu sync.RWMutex// State machine for connection state management// Replaces: usable, Inited, used// Provides thread-safe state transitions with FIFO waiting queue// States: CREATED → INITIALIZING → IDLE ⇄ IN_USE// ↓// UNUSABLE (handoff/reauth)// ↓// IDLE/CLOSEDstateMachine *ConnStateMachine// Handoff metadata - managed separately from state machine// These are atomic for lock-free access during handoff operationshandoffStateAtomic atomic.Value // stores *HandoffStatehandoffRetriesAtomic atomic.Uint32 // retry counterpooled boolpubsub boolcreatedAt time.TimeexpiresAt time.TimepoolName string // Name of the pool this connection belongs to (for metrics)// preparedFieldsets tracks HIMPORT fieldsets prepared on this// connection's current server session: fieldset name -> client-side// registry version. The server drops fieldsets when the session ends,// so the map is cleared whenever the underlying network connection is// replaced. preparedFieldsetsEpoch records the registry's discard-all// epoch the session was prepared under; a session behind the current// epoch replays HIMPORT DISCARDALL before its next HIMPORT command.// Guarded by preparedFieldsetsMu; the map is nil until first use.preparedFieldsetsMu sync.MutexpreparedFieldsets map[string]uint64preparedFieldsetsEpoch uint64// When a goroutine closes a connection, it usually knows the reason, so closeReason is not needed.// closeReason is only used when an in-use connection is closed by another goroutine,// to inform the goroutine using the connection why the connection was closed.closeReason uberatomic.String// closeOnPutReason marks an in-use connection for removal when it is returned// to the pool. The socket is left open for the in-flight command and closed// by ConnPool.Put.closeOnPutReason uberatomic.String// relaxed holds the relaxed-timeout window for maintenance notifications// (migrations and failovers). One atomic pointer publishes the whole window: the// read timeout, the write timeout, the optional deadline, and the holder count.// A reader always gets a consistent snapshot. A reader never sees a half-updated// window, such as a new deadline with old timeouts. nil means no relaxation. The// mutators (SetRelaxedTimeout, SetRelaxedTimeoutWithDeadline,// ClearRelaxedTimeout, expireRelaxedTimeout) install a new relaxedState with a// compare-and-swap. The read path (getEffective* and HasRelaxedTimeout) does one// lock-free Load. Each mutation allocates one relaxedState. This cost is small,// because mutations occur per maintenance notification, not per I/O.relaxed atomic.Pointer[relaxedState]// onClose is read and cleared by Close while initConn (running inside// SetNetConnAndInitConn under the INITIALIZING state) installs it via// SetOnClose; Close transitions to CLOSED from any state, so the two// race when a pool shutdown closes a connection mid-init. Stored as an// atomic pointer so the setter and Close don't need a mutex (keeping// Conn slim).onClose atomic.Pointer[func() error]// Connection initialization function for reconnectionsinitConnFunc func(context.Context, *Conn) error// onCscClose is the client-side-caching close hook, kept separate from// onClose (streaming-credentials cleanup) so neither clobbers the other.// Both keep overwrite semantics, so re-running initConn can't accumulate// them. Atomic for the same init-vs-Close race as onClose: it is also// installed by initConn and read and cleared by Close.onCscClose atomic.Pointer[func() error]// onCscReinit runs after the connection is claimed for reinitialization but// before its socket is replaced. CSC uses it to invalidate entries whose// server-side tracking coverage belongs to the old socket.onCscReinit func()// cscReadPending requests one conservative drain after a command read through// a transport whose buffered state cannot be fully observed by MaybeHasData.cscReadPending atomic.Bool// lastCscPeriodicProbeNs throttles bounded fallback reads on platforms and// opaque transports without a non-consuming readiness mechanism.lastCscPeriodicProbeNs atomic.Int64}func ( net.Conn) *Conn {return NewConnWithBufferSize(, proto.DefaultBufferSize, proto.DefaultBufferSize)}func ( net.Conn, , int) *Conn {:= time.Now():= &Conn{createdAt: ,id: generateConnID(), // Generate unique ID for this connectionstateMachine: NewConnStateMachine(),}// Use specified buffer sizes, or fall back to 32KiB defaults if 0if > 0 {.rd = proto.NewReaderSize(, )} else {.rd = proto.NewReader() // Uses 32KiB default}if > 0 {.bw = bufio.NewWriterSize(, )} else {.bw = bufio.NewWriterSize(, proto.DefaultBufferSize)}// Store netConn atomically for lock-free access using wrapper.netConnAtomic.Store(&atomicNetConn{conn: }).wr = proto.NewWriter(.bw).SetUsedAt()// Initialize handoff state atomically:= &HandoffState{ShouldHandoff: false,Endpoint: "",SeqID: 0,}.handoffStateAtomic.Store()return}func ( *Conn) () time.Time {return time.Unix(0, .usedAt.Load())}func ( *Conn) ( time.Time) {.usedAt.Store(.UnixNano())}func ( *Conn) () int64 {return .usedAt.Load()}func ( *Conn) ( int64) {.usedAt.Store()}func ( *Conn) () int64 {return .lastPutAt.Load()}func ( *Conn) ( int64) {.lastPutAt.Store()}// GetDialStartNs returns the time when the dial started (in nanoseconds since epoch).// This is used to calculate the full connection creation time (TCP + handshake).func ( *Conn) () int64 {return .dialStartNs.Load()}// PoolName returns the name of the pool this connection belongs to.// This is used for metrics to identify which pool a connection is from.func ( *Conn) () string {return .poolName}// SetPoolName sets the name of the pool this connection belongs to.// This should be called when the connection is added to a pool.func ( *Conn) ( string) {.poolName =}// Backward-compatible wrapper methods for state machine// These maintain the existing API while using the new state machine internally// CompareAndSwapUsable atomically compares and swaps the usable flag (lock-free).//// This is used by background operations (handoff, re-auth) to acquire exclusive// access to a connection. The operation sets usable to false, preventing the pool// from returning the connection to clients.//// Returns true if the swap was successful (old value matched), false otherwise.//// Implementation note: This is a compatibility wrapper around the state machine.// It checks if the current state is "usable" (IDLE or IN_USE) and transitions accordingly.// Deprecated: Use GetStateMachine().TryTransition() directly for better state management.func ( *Conn) (, bool) bool {:= .stateMachine.GetState()// Check if current state matches the "old" usable value:= ( == StateIdle || == StateInUse)if != {return false}// If we're trying to set to the same value, succeed immediatelyif == {return true}// Transition based on new valueif {// Trying to make usable - transition from UNUSABLE to IDLE// This should only work from UNUSABLE or INITIALIZING states// Use predefined slice to avoid allocation, := .stateMachine.TryTransition(validFromInitializingOrUnusable,StateIdle,)return == nil}// Trying to make unusable - transition from IDLE to UNUSABLE// This is typically for acquiring the connection for background operations// Use predefined slice to avoid allocation, := .stateMachine.TryTransition(validFromIdle,StateUnusable,)return == nil}// IsUsable returns true if the connection is safe to use for new commands (lock-free).//// A connection is "usable" when it's in a stable state and can be returned to clients.// It becomes unusable during:// - Handoff operations (network connection replacement)// - Re-authentication (credential updates)// - Other background operations that need exclusive access//// Note: CREATED state is considered usable because new connections need to pass OnGet() hook// before initialization. The initialization happens after OnGet() in the client code.func ( *Conn) () bool {:= .stateMachine.GetState()// CREATED, IDLE, and IN_USE states are considered usable// CREATED: new connection, not yet initialized (will be initialized by client)// IDLE: initialized and ready to be acquired// IN_USE: usable but currently acquired by someonereturn == StateCreated || == StateIdle || == StateInUse}// SetUsable sets the usable flag for the connection (lock-free).//// Deprecated: Use GetStateMachine().Transition() directly for better state management.// This method is kept for backwards compatibility.//// This should be called to mark a connection as usable after initialization or// to release it after a background operation completes.//// Prefer CompareAndSwapUsable() when acquiring exclusive access to avoid race conditions.// Deprecated: Use GetStateMachine().Transition() directly for better state management.func ( *Conn) ( bool) {if {// Transition to IDLE state (ready to be acquired).stateMachine.Transition(StateIdle)} else {// Transition to UNUSABLE state (for background operations).stateMachine.Transition(StateUnusable)}}// IsInited returns true if the connection has been initialized.// This is a backward-compatible wrapper around the state machine.func ( *Conn) () bool {:= .stateMachine.GetState()// Connection is initialized if it's in IDLE or any post-initialization statereturn != StateCreated && != StateInitializing && != StateClosed}// Used - State machine based implementation// CompareAndSwapUsed atomically compares and swaps the used flag (lock-free).// This method is kept for backwards compatibility.//// This is the preferred method for acquiring a connection from the pool, as it// ensures that only one goroutine marks the connection as used.//// Implementation: Uses state machine transitions IDLE ⇄ IN_USE//// Returns true if the swap was successful (old value matched), false otherwise.// Deprecated: Use GetStateMachine().TryTransition() directly for better state management.func ( *Conn) (, bool) bool {if == {// No change needed:= .stateMachine.GetState():= ( == StateInUse)return ==}if ! && {// Acquiring: IDLE → IN_USE// Use predefined slice to avoid allocation, := .stateMachine.TryTransition(validFromCreatedOrIdle, StateInUse)return == nil} else {// Releasing: IN_USE → IDLE// Use predefined slice to avoid allocation, := .stateMachine.TryTransition(validFromInUse, StateIdle)return == nil}}// IsUsed returns true if the connection is currently in use (lock-free).//// Deprecated: Use GetStateMachine().GetState() == StateInUse directly for better clarity.// This method is kept for backwards compatibility.//// A connection is "used" when it has been retrieved from the pool and is// actively processing a command. Background operations (like re-auth) should// wait until the connection is not used before executing commands.func ( *Conn) () bool {return .stateMachine.GetState() == StateInUse}// SetUsed sets the used flag for the connection (lock-free).//// This should be called when returning a connection to the pool (set to false)// or when a single-connection pool retrieves its connection (set to true).//// Prefer CompareAndSwapUsed() when acquiring from a multi-connection pool to// avoid race conditions.// Deprecated: Use GetStateMachine().Transition() directly for better state management.func ( *Conn) ( bool) {if {.stateMachine.Transition(StateInUse)} else {.stateMachine.Transition(StateIdle)}}// getNetConn returns the current network connection using atomic load (lock-free).// This is the fast path for accessing netConn without mutex overhead.func ( *Conn) () net.Conn {if := .netConnAtomic.Load(); != nil {if , := .(*atomicNetConn); {return .conn}}return nil}// setNetConn stores the network connection atomically (lock-free).// This is used for the fast path of connection replacement.func ( *Conn) ( net.Conn) {.netConnAtomic.Store(&atomicNetConn{conn: })}// Handoff state management - atomic access to handoff metadata// ShouldHandoff returns true if connection needs handoff (lock-free).func ( *Conn) () bool {if := .handoffStateAtomic.Load(); != nil {return .(*HandoffState).ShouldHandoff}return false}// GetHandoffEndpoint returns the new endpoint for handoff (lock-free).func ( *Conn) () string {if := .handoffStateAtomic.Load(); != nil {return .(*HandoffState).Endpoint}return ""}// GetMovingSeqID returns the sequence ID from the MOVING notification (lock-free).func ( *Conn) () int64 {if := .handoffStateAtomic.Load(); != nil {return .(*HandoffState).SeqID}return 0}// GetHandoffInfo returns all handoff information atomically (lock-free).// This method prevents race conditions by returning all handoff state in a single atomic operation.// Returns (shouldHandoff, endpoint, seqID).func ( *Conn) () (bool, string, int64) {if := .handoffStateAtomic.Load(); != nil {:= .(*HandoffState)return .ShouldHandoff, .Endpoint, .SeqID}return false, "", 0}// HandoffRetries returns the current handoff retry count (lock-free).func ( *Conn) () int {return int(.handoffRetriesAtomic.Load())}// IncrementAndGetHandoffRetries atomically increments and returns handoff retries (lock-free).func ( *Conn) ( int) int {return int(.handoffRetriesAtomic.Add(uint32()))}// IsPooled returns true if the connection is managed by a pool and will be pooled on Put.func ( *Conn) () bool {return .pooled}// MarkCloseOnPut marks the connection for removal when it is returned to the pool.func ( *Conn) ( string) {.closeOnPutReason.Store()}// CloseOnPutReason returns a non-empty reason when the connection should be// removed instead of pooled on Put.func ( *Conn) () string {return .closeOnPutReason.Load()}// IsPubSub returns true if the connection is used for PubSub.func ( *Conn) () bool {return .pubsub}// SetRelaxedTimeout sets the relaxed timeouts for this connection during a// maintenance-notification upgrade. They apply to every later command until an// equal number of ClearRelaxedTimeout calls remove this holder. This method is// lock-free. It installs a new snapshot with a compare-and-swap and keeps any// deadline that is already in effect.// Note: the caller (the notification handler) records the metrics, because it// knows the notification type and the pool name.func ( *Conn) (, time.Duration) {for {:= .relaxed.Load()var relaxedStateif != nil {= *}.readNs = int64().writeNs = int64().count++if .relaxed.CompareAndSwap(, &) {return}}}// SetRelaxedTimeoutWithDeadline sets the relaxed timeouts and a deadline. After// the deadline the window reverts on its own, because handoff never calls Clear.// Only the first deadline window takes a holder slot. An overlapping handoff// re-arms the window: it replaces the deadline in place and does not add a holder.// So the later expiry removes exactly one holder and the window clears. The// connection does not stay relaxed forever. This method is lock-free.func ( *Conn) (, time.Duration, time.Time) {:= .UnixNano()for {:= .relaxed.Load()var relaxedStateif != nil {= *}.readNs = int64().writeNs = int64()if .deadlineNs == 0 {// There is no deadline holder yet. Take one. A re-arm already has a// deadline set, so it replaces the deadline and does not add a holder..count++}.deadlineNs =if .relaxed.CompareAndSwap(, &) {return}}}// ClearRelaxedTimeout removes one holder that SetRelaxedTimeout added. When the// last holder is gone and no unexpired deadline remains, it drops the whole// window. The count has a lower bound of zero. So a second clear, or a clear after// a deadline expiry already emptied the window, does nothing and cannot block a// later relaxation. This method is lock-free.func ( *Conn) () {for {:= .relaxed.Load()if == nil || .count <= 0 {return // already cleared (for example, by a deadline expiry)}:= *.count--// Keep the deadline gate. After an explicit clear the window can still hold// the relaxed timeouts until the safety deadline. Drop the window only when// the last holder leaves and the deadline is unset or already past.:= &if .count <= 0 && (.deadlineNs == 0 || time.Now().UnixNano() >= .deadlineNs) {= nil}if .relaxed.CompareAndSwap(, ) {return}}}// expireRelaxedTimeout removes the deadline holder after the deadline passes.// getEffective* calls it when a read finds the deadline expired. The// compare-and-swap checks that the current snapshot still holds THIS deadline. A// concurrent SetRelaxedTimeout* can install a newer window; then this expiry is// stale and does nothing. It removes only the deadline holder (count minus one).// A notification holder on the same connection stays active. The window clears in// full only when the deadline holder was the last holder. One snapshot publishes// the whole window, so a reader never sees a half-cleared state.func ( *Conn) ( int64) {for {:= .relaxed.Load()if == nil || .deadlineNs != {// Already cleared, or replaced by a newer window: stale expiry.return}:= *.deadlineNs = 0if .count > 0 {.count--}:= &if .count <= 0 {= nil}if .relaxed.CompareAndSwap(, ) {internal.Logger.Printf(context.Background(), "%s", logs.UnrelaxedTimeoutAfterDeadline(.GetID()))return}}}// HasRelaxedTimeout returns true when the relaxed timeouts are active on this// connection. Active means a holder is present, a timeout is set, and any deadline// is still in the future. When the deadline has passed it removes the deadline holder// once (like getEffective*) and re-reads the snapshot: a surviving non-deadline holder// keeps the window active. Before this, an expired deadline made it report false even// while another holder was still active, contradicting the surviving-holder semantics.// This reports the boolean only; the timeout VALUE it would relax to may still be the// expired holder's, since all holders share one (readNs, writeNs) pair — see the// per-holder timeout follow-up.func ( *Conn) () bool {:= .relaxed.Load()if == nil || .count <= 0 || (.readNs <= 0 && .writeNs <= 0) {return false}if .deadlineNs == 0 || time.Now().UnixNano() < .deadlineNs {return true}// The deadline passed. Remove the deadline holder, then re-read: a notification// holder can still be active with no deadline (surviving-holder semantics), matching// getEffectiveReadTimeout..expireRelaxedTimeout(.deadlineNs):= .relaxed.Load()if == nil || .count <= 0 || (.readNs <= 0 && .writeNs <= 0) {return false}return .deadlineNs == 0 || time.Now().UnixNano() < .deadlineNs}// EffectiveReadTimeout reports the read timeout that a read on this connection// uses now. It returns the active relaxed timeout when the window is set and// unexpired. Otherwise it returns normalTimeout. A caller that bounds how long a// blocked read may take (for example, the CSC full-duplex drain backstop) must use// this method, not the configured ReadTimeout. With ReadTimeout, a relaxed read// ends too soon.//// It is safe to call from several goroutines at once (the reader, the writer, and// the drain backstop of a full-duplex connection). It reads one atomic snapshot.// The only change it makes is the deadline safety net: when the deadline has// passed, it removes the deadline holder once (see expireRelaxedTimeout). It never// removes an active, unexpired window. If a notification holder survives that// expiry, it returns that holder's relaxed timeout, not normalTimeout.func ( *Conn) ( time.Duration) time.Duration {return .getEffectiveReadTimeout()}// EffectiveWriteTimeout is the write side of EffectiveReadTimeout. Use it to bound// how long a blocked write may take. It has the same snapshot behavior.func ( *Conn) ( time.Duration) time.Duration {return .getEffectiveWriteTimeout()}// getEffectiveReadTimeout returns the timeout for read operations. It returns the// relaxed read timeout while the window is set and unexpired. Otherwise it returns// normalTimeout. When the deadline has passed, it removes the deadline holder and// then reads the window again. A surviving notification holder's relaxed timeout// still takes priority over normalTimeout.func ( *Conn) ( time.Duration) time.Duration {:= .relaxed.Load()if == nil || .readNs <= 0 {return}if .deadlineNs == 0 {return time.Duration(.readNs)}// Use the cached time to avoid an expensive system call. Up to 50 ms of// staleness is acceptable.if getCachedTimeNs() < .deadlineNs {return time.Duration(.readNs)}// The deadline passed. Remove the deadline holder, then read the window again.// A notification holder can still be active with no deadline. Use its relaxed// timeout. Do not end this call early with normalTimeout..expireRelaxedTimeout(.deadlineNs)if := .relaxed.Load(); != nil && .readNs > 0 && (.deadlineNs == 0 || getCachedTimeNs() < .deadlineNs) {return time.Duration(.readNs)}return}// getEffectiveWriteTimeout is the write side of getEffectiveReadTimeout.func ( *Conn) ( time.Duration) time.Duration {:= .relaxed.Load()if == nil || .writeNs <= 0 {return}if .deadlineNs == 0 {return time.Duration(.writeNs)}if getCachedTimeNs() < .deadlineNs {return time.Duration(.writeNs)}.expireRelaxedTimeout(.deadlineNs)if := .relaxed.Load(); != nil && .writeNs > 0 && (.deadlineNs == 0 || getCachedTimeNs() < .deadlineNs) {return time.Duration(.writeNs)}return}// SetOnClose installs fn as the callback invoked exactly once when this// connection is closed (via Conn.Close).//// IMPORTANT: SetOnClose OVERWRITES any previously installed callback — it// does not compose, chain, or deduplicate. A Conn has room for a single// onClose hook by design, because its lifecycle is bounded (a Conn is// created, optionally re-initialized on its own net.Conn, and then closed// once) and the pool's OnRemove hooks handle any registry-level cleanup// that must survive the net.Conn being swapped.//// This has a subtle implication for per-connection subscriptions such as// the unsubscribe function returned by StreamingCredentialsProvider// (e.g. EntraID token rotation): if SetOnClose is called twice on the// same Conn with DIFFERENT unsubscribe closures — for example because// initConn ran a second time and obtained a fresh Subscribe() —// the previous unsubscribe is dropped and will NEVER run, leaking a// subscription on the provider. Callers must therefore ensure either://// - the provider's Subscribe is idempotent for the same listener (the// streaming credentials Manager deduplicates listeners by connection// id, so re-Subscribe returns an equivalent unsubscribe), OR// - the previous callback has already been invoked before SetOnClose is// called again.//// Design note: unlike the client-level onCloseHooks registry (see// redis.baseClient), there is intentionally NO named-hook dedup or// multi-callback support on Conn. This is a deliberate trade-off to keep// the Conn object slim — a pool can hold thousands of Conn values and// each one is a hot allocation, so paying for a sync.Mutex plus a// map[string]func() error per connection to support a feature that would// only be used by at most one subsystem today (streaming credentials) is// not worth the per-connection memory and allocation cost. For a single// Conn there is at most one meaningful close callback at any point in// time, and a richer registry here would not even solve the "stale// closure" hazard described above.func ( *Conn) ( func() error) {if == nil {.onClose.Store(nil)return}.onClose.Store(&)}// SetOnCscClose sets the client-side-caching close hook, overwriting any// previous one. It runs on Close in addition to the SetOnClose callback.func ( *Conn) ( func() error) {if == nil {.onCscClose.Store(nil)return}.onCscClose.Store(&)}// SetOnCscReinit sets the client-side-caching pre-reinitialization hook,// overwriting any previous one.func ( *Conn) ( func()) {.onCscReinit =}// SetInitConnFunc sets the connection initialization function to be called on reconnections.func ( *Conn) ( func(context.Context, *Conn) error) {.initConnFunc =}// ExecuteInitConn runs the stored connection initialization function if available.func ( *Conn) ( context.Context) error {if .initConnFunc != nil {return .initConnFunc(, )}return fmt.Errorf("redis: no initConnFunc set for conn[%d]", .GetID())}func ( *Conn) ( net.Conn) {// Store the new connection atomically first (lock-free).setNetConn()// Protect reader reset operations to avoid data races// Use write lock since we're modifying the reader state.readerMu.Lock().rd.Reset().readerMu.Unlock().bw.Reset()// A new socket is a new server session with no HIMPORT fieldsets and// nothing left to discard..ClearPreparedFieldsets(0)}// FieldsetPreparedVersion returns the client-side registry version at which// the named HIMPORT fieldset was prepared on this connection's current server// session, or 0 if it was not prepared on it (registry versions start at 1).func ( *Conn) ( string) uint64 {.preparedFieldsetsMu.Lock():= .preparedFieldsets[].preparedFieldsetsMu.Unlock()return}// MarkFieldsetPrepared records that the named HIMPORT fieldset was prepared// on this connection's current server session at the given registry version.// A session acquiring its first fieldset adopts the given discard-all epoch// (fieldsets prepared after an HIMPORT DISCARDALL are not subject to it);// the epoch never moves backwards, so a mark carrying an older snapshot// cannot regress a session already wiped at a newer epoch.func ( *Conn) ( string, , uint64) {.preparedFieldsetsMu.Lock()if len(.preparedFieldsets) == 0 {.preparedFieldsets = make(map[string]uint64)if > .preparedFieldsetsEpoch {.preparedFieldsetsEpoch =}}.preparedFieldsets[] =.preparedFieldsetsMu.Unlock()}// UnmarkFieldsetPrepared forgets that the named HIMPORT fieldset was prepared// on this connection, forcing a replay before the next HIMPORT SET using it.func ( *Conn) ( string) {.preparedFieldsetsMu.Lock()delete(.preparedFieldsets, ).preparedFieldsetsMu.Unlock()}// HasPreparedFieldsets reports whether any HIMPORT fieldset is prepared on// this connection's current server session.func ( *Conn) () bool {.preparedFieldsetsMu.Lock():= len(.preparedFieldsets).preparedFieldsetsMu.Unlock()return > 0}// PreparedFieldsetNames returns the names of the HIMPORT fieldsets prepared// on this connection's current server session.func ( *Conn) () []string {.preparedFieldsetsMu.Lock():= make([]string, 0, len(.preparedFieldsets))for := range .preparedFieldsets {= append(, )}.preparedFieldsetsMu.Unlock()return}// FieldsetEpoch returns the discard-all epoch this connection's prepared// fieldsets belong to (0 when none were ever prepared on the session).func ( *Conn) () uint64 {.preparedFieldsetsMu.Lock():= .preparedFieldsetsEpoch.preparedFieldsetsMu.Unlock()return}// ClearPreparedFieldsets forgets all HIMPORT fieldsets prepared on this// connection and records the discard-all epoch the wipe corresponds to.func ( *Conn) ( uint64) {.preparedFieldsetsMu.Lock().preparedFieldsets = nil.preparedFieldsetsEpoch =.preparedFieldsetsMu.Unlock()}// GetNetConn safely returns the current network connection using atomic load (lock-free).// This method is used by the pool for health checks and provides better performance.func ( *Conn) () net.Conn {return .getNetConn()}// SetNetConnAndInitConn replaces the underlying connection and executes the initialization.// This method ensures only one initialization can happen at a time by using atomic state transitions.// If another goroutine is currently initializing, this will wait for it to complete.func ( *Conn) ( context.Context, net.Conn) error {// Wait for and transition to INITIALIZING state - this prevents concurrent initializations// Valid from states: CREATED (first init), IDLE (reconnect), UNUSABLE (handoff/reauth)// If another goroutine is initializing, we'll wait for it to finish// if the context has a deadline, use that, otherwise use the connection read (relaxed) timeout// which should be set during handoff. If it is not set, use a 5 second default, := .Deadline()if ! {= time.Now().Add(.getEffectiveReadTimeout(5 * time.Second))}, := context.WithDeadline(, )defer ()// Use predefined slice to avoid allocation, := .stateMachine.AwaitAndTransition(,validFromCreatedIdleOrUnusable,StateInitializing,)if != nil {return fmt.Errorf("cannot initialize connection from state %s: %w", , )}if .onCscReinit != nil {.onCscReinit()}// Replace the underlying connection.SetNetConn()// Execute initialization// NOTE: ExecuteInitConn (via baseClient.initConn) will transition to IDLE on success// or CLOSED on failure. We don't need to do it here.// NOTE: Initconn returns conn in IDLE state:= .ExecuteInitConn()if != nil {// ExecuteInitConn already transitioned to CLOSED, just return the errorreturn}// ExecuteInitConn already transitioned to IDLEreturn nil}// MarkForHandoff marks the connection for handoff due to MOVING notification.// Returns an error if the connection is already marked for handoff.// Note: This only sets metadata - the connection state is not changed until OnPut.// This allows the current user to finish using the connection before handoff.func ( *Conn) ( string, int64) error {// Check if already marked for handoffif .ShouldHandoff() {return errAlreadyMarkedForHandoff}// Set handoff metadata atomically.handoffStateAtomic.Store(&HandoffState{ShouldHandoff: true,Endpoint: ,SeqID: ,})return nil}// MarkQueuedForHandoff marks the connection as queued for handoff processing.// This makes the connection unusable until handoff completes.// This is called from OnPut hook, where the connection is typically in IN_USE state.// The pool will preserve the UNUSABLE state and not overwrite it with IDLE.func ( *Conn) () error {// Get current handoff state:= .handoffStateAtomic.Load()if == nil {return errNotMarkedForHandoff}:= .(*HandoffState)if !.ShouldHandoff {return errNotMarkedForHandoff}// Create new state with ShouldHandoff=false but preserve endpoint and seqID// This prevents the connection from being queued multiple times while still// allowing the worker to access the handoff metadata:= &HandoffState{ShouldHandoff: false,Endpoint: .Endpoint, // Preserve endpoint for handoff processingSeqID: .SeqID, // Preserve seqID for handoff processing}// Atomic compare-and-swap to update stateif !.handoffStateAtomic.CompareAndSwap(, ) {// State changed between load and CAS - retry or return errorreturn errHandoffStateChanged}// Transition to UNUSABLE from IN_USE (normal flow), IDLE (edge cases), or CREATED (tests/uninitialized)// The connection is typically in IN_USE state when OnPut is called (normal Put flow)// But in some edge cases or tests, it might be in IDLE or CREATED state// The pool will detect this state change and preserve it (not overwrite with IDLE)// Use predefined slice to avoid allocation, := .stateMachine.TryTransition(validFromCreatedInUseOrIdle, StateUnusable)if != nil {// Check if already in UNUSABLE state (race condition or retry)// ShouldHandoff should be false now, but check just in caseif == StateUnusable && !.ShouldHandoff() {// Already unusable - this is fine, keep the new handoff statereturn nil}// Restore the original handoff state only if nothing else changed it// since our CAS above. A concurrent handoff worker may have completed// the handoff and run ClearHandoffState in this window; a plain Store// would clobber that, resurrecting ShouldHandoff=true and wedging the// connection so it can never be acquired again. The CAS leaves the// worker's state intact when it has taken over..handoffStateAtomic.CompareAndSwap(, )return fmt.Errorf("failed to mark connection as unusable: %w", )}return nil}// GetID returns the unique identifier for this connection.func ( *Conn) () uint64 {return .id}// GetStateMachine returns the connection's state machine for advanced state management.// This is primarily used by internal packages like maintnotifications for handoff processing.func ( *Conn) () *ConnStateMachine {return .stateMachine}// TryAcquire attempts to acquire the connection for use.// This is an optimized inline method for the hot path (Get operation).//// It tries to transition from IDLE -> IN_USE or CREATED -> CREATED.// Returns true if the connection was successfully acquired, false otherwise.// The CREATED->CREATED is done so we can keep the state correct for later// initialization of the connection in initConn.//// Performance: This is faster than calling GetStateMachine() + TryTransitionFast()//// NOTE: We directly access cn.stateMachine.state here instead of using the state machine's// methods. This breaks encapsulation but is necessary for performance.// The IDLE->IN_USE and CREATED->CREATED transitions don't need// waiter notification, and benchmarks show 1-3% improvement. If the state machine ever// needs to notify waiters on these transitions, update this to use TryTransitionFast().func ( *Conn) () bool {// The || operator short-circuits, so only 1 CAS in the common casereturn .stateMachine.state.CompareAndSwap(uint32(StateIdle), uint32(StateInUse)) ||.stateMachine.state.CompareAndSwap(uint32(StateCreated), uint32(StateCreated))}// Release releases the connection back to the pool.// This is an optimized inline method for the hot path (Put operation).//// It tries to transition from IN_USE -> IDLE.// Returns true if the connection was successfully released, false otherwise.//// Performance: This is faster than calling GetStateMachine() + TryTransitionFast().//// NOTE: We directly access cn.stateMachine.state here instead of using the state machine's// methods. This breaks encapsulation but is necessary for performance.// Waiters parked in AwaitAndTransition for IDLE (re-auth, handoff) are notified// after a successful transition; notifyWaiters costs one atomic load when nobody waits.func ( *Conn) () bool {// Inline the hot path - single CAS operationif !.stateMachine.state.CompareAndSwap(uint32(StateInUse), uint32(StateIdle)) {return false}// The connection just became IDLE: wake anyone waiting for that state, such// as a re-auth worker parked in AwaitAndTransition. notifyWaiters starts// with an atomic load of waiterCount, so this costs one load when nobody// is waiting..stateMachine.notifyWaiters()return true}// ClearHandoffState clears the handoff state after successful handoff.// Makes the connection usable again.func ( *Conn) () {// Clear handoff metadata.handoffStateAtomic.Store(&HandoffState{ShouldHandoff: false,Endpoint: "",SeqID: 0,})// Reset retry counter.handoffRetriesAtomic.Store(0)// Mark connection as usable again// Use state machine directly instead of deprecated SetUsable// probably done by initConn.stateMachine.Transition(StateIdle)}// ExpiresAt returns the connection's absolute lifetime expiry (zero when no// ConnMaxLifetime applies; jitter included). Set once at dial, so a plain read// is safe. Long-holding callers bound their hold by the REMAINING lifetime.func ( *Conn) () time.Time {return .expiresAt}// HasBufferedData safely checks if the connection has buffered data.// This method is used to avoid data races when checking for push notifications.func ( *Conn) () bool {// Use read lock for concurrent access to reader state.readerMu.RLock()defer .readerMu.RUnlock()return .rd.Buffered() > 0}// PeekReplyTypeSafe safely peeks at the reply type.// This method is used to avoid data races when checking for push notifications.func ( *Conn) () (byte, error) {// Use read lock for concurrent access to reader state.readerMu.RLock()defer .readerMu.RUnlock()if .rd.Buffered() <= 0 {return 0, fmt.Errorf("redis: can't peek reply type, no data available")}return .rd.PeekReplyType()}// PeekReplyTypeForCheck peeks at the reply type while holding readerMu, so it is// safe against a concurrent SetNetConn resetting the reader during handoff.// Unlike PeekReplyTypeSafe it does not require the data to already be buffered:// the pool health check calls it after connCheck reports unexpected socket data,// and connCheck only MSG_PEEKs, so the byte still has to be pulled from the// socket into the reader here.func ( *Conn) () (byte, error) {.readerMu.RLock()defer .readerMu.RUnlock()return .rd.PeekReplyType()}func ( *Conn) ( []byte) (int, error) {// Lock-free netConn access for better performanceif := .getNetConn(); != nil {return .Write()}return 0, net.ErrClosed}func ( *Conn) () net.Addr {// Lock-free netConn access for better performanceif := .getNetConn(); != nil {return .RemoteAddr()}return nil}func ( *Conn) (context.Context, time.Duration, func( *proto.Reader) error,) error {if >= 0 {// Use relaxed timeout if set, otherwise use provided timeout:= .getEffectiveReadTimeout()// Get the connection directly from atomic storage:= .getNetConn()if == nil {return errConnectionNotAvailable}if := .SetReadDeadline(.deadline(, )); != nil {return}} else {// A negative timeout skips SetReadDeadline, and thus deadline(), which is// the only per-I/O usedAt update. Record usage anyway so a long// deadline-free hold (e.g. a full-duplex CSC session under ReadTimeout=-2)// is not misjudged as idle-expired by the pool on the next Get and// needlessly closed + redialed. Cheap: one atomic store, only on the rare// deadline-free path; harmless on a nil netConn..SetUsedAtNs(getCachedTimeNs())}return (.rd)}// WithReaderHardDeadline runs fn under a HARD read deadline of now+timeout,// bypassing getEffectiveReadTimeout so a relaxed maintenance timeout can't extend// it (used by the CSC drainer). Takes no context: an expired cycle ctx must not// become the socket deadline, or the read surfaces context.DeadlineExceeded, which// isBadConn treats as fatal.func ( *Conn) (time.Duration, func( *proto.Reader) error,) ( error) {:= .getNetConn()if == nil {return errConnectionNotAvailable}if := .SetReadDeadline(time.Now().Add()); != nil {return}defer func() {if := .SetReadDeadline(time.Time{}); != nil {=}}()return (.rd)}func ( *Conn) (context.Context, time.Duration, func( *proto.Writer) error,) error {if >= 0 {// Use relaxed timeout if set, otherwise use provided timeout:= .getEffectiveWriteTimeout()// Set write deadline on the connectionif := .getNetConn(); != nil {if := .SetWriteDeadline(.deadline(, )); != nil {return}} else {// Connection is not available - return preallocated errorreturn errConnNotAvailableForWrite}} else {// See WithReader: keep usedAt fresh on the deadline-free write path too so// a long-held conn (ReadTimeout/WriteTimeout=-2) is not misjudged as// idle-expired by the pool on the next Get..SetUsedAtNs(getCachedTimeNs())}// Reset the buffered writer if needed, should not happenif .bw.Buffered() > 0 {if := .getNetConn(); != nil {.bw.Reset()}}if := (.wr); != nil {return}return .bw.Flush()}func ( *Conn) () bool {return .stateMachine.GetState() == StateClosed}func ( *Conn) () error {// Transition to CLOSED. When the connection is already CLOSED, fall through// to the cleanup below rather than returning early: a rejected initConn// marks the connection CLOSED to report failure *before* any teardown runs// (see redis.go initConn failure paths), so the pool's subsequent Close must// still release the transport and run the close callbacks. Returning early// on StateClosed leaked the socket and skipped the streaming-credentials// unsubscribe / CSC close callbacks (issue #3982).for {:= .stateMachine.GetState()if == StateClosed {break}if .stateMachine.TryTransitionFast(, StateClosed) {// TryTransitionFast deliberately skips waiter notification; Close// still needs to wake any goroutine waiting on initialization..stateMachine.notifyWaiters()break}}// Callbacks are cleared with an atomic swap so each runs at most once even// across concurrent or repeated Close calls, and independently of the state// machine — the CLOSED state may have been set by a failed initialization// rather than here.if := .onClose.Swap(nil); != nil {// ignore error_ = (*)()}if := .onCscClose.Swap(nil); != nil {// ignore error_ = (*)()}// Close the current transport generation exactly once, claiming it via the// wrapper's per-generation flag. The wrapper is left in netConnAtomic (not// nil-ed or swapped out) so getNetConn keeps returning the closed conn,// preserving the pre-fix contract that RemoteAddr/LocalAddr and the// connCheck health path rely on.//// Load-then-CAS is deliberately not a single atomic step: if a concurrent// handoff installs a new wrapper between the Load and the CAS, this Close// claims and closes the OLD generation (which still needs closing) while the// replacement is a fresh wrapper (closed=false) claimed by the next Close.// That is the correct outcome and is what makes teardown generation-bound// rather than leaking a socket installed after a Close set a lifetime flag.//// Repeat/concurrent closes of the same generation lose the CAS and return// nil, so no spurious "use of closed network connection" reaches callers// such as ConnPool.closeConnsIf. A handoff may also close the pre-handoff// socket directly (handoff_worker.go captures oldConn); if that races this// path the socket is closed twice, which is harmless — the extra close is// discarded.if := .netConnAtomic.Load(); != nil {if , := .(*atomicNetConn); && .conn != nil {if .closed.CompareAndSwap(false, true) {return .conn.Close()}}}return nil}// MaybeHasData tries to peek at the next byte in the socket without consuming it// This is used to check if there are push notifications available// Important: This will work on Linux, but not on Windowsfunc ( *Conn) () bool {// Lock-free netConn access for better performanceif := .getNetConn(); != nil {return maybeHasData()}return false}// CheckForData reports whether the socket has data ready and surfaces a// detected closed or failed socket.func ( *Conn) () (bool, error) {if := .getNetConn(); != nil {return checkForData()}return false, nil}// MarkCscReadPending requests one conservative CSC drain after a command read// when the transport may retain data that MaybeHasData cannot observe.func ( *Conn) () {:= .getNetConn()if == nil {return}if needsCscReadProbe() {.cscReadPending.Store(true)}}// TakeCscReadPending consumes the post-command conservative-drain request.func ( *Conn) () bool {return .cscReadPending.Swap(false)}// TakeCscPeriodicReadPending schedules a throttled conservative read for// transports with no readiness mechanism. It returns true at most once per// interval, including when several drainer passes race.func ( *Conn) ( time.Duration) bool {:= .getNetConn()if == nil || <= 0 || !needsCscPeriodicProbe() {return false}:= time.Since(.createdAt).Nanoseconds()if <= 0 {= 1}for {:= .lastCscPeriodicProbeNs.Load()if != 0 && >= && - < int64() {return false}if .lastCscPeriodicProbeNs.CompareAndSwap(, ) {return true}}}// deadline computes the effective deadline time based on context and timeout.// It updates the usedAt timestamp to now.// Uses cached time to avoid expensive syscall (max 50ms staleness is acceptable for deadline calculation).func ( *Conn) ( context.Context, time.Duration) time.Time {// Use cached time for deadline calculation (called 2x per command: read + write):= getCachedTimeNs().SetUsedAtNs():= time.Unix(0, )if > 0 {= .Add()}if != nil {, := .Deadline()if {if == 0 {return}if .Before() {return}return}}if > 0 {return}return noDeadline}
![]() |
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. |