mirror of
https://github.com/redis/go-redis.git
synced 2025-07-28 06:42:00 +03:00
Add RingOptions.OnNewShard
This commit is contained in:
18
ring.go
18
ring.go
@ -56,6 +56,9 @@ type RingOptions struct {
|
||||
// See https://arxiv.org/abs/1406.2294 for reference
|
||||
HashReplicas int
|
||||
|
||||
// Optional hook that is called when a new shard is created.
|
||||
OnNewShard func(*Client)
|
||||
|
||||
// Following options are copied from Options struct.
|
||||
|
||||
OnConnect func(*Conn) error
|
||||
@ -376,9 +379,8 @@ func NewRing(opt *RingOptions) *Ring {
|
||||
ring.cmdsInfoCache = newCmdsInfoCache(ring.cmdsInfo)
|
||||
|
||||
for name, addr := range opt.Addrs {
|
||||
clopt := opt.clientOptions(name)
|
||||
clopt.Addr = addr
|
||||
ring.shards.Add(name, NewClient(clopt))
|
||||
shard := newRingShard(opt, name, addr)
|
||||
ring.shards.Add(name, shard)
|
||||
}
|
||||
|
||||
go ring.shards.Heartbeat(opt.HeartbeatFrequency)
|
||||
@ -386,6 +388,16 @@ func NewRing(opt *RingOptions) *Ring {
|
||||
return &ring
|
||||
}
|
||||
|
||||
func newRingShard(opt *RingOptions, name, addr string) *Client {
|
||||
clopt := opt.clientOptions(name)
|
||||
clopt.Addr = addr
|
||||
shard := NewClient(clopt)
|
||||
if opt.OnNewShard != nil {
|
||||
opt.OnNewShard(shard)
|
||||
}
|
||||
return shard
|
||||
}
|
||||
|
||||
func (c *Ring) init() {
|
||||
c.cmdable = c.Process
|
||||
}
|
||||
|
Reference in New Issue
Block a user