Skip to content

Commit

Permalink
Add RingOptions.OnNewShard
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Aug 23, 2019
1 parent 4723229 commit 0c4c236
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 15 additions & 3 deletions ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -376,16 +379,25 @@ 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)

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
}
Expand Down

0 comments on commit 0c4c236

Please sign in to comment.