From 0c4c23679390b54a161d526a6c4657e651b3fb13 Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Fri, 23 Aug 2019 14:46:40 +0300 Subject: [PATCH] Add RingOptions.OnNewShard --- options.go | 2 +- ring.go | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/options.go b/options.go index d29526c..54c6fae 100644 --- a/options.go +++ b/options.go @@ -114,7 +114,7 @@ func (opt *Options) init() { if opt.TLSConfig == nil { return netDialer.DialContext(ctx, network, addr) } - return tls.DialWithDialer(netDialer, opt.Network, opt.Addr, opt.TLSConfig) + return tls.DialWithDialer(netDialer, network, addr, opt.TLSConfig) } } if opt.PoolSize == 0 { diff --git a/ring.go b/ring.go index 9a53d04..1c98191 100644 --- a/ring.go +++ b/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 }