Skip to content

Set cluster slot for scan commands, rather than random #2623

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

Merged
merged 3 commits into from
Jun 24, 2025
Merged
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: 8 additions & 0 deletions generic_commands.go
Original file line number Diff line number Diff line change
@@ -3,6 +3,8 @@ package redis
import (
"context"
"time"

"github.com/redis/go-redis/v9/internal/hashtag"
)

type GenericCmdable interface {
@@ -363,6 +365,9 @@ func (c cmdable) Scan(ctx context.Context, cursor uint64, match string, count in
args = append(args, "count", count)
}
cmd := NewScanCmd(ctx, c, args...)
if hashtag.Present(match) {
cmd.SetFirstKeyPos(3)
}
_ = c(ctx, cmd)
return cmd
}
@@ -379,6 +384,9 @@ func (c cmdable) ScanType(ctx context.Context, cursor uint64, match string, coun
args = append(args, "type", keyType)
}
cmd := NewScanCmd(ctx, c, args...)
if hashtag.Present(match) {
cmd.SetFirstKeyPos(3)
}
_ = c(ctx, cmd)
return cmd
}
8 changes: 8 additions & 0 deletions hash_commands.go
Original file line number Diff line number Diff line change
@@ -3,6 +3,8 @@ package redis
import (
"context"
"time"

"github.com/redis/go-redis/v9/internal/hashtag"
)

type HashCmdable interface {
@@ -192,6 +194,9 @@ func (c cmdable) HScan(ctx context.Context, key string, cursor uint64, match str
args = append(args, "count", count)
}
cmd := NewScanCmd(ctx, c, args...)
if hashtag.Present(match) {
cmd.SetFirstKeyPos(4)
}
_ = c(ctx, cmd)
return cmd
}
@@ -211,6 +216,9 @@ func (c cmdable) HScanNoValues(ctx context.Context, key string, cursor uint64, m
}
args = append(args, "novalues")
cmd := NewScanCmd(ctx, c, args...)
if hashtag.Present(match) {
cmd.SetFirstKeyPos(4)
}
_ = c(ctx, cmd)
return cmd
}
12 changes: 12 additions & 0 deletions internal/hashtag/hashtag.go
Original file line number Diff line number Diff line change
@@ -56,6 +56,18 @@ func Key(key string) string {
return key
}

func Present(key string) bool {
if key == "" {
return false
}
if s := strings.IndexByte(key, '{'); s > -1 {
if e := strings.IndexByte(key[s+1:], '}'); e > 0 {
return true
}
}
return false
}

func RandomSlot() int {
return rand.Intn(slotNumber)
}
25 changes: 25 additions & 0 deletions internal/hashtag/hashtag_test.go
Original file line number Diff line number Diff line change
@@ -69,3 +69,28 @@ var _ = Describe("HashSlot", func() {
}
})
})

var _ = Describe("Present", func() {
It("should calculate hash slots", func() {
tests := []struct {
key string
present bool
}{
{"123456789", false},
{"{}foo", false},
{"foo{}", false},
{"foo{}{bar}", false},
{"", false},
{string([]byte{83, 153, 134, 118, 229, 214, 244, 75, 140, 37, 215, 215}), false},
{"foo{bar}", true},
{"{foo}bar", true},
{"{user1000}.following", true},
{"foo{{bar}}zap", true},
{"foo{bar}{zap}", true},
}

for _, test := range tests {
Expect(Present(test.key)).To(Equal(test.present), "for %s", test.key)
}
})
})
9 changes: 8 additions & 1 deletion set_commands.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package redis

import "context"
import (
"context"

"github.com/redis/go-redis/v9/internal/hashtag"
)

type SetCmdable interface {
SAdd(ctx context.Context, key string, members ...interface{}) *IntCmd
@@ -211,6 +215,9 @@ func (c cmdable) SScan(ctx context.Context, key string, cursor uint64, match str
args = append(args, "count", count)
}
cmd := NewScanCmd(ctx, c, args...)
if hashtag.Present(match) {
cmd.SetFirstKeyPos(4)
}
_ = c(ctx, cmd)
return cmd
}
5 changes: 5 additions & 0 deletions sortedset_commands.go
Original file line number Diff line number Diff line change
@@ -4,6 +4,8 @@ import (
"context"
"strings"
"time"

"github.com/redis/go-redis/v9/internal/hashtag"
)

type SortedSetCmdable interface {
@@ -719,6 +721,9 @@ func (c cmdable) ZScan(ctx context.Context, key string, cursor uint64, match str
args = append(args, "count", count)
}
cmd := NewScanCmd(ctx, c, args...)
if hashtag.Present(match) {
cmd.SetFirstKeyPos(4)
}
_ = c(ctx, cmd)
return cmd
}