Skip to content

Commit b9ba5a4

Browse files
authored
fix(spx-backend): co-locate quota keys in Redis Cluster using hash tags (#2494)
This follows up on #2483. Signed-off-by: Aofei Sheng <aofei@aofeisheng.com>
1 parent a108716 commit b9ba5a4

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

spx-backend/internal/authz/quota/redis.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,5 +213,6 @@ func quotaKey(userID int64, policy authz.QuotaPolicy) string {
213213
if name == "" {
214214
name = string(policy.Resource)
215215
}
216-
return fmt.Sprintf("%d:%s", userID, name)
216+
// Use a hash tag to keep all quota keys for the same user in one hash slot on Redis Cluster.
217+
return fmt.Sprintf("quota:{user:%d}:%s", userID, name)
217218
}

spx-backend/internal/authz/quota/redis_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,12 +327,12 @@ func TestQuotaKey(t *testing.T) {
327327
t.Run("UsesPolicyName", func(t *testing.T) {
328328
policy := authz.QuotaPolicy{Name: "copilotMessage:limit", Resource: authz.ResourceCopilotMessage}
329329
key := quotaKey(123, policy)
330-
assert.Equal(t, "123:copilotMessage:limit", key)
330+
assert.Equal(t, "quota:{user:123}:copilotMessage:limit", key)
331331
})
332332

333333
t.Run("FallsBackToResourceWhenNameEmpty", func(t *testing.T) {
334334
policy := authz.QuotaPolicy{Name: "", Resource: authz.ResourceAIDescription}
335335
key := quotaKey(456, policy)
336-
assert.Equal(t, "456:aiDescription", key)
336+
assert.Equal(t, "quota:{user:456}:aiDescription", key)
337337
})
338338
}

0 commit comments

Comments
 (0)