diff --git a/cluster.go b/cluster.go index 99d8d7d..6a4bbe8 100644 --- a/cluster.go +++ b/cluster.go @@ -349,7 +349,7 @@ func NewClusterClient(opt *ClusterOptions) *ClusterClient { opt: opt, nodes: newClusterNodes(opt), } - c.cmdable.process = c.Process + c.setProcessor(c.Process) // Add initial nodes. for _, addr := range opt.Addrs { @@ -678,8 +678,7 @@ func (c *ClusterClient) Pipeline() Pipeliner { pipe := Pipeline{ exec: c.pipelineExec, } - pipe.cmdable.process = pipe.Process - pipe.statefulCmdable.process = pipe.Process + pipe.setProcessor(pipe.Process) return &pipe } @@ -801,8 +800,7 @@ func (c *ClusterClient) TxPipeline() Pipeliner { pipe := Pipeline{ exec: c.txPipelineExec, } - pipe.cmdable.process = pipe.Process - pipe.statefulCmdable.process = pipe.Process + pipe.setProcessor(pipe.Process) return &pipe } diff --git a/commands.go b/commands.go index a098432..51dcf94 100644 --- a/commands.go +++ b/commands.go @@ -238,6 +238,7 @@ type Cmdable interface { } type StatefulCmdable interface { + Cmdable Auth(password string) *StatusCmd Select(index int) *StatusCmd ClientSetName(name string) *BoolCmd @@ -255,10 +256,20 @@ type cmdable struct { process func(cmd Cmder) error } +func (c *cmdable) setProcessor(fn func(Cmder) error) { + c.process = fn +} + type statefulCmdable struct { + cmdable process func(cmd Cmder) error } +func (c *statefulCmdable) setProcessor(fn func(Cmder) error) { + c.process = fn + c.cmdable.setProcessor(fn) +} + //------------------------------------------------------------------------------ func (c *statefulCmdable) Auth(password string) *StatusCmd { @@ -280,7 +291,6 @@ func (c *cmdable) Ping() *StatusCmd { } func (c *cmdable) Wait(numSlaves int, timeout time.Duration) *IntCmd { - cmd := NewIntCmd("wait", numSlaves, int(timeout/time.Millisecond)) c.process(cmd) return cmd diff --git a/pipeline.go b/pipeline.go index 977f5eb..de99f12 100644 --- a/pipeline.go +++ b/pipeline.go @@ -10,7 +10,6 @@ import ( type pipelineExecer func([]Cmder) error type Pipeliner interface { - Cmdable StatefulCmdable Process(cmd Cmder) error Close() error @@ -26,7 +25,6 @@ var _ Pipeliner = (*Pipeline)(nil) // http://redis.io/topics/pipelining. It's safe for concurrent use // by multiple goroutines. type Pipeline struct { - cmdable statefulCmdable exec pipelineExecer diff --git a/redis.go b/redis.go index ca88df0..89f985e 100644 --- a/redis.go +++ b/redis.go @@ -294,7 +294,7 @@ func newClient(opt *Options, pool pool.Pooler) *Client { connPool: pool, }, } - client.cmdable.process = client.Process + client.setProcessor(client.Process) return &client } @@ -307,7 +307,7 @@ func NewClient(opt *Options) *Client { func (c *Client) copy() *Client { c2 := new(Client) *c2 = *c - c2.cmdable.process = c2.Process + c2.setProcessor(c2.Process) return c2 } @@ -332,8 +332,7 @@ func (c *Client) Pipeline() Pipeliner { pipe := Pipeline{ exec: c.pipelineExecer(c.pipelineProcessCmds), } - pipe.cmdable.process = pipe.Process - pipe.statefulCmdable.process = pipe.Process + pipe.setProcessor(pipe.Process) return &pipe } @@ -346,8 +345,7 @@ func (c *Client) TxPipeline() Pipeliner { pipe := Pipeline{ exec: c.pipelineExecer(c.txPipelineProcessCmds), } - pipe.cmdable.process = pipe.Process - pipe.statefulCmdable.process = pipe.Process + pipe.setProcessor(pipe.Process) return &pipe } diff --git a/ring.go b/ring.go index 270a81f..9c57430 100644 --- a/ring.go +++ b/ring.go @@ -148,7 +148,7 @@ func NewRing(opt *RingOptions) *Ring { cmdsInfoOnce: new(sync.Once), } - ring.cmdable.process = ring.Process + ring.setProcessor(ring.Process) for name, addr := range opt.Addrs { clopt := opt.clientOptions() clopt.Addr = addr @@ -385,8 +385,7 @@ func (c *Ring) Pipeline() Pipeliner { pipe := Pipeline{ exec: c.pipelineExec, } - pipe.cmdable.process = pipe.Process - pipe.statefulCmdable.process = pipe.Process + pipe.setProcessor(pipe.Process) return &pipe } diff --git a/sentinel.go b/sentinel.go index 799f530..da3a431 100644 --- a/sentinel.go +++ b/sentinel.go @@ -82,7 +82,7 @@ func NewFailoverClient(failoverOpt *FailoverOptions) *Client { }, }, } - client.cmdable.process = client.Process + client.setProcessor(client.Process) return &client } diff --git a/tx.go b/tx.go index 21c5c70..5ef8961 100644 --- a/tx.go +++ b/tx.go @@ -13,7 +13,6 @@ const TxFailedErr = internal.RedisError("redis: transaction failed") // by multiple goroutines, because Exec resets list of watched keys. // If you don't need WATCH it is better to use Pipeline. type Tx struct { - cmdable statefulCmdable baseClient } @@ -25,8 +24,7 @@ func (c *Client) newTx() *Tx { connPool: pool.NewStickyConnPool(c.connPool.(*pool.ConnPool), true), }, } - tx.cmdable.process = tx.Process - tx.statefulCmdable.process = tx.Process + tx.setProcessor(tx.Process) return &tx } @@ -80,8 +78,7 @@ func (c *Tx) Pipeline() Pipeliner { pipe := Pipeline{ exec: c.pipelineExecer(c.txPipelineProcessCmds), } - pipe.cmdable.process = pipe.Process - pipe.statefulCmdable.process = pipe.Process + pipe.setProcessor(pipe.Process) return &pipe }