go-redis icon indicating copy to clipboard operation
go-redis copied to clipboard

TTL duration overflow

Open tkrause opened this issue 8 months ago • 0 comments

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.

tkrause avatar Jun 18 '24 16:06 tkrause