@@ -18,9 +18,9 @@ import (
1818
1919var noDeadline = time.Time {}
2020
21- // Global time cache updated every 100ms by background goroutine.
21+ // Global time cache updated every 50ms by background goroutine.
2222// This avoids expensive time.Now() syscalls in hot paths like getEffectiveReadTimeout.
23- // Max staleness: 100ms , which is acceptable for timeout deadline checks (timeouts are typically 3-30 seconds).
23+ // Max staleness: 50ms , which is acceptable for timeout deadline checks (timeouts are typically 3-30 seconds).
2424var globalTimeCache struct {
2525 nowNs atomic.Int64
2626}
@@ -49,7 +49,7 @@ func getCachedTimeNs() int64 {
4949
5050// GetCachedTimeNs returns the current time in nanoseconds from the global cache.
5151// This is updated every 100ms by a background goroutine, avoiding expensive syscalls.
52- // Max staleness: 100ms .
52+ // Max staleness: 50ms .
5353// Exported for use by other packages that need fast time access.
5454func GetCachedTimeNs () int64 {
5555 return getCachedTimeNs ()
@@ -499,7 +499,7 @@ func (cn *Conn) getEffectiveReadTimeout(normalTimeout time.Duration) time.Durati
499499 return time .Duration (readTimeoutNs )
500500 }
501501
502- // Use cached time to avoid expensive syscall (max 100ms staleness is acceptable for timeout checks)
502+ // Use cached time to avoid expensive syscall (max 50ms staleness is acceptable for timeout checks)
503503 nowNs := getCachedTimeNs ()
504504 // Check if deadline has passed
505505 if nowNs < deadlineNs {
@@ -533,7 +533,7 @@ func (cn *Conn) getEffectiveWriteTimeout(normalTimeout time.Duration) time.Durat
533533 return time .Duration (writeTimeoutNs )
534534 }
535535
536- // Use cached time to avoid expensive syscall (max 100ms staleness is acceptable for timeout checks)
536+ // Use cached time to avoid expensive syscall (max 50ms staleness is acceptable for timeout checks)
537537 nowNs := getCachedTimeNs ()
538538 // Check if deadline has passed
539539 if nowNs < deadlineNs {
@@ -725,7 +725,7 @@ func (cn *Conn) GetStateMachine() *ConnStateMachine {
725725func (cn * Conn ) TryAcquire () bool {
726726 // The || operator short-circuits, so only 1 CAS in the common case
727727 return cn .stateMachine .state .CompareAndSwap (uint32 (StateIdle ), uint32 (StateInUse )) ||
728- cn .stateMachine .state .CompareAndSwap ( uint32 ( StateCreated ), uint32 (StateCreated ) )
728+ cn .stateMachine .state .Load () == uint32 (StateCreated )
729729}
730730
731731// Release releases the connection back to the pool.
@@ -829,19 +829,18 @@ func (cn *Conn) WithWriter(
829829 // Use relaxed timeout if set, otherwise use provided timeout
830830 effectiveTimeout := cn .getEffectiveWriteTimeout (timeout )
831831
832- // Always set write deadline, even if getNetConn() returns nil
833- // This prevents write operations from hanging indefinitely
832+ // Set write deadline on the connection
834833 if netConn := cn .getNetConn (); netConn != nil {
835834 if err := netConn .SetWriteDeadline (cn .deadline (ctx , effectiveTimeout )); err != nil {
836835 return err
837836 }
838837 } else {
839- // If getNetConn() returns nil, we still need to respect the timeout
840- // Return an error to prevent indefinite blocking
838+ // Connection is not available - return error
841839 return fmt .Errorf ("redis: conn[%d] not available for write operation" , cn .GetID ())
842840 }
843841 }
844842
843+ // Reset the buffered writer if needed, should not happen
845844 if cn .bw .Buffered () > 0 {
846845 if netConn := cn .getNetConn (); netConn != nil {
847846 cn .bw .Reset (netConn )
@@ -890,7 +889,7 @@ func (cn *Conn) MaybeHasData() bool {
890889
891890// deadline computes the effective deadline time based on context and timeout.
892891// It updates the usedAt timestamp to now.
893- // Uses cached time to avoid expensive syscall (max 100ms staleness is acceptable for deadline calculation).
892+ // Uses cached time to avoid expensive syscall (max 50ms staleness is acceptable for deadline calculation).
894893func (cn * Conn ) deadline (ctx context.Context , timeout time.Duration ) time.Time {
895894 // Use cached time for deadline calculation (called 2x per command: read + write)
896895 tm := time .Unix (0 , getCachedTimeNs ())
0 commit comments