Skip to content

Commit

Permalink
Fix KeyValue TTL (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
tbuchaillot authored Jan 22, 2024
1 parent 6fde14b commit 5f62fd2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
8 changes: 7 additions & 1 deletion temporal/internal/driver/redisv9/keyvalue.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,20 @@ func (r *RedisV9) Expire(ctx context.Context, key string, expiration time.Durati
// TTL returns the remaining time to live of a key that has a timeout
func (r *RedisV9) TTL(ctx context.Context, key string) (int64, error) {
if key == "" {
return 0, temperr.KeyEmpty
return -2, temperr.KeyEmpty
}

duration, err := r.client.TTL(ctx, key).Result()
if err != nil {
return 0, err
}

// since redis-go v8.3.1, if there's no expiration or the key doesn't exists,
// the ttl returned is measured in nanoseconds
if duration.Nanoseconds() == -1 || duration.Nanoseconds() == -2 {
return duration.Nanoseconds(), nil
}

return int64(duration.Seconds()), nil
}

Expand Down
30 changes: 27 additions & 3 deletions temporal/keyvalue/keyvalue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,15 +505,39 @@ func TestKeyValue_TTL(t *testing.T) {
expectedErr: nil,
},
{
name: "non_existing_key",
name: "existing_non_exipiring_key",
setup: func(db KeyValue) {
err := db.Set(context.Background(), "key2", "value1", -1*time.Second)
if err != nil {
t.Fatalf("Set() error = %v", err)
}
},
key: "key2",
expectedTTL: 0,
expectedTTL: -1,
expectedErr: nil,
},
{
name: "key_without_ttl",
setup: func(db KeyValue) {
err := db.Set(context.Background(), "key_without_ttl", "value1", 0*time.Second)
if err != nil {
t.Fatalf("Set() error = %v", err)
}
},
key: "key_without_ttl",
expectedTTL: -1,
expectedErr: nil,
},
{
name: "non_existing_key",
key: "non_existing_key",
expectedTTL: -2,
expectedErr: nil,
},
{
name: "empty_key",
key: "",
expectedTTL: 0,
expectedTTL: -2,
expectedErr: temperr.KeyEmpty,
},
}
Expand Down

0 comments on commit 5f62fd2

Please sign in to comment.