Skip to content

Commit

Permalink
fix redisKey and ZCount (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
leeym authored May 22, 2024
1 parent 6804c15 commit 9d9ccea
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion concurrent_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (c *ConcurrentBufferRedis) Add(ctx context.Context, key string) (int64, err
Score: float64(now.UnixNano()),
Member: key,
})
countCmd = pipeliner.ZCount(ctx, c.key, "-inf", "+inf")
countCmd = pipeliner.ZCard(ctx, c.key)
return nil
})
}()
Expand Down
8 changes: 7 additions & 1 deletion tokenbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,14 @@ const (
redisKeyTBVersion = "version"
)

// If we do use cluster client and if the cluster is large enough, it is possible that when accessing multiple keys
// in leaky bucket or token bucket, these keys might go different slots and it will fail with error message
// `CROSSSLOT Keys in request don't hash to the same slot`. Adding hash tags in redisKey will force them into the
// same slot for keys with the same prefix.
//
// https://redis.io/docs/latest/operate/oss_and_stack/reference/cluster-spec/#hash-tags
func redisKey(prefix, key string) string {
return fmt.Sprintf("%s/%s", prefix, key)
return fmt.Sprintf("{%s}%s", prefix, key)
}

// TokenBucketRedis is a Redis implementation of a TokenBucketStateBackend.
Expand Down

0 comments on commit 9d9ccea

Please sign in to comment.