redis / go-redis

Redis Go client
https://redis.uptrace.dev
BSD 2-Clause "Simplified" License
19.6k stars 2.31k forks source link

TTL duration overflow #3031

Open tkrause opened 2 weeks ago

tkrause commented 2 weeks ago

Issue tracker is used for reporting bugs and discussing new features. Please use stackoverflow for supporting issues.

Expected Behavior

That retrieving a TTL for a key should return a positive value.

Current Behavior

When retrieving a TTL for key with a long duration, time.Duration overflows. For example, set a key in Redis with a TTL of 116637714045176. This library effectively executes the following:

import (
    "fmt"
    "time"
)

func main() {
    i := 116637714045176
    fmt.Println(time.Second * time.Duration(i))
}

The result will be -291314h41m29.494867968s which is no where near correct as it should be 32399365012.54889 hours.

This was discovered when writing a background scan job with this library to remove TTLs that were set incorrectly previously. Because of the overflow, these longer TTL values are not represented correctly but valid in Redis.

Possible Solution

The use of time.Duration should likely be replaced with int64 instead to properly match up with Redis as the time.Duration time is in nanoseconds.