@@ -24,6 +24,12 @@ import (
24
24
25
25
var errRingShardsDown = errors .New ("redis: all ring shards are down" )
26
26
27
+ // defaultHeartbeatFn is the default function used to check the shard liveness
28
+ var defaultHeartbeatFn = func (ctx context.Context , client * Client ) bool {
29
+ err := client .Ping (ctx ).Err ()
30
+ return err == nil || err == pool .ErrPoolTimeout
31
+ }
32
+
27
33
//------------------------------------------------------------------------------
28
34
29
35
type ConsistentHash interface {
@@ -56,10 +62,14 @@ type RingOptions struct {
56
62
// ClientName will execute the `CLIENT SETNAME ClientName` command for each conn.
57
63
ClientName string
58
64
59
- // Frequency of PING commands sent to check shards availability.
65
+ // Frequency of executing HeartbeatFn to check shards availability.
60
66
// Shard is considered down after 3 subsequent failed checks.
61
67
HeartbeatFrequency time.Duration
62
68
69
+ // A function used to check the shard liveness
70
+ // if not set, defaults to defaultHeartbeatFn
71
+ HeartbeatFn func (ctx context.Context , client * Client ) bool
72
+
63
73
// NewConsistentHash returns a consistent hash that is used
64
74
// to distribute keys across the shards.
65
75
//
@@ -157,6 +167,10 @@ func (opt *RingOptions) init() {
157
167
opt .HeartbeatFrequency = 500 * time .Millisecond
158
168
}
159
169
170
+ if opt .HeartbeatFn == nil {
171
+ opt .HeartbeatFn = defaultHeartbeatFn
172
+ }
173
+
160
174
if opt .NewConsistentHash == nil {
161
175
opt .NewConsistentHash = newRendezvous
162
176
}
@@ -474,8 +488,7 @@ func (c *ringSharding) Heartbeat(ctx context.Context, frequency time.Duration) {
474
488
475
489
// note: `c.List()` return a shadow copy of `[]*ringShard`.
476
490
for _ , shard := range c .List () {
477
- err := shard .Client .Ping (ctx ).Err ()
478
- isUp := err == nil || err == pool .ErrPoolTimeout
491
+ isUp := c .opt .HeartbeatFn (ctx , shard .Client )
479
492
if shard .Vote (isUp ) {
480
493
internal .Logger .Printf (ctx , "ring shard state changed: %s" , shard )
481
494
rebalance = true
0 commit comments