Skip to content

Commit 6390822

Browse files
authored
fix(time): remove cached time optimization (#3611)
1 parent 1bb9e0d commit 6390822

File tree

3 files changed

+8
-87
lines changed

3 files changed

+8
-87
lines changed

internal/pool/conn.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,17 @@ var (
2727
errConnNotAvailableForWrite = errors.New("redis: connection not available for write operation")
2828
)
2929

30-
// getCachedTimeNs returns the current time in nanoseconds from the global cache.
31-
// This is updated every 50ms by a background goroutine, avoiding expensive syscalls.
32-
// Max staleness: 50ms.
30+
// getCachedTimeNs returns the current time in nanoseconds.
31+
// This function previously used a global cache updated by a background goroutine,
32+
// but that caused unnecessary CPU usage when the client was idle (ticker waking up
33+
// the scheduler every 50ms). We now use time.Now() directly, which is fast enough
34+
// on modern systems (vDSO on Linux) and only adds ~1-2% overhead in extreme
35+
// high-concurrency benchmarks while eliminating idle CPU usage.
3336
func getCachedTimeNs() int64 {
34-
return globalTimeCache.nowNs.Load()
37+
return time.Now().UnixNano()
3538
}
3639

37-
// GetCachedTimeNs returns the current time in nanoseconds from the global cache.
38-
// This is updated every 50ms by a background goroutine, avoiding expensive syscalls.
39-
// Max staleness: 50ms.
40+
// GetCachedTimeNs returns the current time in nanoseconds.
4041
// Exported for use by other packages that need fast time access.
4142
func GetCachedTimeNs() int64 {
4243
return getCachedTimeNs()

internal/pool/global_time_cache.go

Lines changed: 0 additions & 74 deletions
This file was deleted.

internal/pool/pool.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,6 @@ func NewConnPool(opt *Options) *ConnPool {
178178
p.connsMu.Unlock()
179179
}
180180

181-
startGlobalTimeCache()
182-
subscribeToGlobalTimeCache()
183-
184181
return p
185182
}
186183

@@ -981,9 +978,6 @@ func (p *ConnPool) Close() error {
981978
return ErrClosed
982979
}
983980

984-
unsubscribeFromGlobalTimeCache()
985-
stopGlobalTimeCache()
986-
987981
var firstErr error
988982
p.connsMu.Lock()
989983
for _, cn := range p.conns {

0 commit comments

Comments
 (0)