@@ -18,9 +18,9 @@ import (
1818
1919var noDeadline = time.Time {}
2020
21- // Global time cache updated every 50ms by background goroutine.
21+ // Global time cache updated every 100ms by background goroutine.
2222// This avoids expensive time.Now() syscalls in hot paths like getEffectiveReadTimeout.
23- // Max staleness: 50ms , which is acceptable for timeout deadline checks (timeouts are typically 3-30 seconds).
23+ // Max staleness: 100ms , which is acceptable for timeout deadline checks (timeouts are typically 3-30 seconds).
2424var globalTimeCache struct {
2525 nowNs atomic.Int64
2626}
@@ -31,7 +31,7 @@ func init() {
3131
3232 // Start background updater
3333 go func () {
34- ticker := time .NewTicker (50 * time .Millisecond )
34+ ticker := time .NewTicker (100 * time .Millisecond )
3535 defer ticker .Stop ()
3636
3737 for range ticker .C {
@@ -41,12 +41,20 @@ func init() {
4141}
4242
4343// getCachedTimeNs returns the current time in nanoseconds from the global cache.
44- // This is updated every 50ms by a background goroutine, avoiding expensive syscalls.
45- // Max staleness: 50ms .
44+ // This is updated every 100ms by a background goroutine, avoiding expensive syscalls.
45+ // Max staleness: 100ms .
4646func getCachedTimeNs () int64 {
4747 return globalTimeCache .nowNs .Load ()
4848}
4949
50+ // GetCachedTimeNs returns the current time in nanoseconds from the global cache.
51+ // This is updated every 100ms by a background goroutine, avoiding expensive syscalls.
52+ // Max staleness: 100ms.
53+ // Exported for use by other packages that need fast time access.
54+ func GetCachedTimeNs () int64 {
55+ return getCachedTimeNs ()
56+ }
57+
5058// Global atomic counter for connection IDs
5159var connIDCounter uint64
5260
@@ -170,6 +178,9 @@ func (cn *Conn) UsedAt() time.Time {
170178 unixNano := atomic .LoadInt64 (& cn .usedAt )
171179 return time .Unix (0 , unixNano )
172180}
181+ func (cn * Conn ) UsedAtNs () int64 {
182+ return atomic .LoadInt64 (& cn .usedAt )
183+ }
173184
174185func (cn * Conn ) SetUsedAt (tm time.Time ) {
175186 atomic .StoreInt64 (& cn .usedAt , tm .UnixNano ())
@@ -488,7 +499,7 @@ func (cn *Conn) getEffectiveReadTimeout(normalTimeout time.Duration) time.Durati
488499 return time .Duration (readTimeoutNs )
489500 }
490501
491- // Use cached time to avoid expensive syscall (max 50ms staleness is acceptable for timeout checks)
502+ // Use cached time to avoid expensive syscall (max 100ms staleness is acceptable for timeout checks)
492503 nowNs := getCachedTimeNs ()
493504 // Check if deadline has passed
494505 if nowNs < deadlineNs {
@@ -522,7 +533,7 @@ func (cn *Conn) getEffectiveWriteTimeout(normalTimeout time.Duration) time.Durat
522533 return time .Duration (writeTimeoutNs )
523534 }
524535
525- // Use cached time to avoid expensive syscall (max 50ms staleness is acceptable for timeout checks)
536+ // Use cached time to avoid expensive syscall (max 100ms staleness is acceptable for timeout checks)
526537 nowNs := getCachedTimeNs ()
527538 // Check if deadline has passed
528539 if nowNs < deadlineNs {
@@ -879,7 +890,7 @@ func (cn *Conn) MaybeHasData() bool {
879890
880891// deadline computes the effective deadline time based on context and timeout.
881892// It updates the usedAt timestamp to now.
882- // Uses cached time to avoid expensive syscall (max 50ms staleness is acceptable for deadline calculation).
893+ // Uses cached time to avoid expensive syscall (max 100ms staleness is acceptable for deadline calculation).
883894func (cn * Conn ) deadline (ctx context.Context , timeout time.Duration ) time.Time {
884895 // Use cached time for deadline calculation (called 2x per command: read + write)
885896 tm := time .Unix (0 , getCachedTimeNs ())
0 commit comments