Skip to content

Commit 436ec07

Browse files
authored
Merge branch 'master' into pool-timeout-test
2 parents 8c85492 + ba26e35 commit 436ec07

File tree

6 files changed

+50
-26
lines changed

6 files changed

+50
-26
lines changed

error.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ import (
1515
// ErrClosed performs any operation on the closed client will return this error.
1616
var ErrClosed = pool.ErrClosed
1717

18+
// ErrPoolExhausted is returned from a pool connection method
19+
// when the maximum number of database connections in the pool has been reached.
20+
var ErrPoolExhausted = pool.ErrPoolExhausted
21+
22+
// ErrPoolTimeout timed out waiting to get a connection from the connection pool.
23+
var ErrPoolTimeout = pool.ErrPoolTimeout
24+
1825
// HasErrorPrefix checks if the err is a Redis error and the message contains a prefix.
1926
func HasErrorPrefix(err error, prefix string) bool {
2027
var rErr Error

export_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import (
1111
"github.com/redis/go-redis/v9/internal/pool"
1212
)
1313

14-
var ErrPoolTimeout = pool.ErrPoolTimeout
15-
1614
func (c *baseClient) Pool() pool.Pooler {
1715
return c.connPool
1816
}

internal/util.go

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,7 @@ func isLower(s string) bool {
4949
}
5050

5151
func ReplaceSpaces(s string) string {
52-
// Pre-allocate a builder with the same length as s to minimize allocations.
53-
// This is a basic optimization; adjust the initial size based on your use case.
54-
var builder strings.Builder
55-
builder.Grow(len(s))
56-
57-
for _, char := range s {
58-
if char == ' ' {
59-
// Replace space with a hyphen.
60-
builder.WriteRune('-')
61-
} else {
62-
// Copy the character as-is.
63-
builder.WriteRune(char)
64-
}
65-
}
66-
67-
return builder.String()
52+
return strings.ReplaceAll(s, " ", "-")
6853
}
6954

7055
func GetAddr(addr string) string {

internal/util_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package internal
22

33
import (
4+
"runtime"
45
"strings"
56
"testing"
67

@@ -72,3 +73,36 @@ func TestGetAddr(t *testing.T) {
7273
Expect(GetAddr("127")).To(Equal(""))
7374
})
7475
}
76+
77+
func BenchmarkReplaceSpaces(b *testing.B) {
78+
version := runtime.Version()
79+
for i := 0; i < b.N; i++ {
80+
_ = ReplaceSpaces(version)
81+
}
82+
}
83+
84+
func ReplaceSpacesUseBuilder(s string) string {
85+
// Pre-allocate a builder with the same length as s to minimize allocations.
86+
// This is a basic optimization; adjust the initial size based on your use case.
87+
var builder strings.Builder
88+
builder.Grow(len(s))
89+
90+
for _, char := range s {
91+
if char == ' ' {
92+
// Replace space with a hyphen.
93+
builder.WriteRune('-')
94+
} else {
95+
// Copy the character as-is.
96+
builder.WriteRune(char)
97+
}
98+
}
99+
100+
return builder.String()
101+
}
102+
103+
func BenchmarkReplaceSpacesUseBuilder(b *testing.B) {
104+
version := runtime.Version()
105+
for i := 0; i < b.N; i++ {
106+
_ = ReplaceSpacesUseBuilder(version)
107+
}
108+
}

internal_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,14 +364,14 @@ var _ = Describe("ClusterClient", func() {
364364
It("select slot from args for GETKEYSINSLOT command", func() {
365365
cmd := NewStringSliceCmd(ctx, "cluster", "getkeysinslot", 100, 200)
366366

367-
slot := client.cmdSlot(context.Background(), cmd)
367+
slot := client.cmdSlot(cmd)
368368
Expect(slot).To(Equal(100))
369369
})
370370

371371
It("select slot from args for COUNTKEYSINSLOT command", func() {
372372
cmd := NewStringSliceCmd(ctx, "cluster", "countkeysinslot", 100)
373373

374-
slot := client.cmdSlot(context.Background(), cmd)
374+
slot := client.cmdSlot(cmd)
375375
Expect(slot).To(Equal(100))
376376
})
377377
})

osscluster.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,7 @@ func (c *ClusterClient) Process(ctx context.Context, cmd Cmder) error {
981981
}
982982

983983
func (c *ClusterClient) process(ctx context.Context, cmd Cmder) error {
984-
slot := c.cmdSlot(ctx, cmd)
984+
slot := c.cmdSlot(cmd)
985985
var node *clusterNode
986986
var moved bool
987987
var ask bool
@@ -1329,7 +1329,7 @@ func (c *ClusterClient) mapCmdsByNode(ctx context.Context, cmdsMap *cmdsMap, cmd
13291329

13301330
if c.opt.ReadOnly && c.cmdsAreReadOnly(ctx, cmds) {
13311331
for _, cmd := range cmds {
1332-
slot := c.cmdSlot(ctx, cmd)
1332+
slot := c.cmdSlot(cmd)
13331333
node, err := c.slotReadOnlyNode(state, slot)
13341334
if err != nil {
13351335
return err
@@ -1340,7 +1340,7 @@ func (c *ClusterClient) mapCmdsByNode(ctx context.Context, cmdsMap *cmdsMap, cmd
13401340
}
13411341

13421342
for _, cmd := range cmds {
1343-
slot := c.cmdSlot(ctx, cmd)
1343+
slot := c.cmdSlot(cmd)
13441344
node, err := state.slotMasterNode(slot)
13451345
if err != nil {
13461346
return err
@@ -1540,7 +1540,7 @@ func (c *ClusterClient) processTxPipeline(ctx context.Context, cmds []Cmder) err
15401540
func (c *ClusterClient) mapCmdsBySlot(ctx context.Context, cmds []Cmder) map[int][]Cmder {
15411541
cmdsMap := make(map[int][]Cmder)
15421542
for _, cmd := range cmds {
1543-
slot := c.cmdSlot(ctx, cmd)
1543+
slot := c.cmdSlot(cmd)
15441544
cmdsMap[slot] = append(cmdsMap[slot], cmd)
15451545
}
15461546
return cmdsMap
@@ -1569,7 +1569,7 @@ func (c *ClusterClient) processTxPipelineNode(
15691569
}
15701570

15711571
func (c *ClusterClient) processTxPipelineNodeConn(
1572-
ctx context.Context, node *clusterNode, cn *pool.Conn, cmds []Cmder, failedCmds *cmdsMap,
1572+
ctx context.Context, _ *clusterNode, cn *pool.Conn, cmds []Cmder, failedCmds *cmdsMap,
15731573
) error {
15741574
if err := cn.WithWriter(c.context(ctx), c.opt.WriteTimeout, func(wr *proto.Writer) error {
15751575
return writeCmds(wr, cmds)
@@ -1858,7 +1858,7 @@ func (c *ClusterClient) cmdInfo(ctx context.Context, name string) *CommandInfo {
18581858
return info
18591859
}
18601860

1861-
func (c *ClusterClient) cmdSlot(ctx context.Context, cmd Cmder) int {
1861+
func (c *ClusterClient) cmdSlot(cmd Cmder) int {
18621862
args := cmd.Args()
18631863
if args[0] == "cluster" && (args[1] == "getkeysinslot" || args[1] == "countkeysinslot") {
18641864
return args[2].(int)

0 commit comments

Comments
 (0)