Fix not applying updated ClusterClient context after calling WithContext method (#1480)

pass context to command info cache init call
This commit is contained in:
GreenHedgehog 2020-09-14 15:30:50 +03:00 committed by GitHub
parent f354306eec
commit 1a65d677b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 10 deletions

View File

@ -1554,7 +1554,7 @@ func (c *ClusterClient) retryBackoff(attempt int) time.Duration {
return internal.RetryBackoff(attempt, c.opt.MinRetryBackoff, c.opt.MaxRetryBackoff) return internal.RetryBackoff(attempt, c.opt.MinRetryBackoff, c.opt.MaxRetryBackoff)
} }
func (c *ClusterClient) cmdsInfo() (map[string]*CommandInfo, error) { func (c *ClusterClient) cmdsInfo(ctx context.Context) (map[string]*CommandInfo, error) {
// Try 3 random nodes. // Try 3 random nodes.
const nodeLimit = 3 const nodeLimit = 3
@ -1581,7 +1581,7 @@ func (c *ClusterClient) cmdsInfo() (map[string]*CommandInfo, error) {
continue continue
} }
info, err := node.Client.Command(c.ctx).Result() info, err := node.Client.Command(ctx).Result()
if err == nil { if err == nil {
return info, nil return info, nil
} }
@ -1597,7 +1597,7 @@ func (c *ClusterClient) cmdsInfo() (map[string]*CommandInfo, error) {
} }
func (c *ClusterClient) cmdInfo(name string) *CommandInfo { func (c *ClusterClient) cmdInfo(name string) *CommandInfo {
cmdsInfo, err := c.cmdsInfoCache.Get() cmdsInfo, err := c.cmdsInfoCache.Get(c.ctx)
if err != nil { if err != nil {
return nil return nil
} }

View File

@ -2135,21 +2135,21 @@ func commandInfoParser(rd *proto.Reader, n int64) (interface{}, error) {
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
type cmdsInfoCache struct { type cmdsInfoCache struct {
fn func() (map[string]*CommandInfo, error) fn func(ctx context.Context) (map[string]*CommandInfo, error)
once internal.Once once internal.Once
cmds map[string]*CommandInfo cmds map[string]*CommandInfo
} }
func newCmdsInfoCache(fn func() (map[string]*CommandInfo, error)) *cmdsInfoCache { func newCmdsInfoCache(fn func(ctx context.Context) (map[string]*CommandInfo, error)) *cmdsInfoCache {
return &cmdsInfoCache{ return &cmdsInfoCache{
fn: fn, fn: fn,
} }
} }
func (c *cmdsInfoCache) Get() (map[string]*CommandInfo, error) { func (c *cmdsInfoCache) Get(ctx context.Context) (map[string]*CommandInfo, error) {
err := c.once.Do(func() error { err := c.once.Do(func() error {
cmds, err := c.fn() cmds, err := c.fn(ctx)
if err != nil { if err != nil {
return err return err
} }

View File

@ -547,11 +547,11 @@ func (c *Ring) ForEachShard(
} }
} }
func (c *Ring) cmdsInfo() (map[string]*CommandInfo, error) { func (c *Ring) cmdsInfo(ctx context.Context) (map[string]*CommandInfo, error) {
shards := c.shards.List() shards := c.shards.List()
var firstErr error var firstErr error
for _, shard := range shards { for _, shard := range shards {
cmdsInfo, err := shard.Client.Command(context.TODO()).Result() cmdsInfo, err := shard.Client.Command(ctx).Result()
if err == nil { if err == nil {
return cmdsInfo, nil return cmdsInfo, nil
} }
@ -566,7 +566,7 @@ func (c *Ring) cmdsInfo() (map[string]*CommandInfo, error) {
} }
func (c *Ring) cmdInfo(name string) *CommandInfo { func (c *Ring) cmdInfo(name string) *CommandInfo {
cmdsInfo, err := c.cmdsInfoCache.Get() cmdsInfo, err := c.cmdsInfoCache.Get(c.ctx)
if err != nil { if err != nil {
return nil return nil
} }