Skip to content

Commit e498c9a

Browse files
authored
Merge branch 'master' into jatin/add-custom-attrs
2 parents 140df6f + 8fadbef commit e498c9a

File tree

5 files changed

+67
-1
lines changed

5 files changed

+67
-1
lines changed

error.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ type Error interface {
3838

3939
var _ Error = proto.RedisError("")
4040

41+
func isContextError(err error) bool {
42+
switch err {
43+
case context.Canceled, context.DeadlineExceeded:
44+
return true
45+
default:
46+
return false
47+
}
48+
}
49+
4150
func shouldRetry(err error, retryTimeout bool) bool {
4251
switch err {
4352
case io.EOF, io.ErrUnexpectedEOF:

osscluster.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1350,7 +1350,9 @@ func (c *ClusterClient) processPipelineNode(
13501350
_ = node.Client.withProcessPipelineHook(ctx, cmds, func(ctx context.Context, cmds []Cmder) error {
13511351
cn, err := node.Client.getConn(ctx)
13521352
if err != nil {
1353-
node.MarkAsFailing()
1353+
if !isContextError(err) {
1354+
node.MarkAsFailing()
1355+
}
13541356
_ = c.mapCmdsByNode(ctx, failedCmds, cmds)
13551357
setCmdsErr(cmds, err)
13561358
return err

osscluster_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,39 @@ var _ = Describe("ClusterClient", func() {
539539
AfterEach(func() {})
540540

541541
assertPipeline()
542+
543+
It("doesn't fail node with context.Canceled error", func() {
544+
ctx, cancel := context.WithCancel(context.Background())
545+
cancel()
546+
pipe.Set(ctx, "A", "A_value", 0)
547+
_, err := pipe.Exec(ctx)
548+
549+
Expect(err).To(HaveOccurred())
550+
Expect(errors.Is(err, context.Canceled)).To(BeTrue())
551+
552+
clientNodes, _ := client.Nodes(ctx, "A")
553+
554+
for _, node := range clientNodes {
555+
Expect(node.Failing()).To(BeFalse())
556+
}
557+
})
558+
559+
It("doesn't fail node with context.DeadlineExceeded error", func() {
560+
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Nanosecond)
561+
defer cancel()
562+
563+
pipe.Set(ctx, "A", "A_value", 0)
564+
_, err := pipe.Exec(ctx)
565+
566+
Expect(err).To(HaveOccurred())
567+
Expect(errors.Is(err, context.DeadlineExceeded)).To(BeTrue())
568+
569+
clientNodes, _ := client.Nodes(ctx, "A")
570+
571+
for _, node := range clientNodes {
572+
Expect(node.Failing()).To(BeFalse())
573+
}
574+
})
542575
})
543576

544577
Describe("with TxPipeline", func() {

universal.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ func (o *UniversalOptions) Failover() *FailoverOptions {
163163

164164
TLSConfig: o.TLSConfig,
165165

166+
ReplicaOnly: o.ReadOnly,
167+
166168
DisableIndentity: o.DisableIndentity,
167169
IdentitySuffix: o.IdentitySuffix,
168170
UnstableResp3: o.UnstableResp3,

universal_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,25 @@ var _ = Describe("UniversalClient", func() {
6060
Expect(a).ToNot(Panic())
6161
})
6262

63+
It("should connect to failover servers on slaves when readonly Options is ok", Label("NonRedisEnterprise"), func() {
64+
client = redis.NewUniversalClient(&redis.UniversalOptions{
65+
MasterName: sentinelName,
66+
Addrs: sentinelAddrs,
67+
ReadOnly: true,
68+
})
69+
Expect(client.Ping(ctx).Err()).NotTo(HaveOccurred())
70+
71+
roleCmd := client.Do(ctx, "ROLE")
72+
role, err := roleCmd.Result()
73+
Expect(err).NotTo(HaveOccurred())
74+
75+
roleSlice, ok := role.([]interface{})
76+
Expect(ok).To(BeTrue())
77+
Expect(roleSlice[0]).To(Equal("slave"))
78+
79+
err = client.Set(ctx, "somekey", "somevalue", 0).Err()
80+
Expect(err).To(HaveOccurred())
81+
})
6382
It("should connect to clusters if IsClusterMode is set even if only a single address is provided", Label("NonRedisEnterprise"), func() {
6483
client = redis.NewUniversalClient(&redis.UniversalOptions{
6584
Addrs: []string{cluster.addrs()[0]},
@@ -77,3 +96,4 @@ var _ = Describe("UniversalClient", func() {
7796
Expect(client.ClusterSlots(ctx).Val()).To(HaveLen(3))
7897
})
7998
})
99+

0 commit comments

Comments
 (0)