@@ -22,6 +22,12 @@ func SetLogger(logger *log.Logger) {
2222 internal .Logger = logger
2323}
2424
25+ func (c * baseClient ) init () {
26+ c .process = c .defaultProcess
27+ c .processPipeline = c .defaultProcessPipeline
28+ c .processTxPipeline = c .defaultProcessTxPipeline
29+ }
30+
2531func (c * baseClient ) String () string {
2632 return fmt .Sprintf ("Redis<%s db:%d>" , c .getAddr (), c .opt .DB )
2733}
@@ -85,7 +91,8 @@ func (c *baseClient) initConn(cn *pool.Conn) error {
8591 connPool : pool .NewSingleConnPool (cn ),
8692 },
8793 }
88- conn .setProcessor (conn .Process )
94+ conn .baseClient .init ()
95+ conn .statefulCmdable .setProcessor (conn .Process )
8996
9097 _ , err := conn .Pipelined (func (pipe Pipeliner ) error {
9198 if c .opt .Password != "" {
@@ -117,14 +124,11 @@ func (c *baseClient) initConn(cn *pool.Conn) error {
117124// an input and returns the new wrapper process func. createWrapper should
118125// use call the old process func within the new process func.
119126func (c * baseClient ) WrapProcess (fn func (oldProcess func (cmd Cmder ) error ) func (cmd Cmder ) error ) {
120- c .process = fn (c .defaultProcess )
127+ c .process = fn (c .process )
121128}
122129
123130func (c * baseClient ) Process (cmd Cmder ) error {
124- if c .process != nil {
125- return c .process (cmd )
126- }
127- return c .defaultProcess (cmd )
131+ return c .process (cmd )
128132}
129133
130134func (c * baseClient ) defaultProcess (cmd Cmder ) error {
@@ -198,35 +202,48 @@ func (c *baseClient) getAddr() string {
198202 return c .opt .Addr
199203}
200204
205+ func (c * baseClient ) WrapProcessPipeline (
206+ fn func (oldProcess func ([]Cmder ) error ) func ([]Cmder ) error ,
207+ ) {
208+ c .processPipeline = fn (c .processPipeline )
209+ c .processTxPipeline = fn (c .processTxPipeline )
210+ }
211+
212+ func (c * baseClient ) defaultProcessPipeline (cmds []Cmder ) error {
213+ return c .generalProcessPipeline (cmds , c .pipelineProcessCmds )
214+ }
215+
216+ func (c * baseClient ) defaultProcessTxPipeline (cmds []Cmder ) error {
217+ return c .generalProcessPipeline (cmds , c .txPipelineProcessCmds )
218+ }
219+
201220type pipelineProcessor func (* pool.Conn , []Cmder ) (bool , error )
202221
203- func (c * baseClient ) pipelineExecer (p pipelineProcessor ) pipelineExecer {
204- return func (cmds []Cmder ) error {
205- for attempt := 0 ; attempt <= c .opt .MaxRetries ; attempt ++ {
206- if attempt > 0 {
207- time .Sleep (c .retryBackoff (attempt ))
208- }
222+ func (c * baseClient ) generalProcessPipeline (cmds []Cmder , p pipelineProcessor ) error {
223+ for attempt := 0 ; attempt <= c .opt .MaxRetries ; attempt ++ {
224+ if attempt > 0 {
225+ time .Sleep (c .retryBackoff (attempt ))
226+ }
209227
210- cn , _ , err := c .getConn ()
211- if err != nil {
212- setCmdsErr (cmds , err )
213- return err
214- }
228+ cn , _ , err := c .getConn ()
229+ if err != nil {
230+ setCmdsErr (cmds , err )
231+ return err
232+ }
215233
216- canRetry , err := p (cn , cmds )
234+ canRetry , err := p (cn , cmds )
217235
218- if err == nil || internal .IsRedisError (err ) {
219- _ = c .connPool .Put (cn )
220- break
221- }
222- _ = c .connPool .Remove (cn )
236+ if err == nil || internal .IsRedisError (err ) {
237+ _ = c .connPool .Put (cn )
238+ break
239+ }
240+ _ = c .connPool .Remove (cn )
223241
224- if ! canRetry || ! internal .IsRetryableError (err , true ) {
225- break
226- }
242+ if ! canRetry || ! internal .IsRetryableError (err , true ) {
243+ break
227244 }
228- return firstCmdsErr (cmds )
229245 }
246+ return firstCmdsErr (cmds )
230247}
231248
232249func (c * baseClient ) pipelineProcessCmds (cn * pool.Conn , cmds []Cmder ) (bool , error ) {
@@ -324,14 +341,15 @@ type Client struct {
324341}
325342
326343func newClient (opt * Options , pool pool.Pooler ) * Client {
327- client := Client {
344+ c := Client {
328345 baseClient : baseClient {
329346 opt : opt ,
330347 connPool : pool ,
331348 },
332349 }
333- client .setProcessor (client .Process )
334- return & client
350+ c .baseClient .init ()
351+ c .cmdable .setProcessor (c .Process )
352+ return & c
335353}
336354
337355// NewClient returns a client to the Redis Server specified by Options.
@@ -343,7 +361,7 @@ func NewClient(opt *Options) *Client {
343361func (c * Client ) copy () * Client {
344362 c2 := new (Client )
345363 * c2 = * c
346- c2 .setProcessor (c2 .Process )
364+ c2 .cmdable . setProcessor (c2 .Process )
347365 return c2
348366}
349367
@@ -366,9 +384,9 @@ func (c *Client) Pipelined(fn func(Pipeliner) error) ([]Cmder, error) {
366384
367385func (c * Client ) Pipeline () Pipeliner {
368386 pipe := Pipeline {
369- exec : c .pipelineExecer ( c . pipelineProcessCmds ) ,
387+ exec : c .processPipeline ,
370388 }
371- pipe .setProcessor (pipe .Process )
389+ pipe .statefulCmdable . setProcessor (pipe .Process )
372390 return & pipe
373391}
374392
@@ -379,9 +397,9 @@ func (c *Client) TxPipelined(fn func(Pipeliner) error) ([]Cmder, error) {
379397// TxPipeline acts like Pipeline, but wraps queued commands with MULTI/EXEC.
380398func (c * Client ) TxPipeline () Pipeliner {
381399 pipe := Pipeline {
382- exec : c .pipelineExecer ( c . txPipelineProcessCmds ) ,
400+ exec : c .processTxPipeline ,
383401 }
384- pipe .setProcessor (pipe .Process )
402+ pipe .statefulCmdable . setProcessor (pipe .Process )
385403 return & pipe
386404}
387405
@@ -430,9 +448,9 @@ func (c *Conn) Pipelined(fn func(Pipeliner) error) ([]Cmder, error) {
430448
431449func (c * Conn ) Pipeline () Pipeliner {
432450 pipe := Pipeline {
433- exec : c .pipelineExecer ( c . pipelineProcessCmds ) ,
451+ exec : c .processPipeline ,
434452 }
435- pipe .setProcessor (pipe .Process )
453+ pipe .statefulCmdable . setProcessor (pipe .Process )
436454 return & pipe
437455}
438456
@@ -443,8 +461,8 @@ func (c *Conn) TxPipelined(fn func(Pipeliner) error) ([]Cmder, error) {
443461// TxPipeline acts like Pipeline, but wraps queued commands with MULTI/EXEC.
444462func (c * Conn ) TxPipeline () Pipeliner {
445463 pipe := Pipeline {
446- exec : c .pipelineExecer ( c . txPipelineProcessCmds ) ,
464+ exec : c .processTxPipeline ,
447465 }
448- pipe .setProcessor (pipe .Process )
466+ pipe .statefulCmdable . setProcessor (pipe .Process )
449467 return & pipe
450468}
0 commit comments