Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix KeyValue TTL #103

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading