Skip to content

Commit 86c73a0

Browse files
committed
preallocated simple errors
1 parent 5c447f9 commit 86c73a0

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

osscluster.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package redis
33
import (
44
"context"
55
"crypto/tls"
6+
"errors"
67
"fmt"
78
"math"
89
"net"
@@ -29,7 +30,11 @@ const (
2930
minLatencyMeasurementInterval = 10 * time.Second
3031
)
3132

32-
var errClusterNoNodes = fmt.Errorf("redis: cluster has no nodes")
33+
var (
34+
errClusterNoNodes = errors.New("redis: cluster has no nodes")
35+
errNoWatchKeys = errors.New("redis: Watch requires at least one key")
36+
errWatchCrosslot = errors.New("redis: Watch requires all keys to be in the same slot")
37+
)
3338

3439
// ClusterOptions are used to configure a cluster client and should be
3540
// passed to NewClusterClient.
@@ -1838,14 +1843,13 @@ func (c *ClusterClient) cmdsMoved(
18381843

18391844
func (c *ClusterClient) Watch(ctx context.Context, fn func(*Tx) error, keys ...string) error {
18401845
if len(keys) == 0 {
1841-
return fmt.Errorf("redis: Watch requires at least one key")
1846+
return errNoWatchKeys
18421847
}
18431848

18441849
slot := hashtag.Slot(keys[0])
18451850
for _, key := range keys[1:] {
18461851
if hashtag.Slot(key) != slot {
1847-
err := fmt.Errorf("redis: Watch requires all keys to be in the same slot")
1848-
return err
1852+
return errWatchCrosslot
18491853
}
18501854
}
18511855

osscluster_router.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package redis
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"reflect"
78
"strings"
@@ -12,6 +13,14 @@ import (
1213
"github.com/redis/go-redis/v9/internal/routing"
1314
)
1415

16+
var (
17+
errInvalidCmdPointer = errors.New("redis: invalid command pointer")
18+
errNoCmdsToAggregate = errors.New("redis: no commands to aggregate")
19+
errNoResToAggregate = errors.New("redis: no results to aggregate")
20+
errInvalidCursorCmdArgsCount = errors.New("redis: FT.CURSOR command requires at least 3 arguments")
21+
errInvalidCursorIdType = errors.New("redis: invalid cursor ID type")
22+
)
23+
1524
// slotResult represents the result of executing a command on a specific slot
1625
type slotResult struct {
1726
cmd Cmder
@@ -275,12 +284,12 @@ func (c *ClusterClient) executeSpecialCommand(ctx context.Context, cmd Cmder, po
275284
func (c *ClusterClient) executeCursorCommand(ctx context.Context, cmd Cmder) error {
276285
args := cmd.Args()
277286
if len(args) < 4 {
278-
return fmt.Errorf("redis: FT.CURSOR command requires at least 3 arguments")
287+
return errInvalidCursorCmdArgsCount
279288
}
280289

281290
cursorID, ok := args[3].(string)
282291
if !ok {
283-
return fmt.Errorf("redis: invalid cursor ID type")
292+
return errInvalidCursorIdType
284293
}
285294

286295
// Route based on cursor ID to maintain stickiness
@@ -394,7 +403,7 @@ func (c *ClusterClient) aggregateMultiSlotResults(ctx context.Context, cmd Cmder
394403
// aggregateKeyedValues aggregates individual key-value pairs while preserving key order
395404
func (c *ClusterClient) aggregateKeyedValues(cmd Cmder, keyedResults map[string]routing.AggregatorResErr, keyOrder []string, policy *routing.CommandPolicy) error {
396405
if len(keyedResults) == 0 {
397-
return fmt.Errorf("redis: no results to aggregate")
406+
return errNoResToAggregate
398407
}
399408

400409
aggregator := c.createAggregator(policy, cmd, true)
@@ -419,7 +428,7 @@ func (c *ClusterClient) aggregateKeyedValues(cmd Cmder, keyedResults map[string]
419428
// aggregateResponses aggregates multiple shard responses
420429
func (c *ClusterClient) aggregateResponses(cmd Cmder, cmds []Cmder, policy *routing.CommandPolicy) error {
421430
if len(cmds) == 0 {
422-
return fmt.Errorf("redis: no commands to aggregate")
431+
return errNoCmdsToAggregate
423432
}
424433

425434
if len(cmds) == 1 {
@@ -958,7 +967,7 @@ func (c *ClusterClient) setCommandValue(cmd Cmder, value interface{}) error {
958967
func (c *ClusterClient) setCommandValueReflection(cmd Cmder, value interface{}) error {
959968
cmdValue := reflect.ValueOf(cmd)
960969
if cmdValue.Kind() != reflect.Ptr || cmdValue.IsNil() {
961-
return fmt.Errorf("redis: invalid command pointer")
970+
return errInvalidCmdPointer
962971
}
963972

964973
setValMethod := cmdValue.MethodByName("SetVal")

0 commit comments

Comments
 (0)