1
0
mirror of https://github.com/redis/go-redis.git synced 2025-07-31 05:04:23 +03:00

Fix WithContext race

This commit is contained in:
Vladimir Mihailenco
2019-08-24 12:22:52 +03:00
parent 5776216677
commit 152e52f203
6 changed files with 38 additions and 45 deletions

14
ring.go
View File

@ -338,8 +338,6 @@ func (c *ringShards) Close() error {
//------------------------------------------------------------------------------
type ring struct {
cmdable
hooks
opt *RingOptions
shards *ringShards
cmdsInfoCache *cmdsInfoCache //nolint:structcheck
@ -361,6 +359,8 @@ type ring struct {
// Otherwise you should use Redis Cluster.
type Ring struct {
*ring
cmdable
hooks
ctx context.Context
}
@ -374,9 +374,8 @@ func NewRing(opt *RingOptions) *Ring {
},
ctx: context.Background(),
}
ring.init()
ring.cmdsInfoCache = newCmdsInfoCache(ring.cmdsInfo)
ring.cmdable = ring.Process
for name, addr := range opt.Addrs {
shard := newRingShard(opt, name, addr)
@ -398,10 +397,6 @@ func newRingShard(opt *RingOptions, name, addr string) *Client {
return shard
}
func (c *Ring) init() {
c.cmdable = c.Process
}
func (c *Ring) Context() context.Context {
return c.ctx
}
@ -411,8 +406,9 @@ func (c *Ring) WithContext(ctx context.Context) *Ring {
panic("nil context")
}
clone := *c
clone.cmdable = clone.Process
clone.hooks.Lock()
clone.ctx = ctx
clone.init()
return &clone
}