Skip to content
Open
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
10 changes: 7 additions & 3 deletions pkg/store/store_redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,19 +297,23 @@ func (rs *redisStore) Close() error {
return rs.cli.Close()
}

// updateSessionLastActivityScript atomically checks if the session key exists
// updateSessionLastActivityLua atomically checks if the session key exists
// and updates its last activity time in the sorted-set index.
// It uses EXISTS to verify presence rather than GET (which retrieved the
// actual value in older implementations). This saves data transfer overhead,
// if in future the value is required GET can be used instead.
var updateSessionLastActivityScript = redisv9.NewScript(`
// Both the redis and valkey stores run this script, so a session deleted
// between the check and the ZADD cannot be re-added to the index.
const updateSessionLastActivityLua = `
if redis.call("EXISTS", KEYS[1]) == 1 then
redis.call("ZADD", KEYS[2], ARGV[2], ARGV[1])
return 1
else
return 0
end
`)
`

var updateSessionLastActivityScript = redisv9.NewScript(updateSessionLastActivityLua)

// UpdateSessionLastActivity updates the last-activity index for the given session.
func (rs *redisStore) UpdateSessionLastActivity(ctx context.Context, sessionID string, at time.Time) error {
Expand Down
25 changes: 13 additions & 12 deletions pkg/store/store_valkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,13 @@ func (vs *valkeyStore) Close() error {
return nil
}

// UpdateSessionLastActivity updates the last-activity index for the given session
// updateSessionLastActivityValkeyScript is the valkey client's handle for
// updateSessionLastActivityLua (defined in store_redis.go).
var updateSessionLastActivityValkeyScript = valkey.NewLuaScript(updateSessionLastActivityLua)

// UpdateSessionLastActivity updates the last-activity index for the given session.
// The existence check and the ZADD run in one script, so a concurrent delete
// cannot leave an orphaned member in the last-activity index.
func (vs *valkeyStore) UpdateSessionLastActivity(ctx context.Context, sessionID string, at time.Time) error {
if sessionID == "" {
return errors.New("UpdateSessionLastActivity: sessionID is empty")
Expand All @@ -310,20 +316,15 @@ func (vs *valkeyStore) UpdateSessionLastActivity(ctx context.Context, sessionID
at = time.Now()
}
sessionKey := vs.sessionKey(sessionID)
existsResult, err := vs.cli.Do(ctx, vs.cli.B().Exists().Key(sessionKey).Build()).AsInt64()
res, err := updateSessionLastActivityValkeyScript.Exec(ctx, vs.cli,
[]string{sessionKey, vs.lastActivityIndexKey},
[]string{sessionID, strconv.FormatInt(at.Unix(), 10)},
).AsInt64()
if err != nil {
return fmt.Errorf("UpdateSessionLastActivity: valkey Exists failed: %w", err)
return fmt.Errorf("UpdateSessionLastActivity: script execution failed for sessionID %s: %w", sessionID, err)
}
if existsResult != 1 {
// Existing result must be 1
if res == 0 {
return ErrNotFound
}

zddCmd := vs.cli.B().Zadd().Key(vs.lastActivityIndexKey).ScoreMember().
ScoreMember(float64(at.Unix()), sessionID).Build()
err = vs.cli.Do(ctx, zddCmd).Error()
if err != nil {
return fmt.Errorf("UpdateSessionLastActivity: ZADD failed: %w", err)
}
return nil
}
64 changes: 64 additions & 0 deletions pkg/store/store_valkey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"errors"
"strings"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -360,3 +361,66 @@ func TestValkeyStore_UpdateSandboxLastActivity(t *testing.T) {
assert.Error(t, err)
assert.True(t, errors.Is(err, ErrNotFound))
}

// afterFirstDoClient wraps a valkey.Client and runs hook once, right after
// the first Do call returns. It lets a test land a concurrent operation
// between two round trips of the code under test.
type afterFirstDoClient struct {
valkey.Client
once sync.Once
hook func()
}

func (c *afterFirstDoClient) Do(ctx context.Context, cmd valkey.Completed) valkey.ValkeyResult {
resp := c.Client.Do(ctx, cmd)
c.once.Do(c.hook)
return resp
}

// TestValkeyStore_UpdateSessionLastActivity_ConcurrentDelete checks that a
// session deleted while its last activity is being updated does not leave an
// orphaned member in the last-activity index. The delete lands right after the
// first round trip of UpdateSessionLastActivity, both before and after the
// script is cached on the server (EVAL fallback vs. EVALSHA).
func TestValkeyStore_UpdateSessionLastActivity_ConcurrentDelete(t *testing.T) {
for _, tc := range []struct {
name string
scriptCached bool
}{
{name: "script not cached", scriptCached: false},
{name: "script cached", scriptCached: true},
} {
t.Run(tc.name, func(t *testing.T) {
ctx := context.Background()
c, mr := newValkeyTestClient(t)

now := time.Now().UTC().Truncate(time.Second)
assert.NoError(t, c.StoreSandbox(ctx, newTestSandbox("sb-1", "sess-1", now.Add(30*time.Minute))))
if tc.scriptCached {
assert.NoError(t, c.StoreSandbox(ctx, newTestSandbox("sb-0", "sess-0", now.Add(30*time.Minute))))
assert.NoError(t, c.UpdateSessionLastActivity(ctx, "sess-0", now))
}

// The delete runs on the unwrapped store, as the GC or a DELETE request would.
wrapped := *c
wrapped.cli = &afterFirstDoClient{
Client: c.cli,
hook: func() {
assert.NoError(t, c.DeleteSandboxBySessionID(ctx, "sess-1"))
},
}

err := wrapped.UpdateSessionLastActivity(ctx, "sess-1", now)
if err != nil {
assert.ErrorIs(t, err, ErrNotFound)
}

assert.False(t, mr.Exists(c.sessionKey("sess-1")), "session key should be deleted")
members, err := mr.ZMembers(c.lastActivityIndexKey)
if err != nil {
assert.ErrorIs(t, err, miniredis.ErrKeyNotFound)
}
assert.NotContains(t, members, "sess-1", "deleted session must not be re-added to the last-activity index")
})
}
}
Loading