Skip to content

Commit 810c129

Browse files
committed
100ms -> 50ms
1 parent 54281d6 commit 810c129

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

internal/pool/conn.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ import (
1818

1919
var 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).
2424
var 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.
5454
func 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 {
725725
func (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).
894893
func (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())

internal/pool/conn_used_at_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestConn_UsedAtUpdatedOnRead(t *testing.T) {
2222
// Get initial usedAt time
2323
initialUsedAt := cn.UsedAt()
2424

25-
// Wait at least 100ms to ensure time difference (usedAt has ~50ms precision from cached time)
25+
// Wait at least 50ms to ensure time difference (usedAt has ~50ms precision from cached time)
2626
time.Sleep(100 * time.Millisecond)
2727

2828
// Simulate a read operation by calling WithReader
@@ -45,10 +45,10 @@ func TestConn_UsedAtUpdatedOnRead(t *testing.T) {
4545
initialUsedAt, updatedUsedAt)
4646
}
4747

48-
// Verify the difference is reasonable (should be around 100ms, accounting for ~50ms cache precision)
48+
// Verify the difference is reasonable (should be around 50ms, accounting for ~50ms cache precision)
4949
diff := updatedUsedAt.Sub(initialUsedAt)
5050
if diff < 50*time.Millisecond || diff > 200*time.Millisecond {
51-
t.Errorf("Expected usedAt difference to be around 100ms (±50ms for cache), got %v", diff)
51+
t.Errorf("Expected usedAt difference to be around 50ms (±50ms for cache), got %v", diff)
5252
}
5353
}
5454

0 commit comments

Comments
 (0)