Skip to content

Commit edc01b2

Browse files
committed
modify Tips and command pplicy in commandInfo (#5)
1 parent 9fb1405 commit edc01b2

File tree

4 files changed

+36
-49
lines changed

4 files changed

+36
-49
lines changed

command.go

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3479,7 +3479,7 @@ type CommandInfo struct {
34793479
LastKeyPos int8
34803480
StepCount int8
34813481
ReadOnly bool
3482-
Tips map[string]string
3482+
Tips *routing.CommandPolicy
34833483
}
34843484

34853485
type CommandsInfoCmd struct {
@@ -3612,8 +3612,7 @@ func (cmd *CommandsInfoCmd) readReply(rd *proto.Reader) error {
36123612
return err
36133613
}
36143614

3615-
cmdInfo.Tips = make(map[string]string, tipsLen)
3616-
3615+
rawTips := make(map[string]string, tipsLen)
36173616
for f := 0; f < tipsLen; f++ {
36183617
tip, err := rd.ReadString()
36193618
if err != nil {
@@ -3622,7 +3621,7 @@ func (cmd *CommandsInfoCmd) readReply(rd *proto.Reader) error {
36223621

36233622
// Handle tips that don't have a colon (like "nondeterministic_output")
36243623
if !strings.Contains(tip, ":") {
3625-
cmdInfo.Tips[tip] = ""
3624+
rawTips[tip] = ""
36263625
continue
36273626
}
36283627

@@ -3631,8 +3630,9 @@ func (cmd *CommandsInfoCmd) readReply(rd *proto.Reader) error {
36313630
if !ok {
36323631
return fmt.Errorf("redis: unexpected tip %q in COMMAND reply", tip)
36333632
}
3634-
cmdInfo.Tips[k] = v
3633+
rawTips[k] = v
36353634
}
3635+
cmdInfo.Tips = parseCommandPolicies(rawTips)
36363636

36373637
if err := rd.DiscardNext(); err != nil {
36383638
return err
@@ -3684,47 +3684,33 @@ func (c *cmdsInfoCache) Get(ctx context.Context) (map[string]*CommandInfo, error
36843684
}
36853685

36863686
// ------------------------------------------------------------------------------
3687-
var BuiltinPolicies = map[string]routing.CommandPolicy{
3688-
"ft.create": {Request: routing.ReqSpecial, Response: routing.RespAllSucceeded},
3689-
"ft.alter": {Request: routing.ReqSpecial, Response: routing.RespAllSucceeded},
3690-
"ft.drop": {Request: routing.ReqSpecial, Response: routing.RespAllSucceeded},
3691-
3692-
"mset": {Request: routing.ReqMultiShard, Response: routing.RespAllSucceeded},
3693-
"mget": {Request: routing.ReqMultiShard, Response: routing.RespSpecial},
3694-
"del": {Request: routing.ReqMultiShard, Response: routing.RespAggSum},
3695-
}
3696-
3697-
func newCommandPolicies(commandInfo map[string]*CommandInfo) map[string]routing.CommandPolicy {
3698-
3699-
table := make(map[string]routing.CommandPolicy, len(commandInfo))
3687+
const requestPolicy = "request_policy"
3688+
const responsePolicy = "response_policy"
37003689

3701-
for name, info := range commandInfo {
3702-
req := routing.ReqDefault
3703-
resp := routing.RespAllSucceeded
3690+
func parseCommandPolicies(commandInfoTips map[string]string) *routing.CommandPolicy {
3691+
req := routing.ReqDefault
3692+
resp := routing.RespAllSucceeded
37043693

3705-
if tips := info.Tips; tips != nil {
3706-
if v, ok := tips["request_policy"]; ok {
3707-
if p, err := routing.ParseRequestPolicy(v); err == nil {
3708-
req = p
3709-
}
3694+
if commandInfoTips != nil {
3695+
if v, ok := commandInfoTips[requestPolicy]; ok {
3696+
if p, err := routing.ParseRequestPolicy(v); err == nil {
3697+
req = p
37103698
}
3711-
if v, ok := tips["response_policy"]; ok {
3712-
if p, err := routing.ParseResponsePolicy(v); err == nil {
3713-
resp = p
3714-
}
3699+
}
3700+
if v, ok := commandInfoTips[responsePolicy]; ok {
3701+
if p, err := routing.ParseResponsePolicy(v); err == nil {
3702+
resp = p
37153703
}
3716-
} else {
3717-
return BuiltinPolicies
37183704
}
3719-
table[name] = routing.CommandPolicy{Request: req, Response: resp}
37203705
}
3721-
3722-
if len(table) == 0 {
3723-
for k, v := range BuiltinPolicies {
3724-
table[k] = v
3706+
tips := make(map[string]string, len(commandInfoTips))
3707+
for k, v := range commandInfoTips {
3708+
if k == requestPolicy || k == responsePolicy {
3709+
continue
37253710
}
3711+
tips[k] = v
37263712
}
3727-
return table
3713+
return &routing.CommandPolicy{Request: req, Response: resp, Tips: tips}
37283714
}
37293715

37303716
//------------------------------------------------------------------------------

commands_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/redis/go-redis/v9"
1515
"github.com/redis/go-redis/v9/internal/proto"
16+
"github.com/redis/go-redis/v9/internal/routing"
1617
)
1718

1819
type TimeValue struct {
@@ -664,13 +665,13 @@ var _ = Describe("Commands", func() {
664665

665666
cmd := cmds["touch"]
666667
Expect(cmd.Name).To(Equal("touch"))
667-
Expect(cmd.Tips["request_policy"]).To(Equal("multi_shard"))
668-
Expect(cmd.Tips["response_policy"]).To(Equal("agg_sum"))
668+
Expect(cmd.Tips.Request).To(Equal(routing.ReqMultiShard))
669+
Expect(cmd.Tips.Response).To(Equal(routing.RespAggSum))
669670

670671
cmd = cmds["flushall"]
671672
Expect(cmd.Name).To(Equal("flushall"))
672-
Expect(cmd.Tips["request_policy"]).To(Equal("all_shards"))
673-
Expect(cmd.Tips["response_policy"]).To(Equal("all_succeeded"))
673+
Expect(cmd.Tips.Request).To(Equal(routing.ReqAllShards))
674+
Expect(cmd.Tips.Response).To(Equal(routing.RespAllSucceeded))
674675
})
675676

676677
It("should return all command names", func() {

internal/routing/policy.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,7 @@ func ParseResponsePolicy(raw string) (ResponsePolicy, error) {
115115
type CommandPolicy struct {
116116
Request RequestPolicy
117117
Response ResponsePolicy
118+
// Tips that are not request_policy or response_policy
119+
// e.g nondeterministic_output, nondeterministic_output_order.
120+
Tips map[string]string
118121
}

osscluster.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"github.com/redis/go-redis/v9/internal/pool"
2121
"github.com/redis/go-redis/v9/internal/proto"
2222
"github.com/redis/go-redis/v9/internal/rand"
23-
"github.com/redis/go-redis/v9/internal/routing"
2423
)
2524

2625
const (
@@ -938,11 +937,10 @@ func (c *clusterStateHolder) ReloadOrGet(ctx context.Context) (*clusterState, er
938937
// or more underlying connections. It's safe for concurrent use by
939938
// multiple goroutines.
940939
type ClusterClient struct {
941-
opt *ClusterOptions
942-
nodes *clusterNodes
943-
state *clusterStateHolder
944-
cmdsInfoCache *cmdsInfoCache
945-
commandPolicies map[string]routing.CommandPolicy
940+
opt *ClusterOptions
941+
nodes *clusterNodes
942+
state *clusterStateHolder
943+
cmdsInfoCache *cmdsInfoCache
946944
cmdable
947945
hooksMixin
948946
}
@@ -962,7 +960,6 @@ func NewClusterClient(opt *ClusterOptions) *ClusterClient {
962960

963961
c.state = newClusterStateHolder(c.loadState)
964962
c.cmdsInfoCache = newCmdsInfoCache(c.cmdsInfo)
965-
c.commandPolicies = newCommandPolicies(c.cmdsInfoCache.cmds)
966963
c.cmdable = c.Process
967964
c.initHooks(hooks{
968965
dial: nil,

0 commit comments

Comments
 (0)