Skip to content

Commit

Permalink
Rework Options initialisation.
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Jun 5, 2016
1 parent 08d3790 commit 079b7ce
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 51 deletions.
21 changes: 10 additions & 11 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
55 changes: 15 additions & 40 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}

Expand Down
1 change: 1 addition & 0 deletions redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down
3 changes: 3 additions & 0 deletions ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type RingOptions struct {
IdleCheckFrequency time.Duration
}

func (opt *RingOptions) init() {}

func (opt *RingOptions) clientOptions() *Options {
return &Options{
DB: opt.DB,
Expand Down Expand Up @@ -127,6 +129,7 @@ type Ring struct {

func NewRing(opt *RingOptions) *Ring {
const nreplicas = 100
opt.init()
ring := &Ring{
opt: opt,
nreplicas: nreplicas,
Expand Down
5 changes: 5 additions & 0 deletions sentinel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -81,6 +84,7 @@ func NewFailoverClient(failoverOpt *FailoverOptions) *Client {
},
}
client.cmdable.process = client.Process

return &client
}

Expand All @@ -92,6 +96,7 @@ type sentinelClient struct {
}

func newSentinel(opt *Options) *sentinelClient {
opt.init()
client := sentinelClient{
baseClient: baseClient{
opt: opt,
Expand Down

0 comments on commit 079b7ce

Please sign in to comment.