Skip to content

Commit 7335ef6

Browse files
committed
chore: cleanup
1 parent dadd6d2 commit 7335ef6

File tree

4 files changed

+83
-85
lines changed

4 files changed

+83
-85
lines changed

cluster_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,9 @@ var _ = Describe("ClusterClient timeout", func() {
13361336
}, 2*pause).ShouldNot(HaveOccurred())
13371337
return nil
13381338
})
1339+
1340+
err := client.Close()
1341+
Expect(err).NotTo(HaveOccurred())
13391342
})
13401343

13411344
testTimeout()

extra/redisotel/tracing.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (th *tracingHook) DialHook(hook redis.DialHook) redis.DialHook {
9494

9595
conn, err := hook(ctx, network, addr)
9696
if err != nil {
97-
recordError(ctx, span, err)
97+
recordError(span, err)
9898
return nil, err
9999
}
100100
return conn, nil
@@ -118,7 +118,7 @@ func (th *tracingHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook {
118118
defer span.End()
119119

120120
if err := hook(ctx, cmd); err != nil {
121-
recordError(ctx, span, err)
121+
recordError(span, err)
122122
return err
123123
}
124124
return nil
@@ -147,14 +147,14 @@ func (th *tracingHook) ProcessPipelineHook(
147147
defer span.End()
148148

149149
if err := hook(ctx, cmds); err != nil {
150-
recordError(ctx, span, err)
150+
recordError(span, err)
151151
return err
152152
}
153153
return nil
154154
}
155155
}
156156

157-
func recordError(ctx context.Context, span trace.Span, err error) {
157+
func recordError(span trace.Span, err error) {
158158
if err != redis.Nil {
159159
span.RecordError(err)
160160
span.SetStatus(codes.Error, err.Error())

internal_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package redis
22

33
import (
4+
"context"
45
"fmt"
56
"testing"
67
"time"
78

9+
"github.com/go-redis/redis/v9/internal/pool"
10+
"github.com/go-redis/redis/v9/internal/proto"
11+
812
. "github.com/onsi/ginkgo"
913
. "github.com/onsi/gomega"
1014
)
@@ -158,3 +162,75 @@ func BenchmarkRingShardingRebalanceLocked(b *testing.B) {
158162
ring.sharding.rebalanceLocked()
159163
}
160164
}
165+
166+
//------------------------------------------------------------------------------
167+
168+
type timeoutErr struct {
169+
error
170+
}
171+
172+
func (e timeoutErr) Timeout() bool {
173+
return true
174+
}
175+
176+
func (e timeoutErr) Temporary() bool {
177+
return true
178+
}
179+
180+
func (e timeoutErr) Error() string {
181+
return "i/o timeout"
182+
}
183+
184+
var _ = Describe("withConn", func() {
185+
var client *Client
186+
187+
BeforeEach(func() {
188+
client = NewClient(&Options{
189+
PoolSize: 1,
190+
})
191+
})
192+
193+
AfterEach(func() {
194+
client.Close()
195+
})
196+
197+
It("should replace the connection in the pool when there is no error", func() {
198+
var conn *pool.Conn
199+
200+
client.withConn(ctx, func(ctx context.Context, c *pool.Conn) error {
201+
conn = c
202+
return nil
203+
})
204+
205+
newConn, err := client.connPool.Get(ctx)
206+
Expect(err).To(BeNil())
207+
Expect(newConn).To(Equal(conn))
208+
})
209+
210+
It("should replace the connection in the pool when there is an error not related to a bad connection", func() {
211+
var conn *pool.Conn
212+
213+
client.withConn(ctx, func(ctx context.Context, c *pool.Conn) error {
214+
conn = c
215+
return proto.RedisError("LOADING")
216+
})
217+
218+
newConn, err := client.connPool.Get(ctx)
219+
Expect(err).To(BeNil())
220+
Expect(newConn).To(Equal(conn))
221+
})
222+
223+
It("should remove the connection from the pool when it times out", func() {
224+
var conn *pool.Conn
225+
226+
client.withConn(ctx, func(ctx context.Context, c *pool.Conn) error {
227+
conn = c
228+
return timeoutErr{}
229+
})
230+
231+
newConn, err := client.connPool.Get(ctx)
232+
Expect(err).To(BeNil())
233+
Expect(newConn).NotTo(Equal(conn))
234+
Expect(client.connPool.Len()).To(Equal(1))
235+
})
236+
})

with_conn_test.go

Lines changed: 0 additions & 81 deletions
This file was deleted.

0 commit comments

Comments
 (0)