From 7e8890b64422ad56e3c609a7b2afe54a6126f03a Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Thu, 25 May 2017 13:38:04 +0300 Subject: [PATCH] Embed Cmdable into StatefulCmdable --- cluster.go | 8 +++----- commands.go | 12 +++++++++++- pipeline.go | 2 -- redis.go | 10 ++++------ ring.go | 5 ++--- sentinel.go | 2 +- tx.go | 7 ++----- 7 files changed, 23 insertions(+), 23 deletions(-) diff --git a/cluster.go b/cluster.go index 99d8d7da..6a4bbe8f 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 a0984321..51dcf941 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 977f5eb3..de99f124 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 ca88df0d..89f985ee 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 270a81f9..9c57430d 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 799f530f..da3a4312 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 21c5c70f..5ef89619 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 }