diff --git a/cluster.go b/cluster.go index e29d6ef7..983480b5 100644 --- a/cluster.go +++ b/cluster.go @@ -41,10 +41,7 @@ type ClusterClient struct { // NewClusterClient returns a Redis Cluster client as described in // http://redis.io/topics/cluster-spec. func NewClusterClient(opt *ClusterOptions) *ClusterClient { - if opt.RouteByLatency { - opt.ReadOnly = true - } - + opt.init() client := &ClusterClient{ opt: opt, nodes: make(map[string]*clusterNode), @@ -246,7 +243,7 @@ func (c *ClusterClient) Process(cmd Cmder) { var ask bool slot, node := c.cmdSlotAndNode(cmd) - for attempt := 0; attempt <= c.opt.getMaxRedirects(); attempt++ { + for attempt := 0; attempt <= c.opt.MaxRedirects; attempt++ { if attempt > 0 { cmd.reset() } @@ -419,7 +416,7 @@ func (c *ClusterClient) pipelineExec(cmds []Cmder) error { cmdsMap[node] = append(cmdsMap[node], cmd) } - for attempt := 0; attempt <= c.opt.getMaxRedirects(); attempt++ { + for attempt := 0; attempt <= c.opt.MaxRedirects; attempt++ { failedCmds := make(map[*clusterNode][]Cmder) for node, cmds := range cmdsMap { @@ -516,14 +513,16 @@ type ClusterOptions struct { IdleCheckFrequency time.Duration } -func (opt *ClusterOptions) getMaxRedirects() int { +func (opt *ClusterOptions) init() { if opt.MaxRedirects == -1 { - return 0 + opt.MaxRedirects = 0 + } else if opt.MaxRedirects == 0 { + opt.MaxRedirects = 16 } - if opt.MaxRedirects == 0 { - return 16 + + if opt.RouteByLatency { + opt.ReadOnly = true } - return opt.MaxRedirects } func (opt *ClusterOptions) clientOptions() *Options { diff --git a/options.go b/options.go index e3db4ad1..4cd62f93 100644 --- a/options.go +++ b/options.go @@ -58,61 +58,36 @@ type Options struct { ReadOnly bool } -func (opt *Options) getNetwork() string { +func (opt *Options) init() { if opt.Network == "" { - return "tcp" + opt.Network = "tcp" } - return opt.Network -} - -func (opt *Options) getDialer() func() (net.Conn, error) { - if opt.Dialer != nil { - return opt.Dialer + if opt.Dialer == nil { + opt.Dialer = func() (net.Conn, error) { + return net.DialTimeout(opt.Network, opt.Addr, opt.DialTimeout) + } } - return func() (net.Conn, error) { - return net.DialTimeout(opt.getNetwork(), opt.Addr, opt.getDialTimeout()) - } -} - -func (opt *Options) getPoolSize() int { if opt.PoolSize == 0 { - return 10 + opt.PoolSize = 10 } - return opt.PoolSize -} - -func (opt *Options) getDialTimeout() time.Duration { if opt.DialTimeout == 0 { - return 5 * time.Second + opt.DialTimeout = 5 * time.Second } - return opt.DialTimeout -} - -func (opt *Options) getPoolTimeout() time.Duration { if opt.PoolTimeout == 0 { - return 1 * time.Second + opt.PoolTimeout = 1 * time.Second } - return opt.PoolTimeout -} - -func (opt *Options) getIdleTimeout() time.Duration { - return opt.IdleTimeout -} - -func (opt *Options) getIdleCheckFrequency() time.Duration { if opt.IdleCheckFrequency == 0 { - return time.Minute + opt.IdleCheckFrequency = time.Minute } - return opt.IdleCheckFrequency } func newConnPool(opt *Options) *pool.ConnPool { return pool.NewConnPool( - opt.getDialer(), - opt.getPoolSize(), - opt.getPoolTimeout(), - opt.getIdleTimeout(), - opt.getIdleCheckFrequency(), + opt.Dialer, + opt.PoolSize, + opt.PoolTimeout, + opt.IdleTimeout, + opt.IdleCheckFrequency, ) } diff --git a/redis.go b/redis.go index d44e86e7..121c5b24 100644 --- a/redis.go +++ b/redis.go @@ -155,6 +155,7 @@ func newClient(opt *Options, pool pool.Pooler) *Client { // NewClient returns a client to the Redis Server specified by Options. func NewClient(opt *Options) *Client { + opt.init() return newClient(opt, newConnPool(opt)) } diff --git a/ring.go b/ring.go index 17f53056..bc749c1d 100644 --- a/ring.go +++ b/ring.go @@ -39,6 +39,8 @@ type RingOptions struct { IdleCheckFrequency time.Duration } +func (opt *RingOptions) init() {} + func (opt *RingOptions) clientOptions() *Options { return &Options{ DB: opt.DB, @@ -127,6 +129,7 @@ type Ring struct { func NewRing(opt *RingOptions) *Ring { const nreplicas = 100 + opt.init() ring := &Ring{ opt: opt, nreplicas: nreplicas, diff --git a/sentinel.go b/sentinel.go index 6cd39863..66f0974c 100644 --- a/sentinel.go +++ b/sentinel.go @@ -64,12 +64,15 @@ func (opt *FailoverOptions) options() *Options { // goroutines. func NewFailoverClient(failoverOpt *FailoverOptions) *Client { opt := failoverOpt.options() + opt.init() + failover := &sentinelFailover{ masterName: failoverOpt.MasterName, sentinelAddrs: failoverOpt.SentinelAddrs, opt: opt, } + client := Client{ baseClient: baseClient{ opt: opt, @@ -81,6 +84,7 @@ func NewFailoverClient(failoverOpt *FailoverOptions) *Client { }, } client.cmdable.process = client.Process + return &client } @@ -92,6 +96,7 @@ type sentinelClient struct { } func newSentinel(opt *Options) *sentinelClient { + opt.init() client := sentinelClient{ baseClient: baseClient{ opt: opt,