@@ -316,17 +316,15 @@ func (c *baseClient) _process(ctx context.Context, cmd Cmder, attempt int) (bool
316316 }
317317
318318 retryTimeout := uint32 (0 )
319- err := c .withConn (ctx , func (ctx context.Context , cn * pool.Conn ) error {
320- err := cn .WithWriter (ctx , c .opt .WriteTimeout , func (wr * proto.Writer ) error {
319+ if err := c .withConn (ctx , func (ctx context.Context , cn * pool.Conn ) error {
320+ if err := cn .WithWriter (c . context ( ctx ) , c .opt .WriteTimeout , func (wr * proto.Writer ) error {
321321 return writeCmd (wr , cmd )
322- })
323- if err != nil {
322+ }); err != nil {
324323 atomic .StoreUint32 (& retryTimeout , 1 )
325324 return err
326325 }
327326
328- err = cn .WithReader (ctx , c .cmdTimeout (cmd ), cmd .readReply )
329- if err != nil {
327+ if err := cn .WithReader (c .context (ctx ), c .cmdTimeout (cmd ), cmd .readReply ); err != nil {
330328 if cmd .readTimeout () == nil {
331329 atomic .StoreUint32 (& retryTimeout , 1 )
332330 } else {
@@ -336,13 +334,12 @@ func (c *baseClient) _process(ctx context.Context, cmd Cmder, attempt int) (bool
336334 }
337335
338336 return nil
339- })
340- if err == nil {
341- return false , nil
337+ }); err != nil {
338+ retry := shouldRetry ( err , atomic . LoadUint32 ( & retryTimeout ) == 1 )
339+ return retry , err
342340 }
343341
344- retry := shouldRetry (err , atomic .LoadUint32 (& retryTimeout ) == 1 )
345- return retry , err
342+ return false , nil
346343}
347344
348345func (c * baseClient ) retryBackoff (attempt int ) time.Duration {
@@ -430,14 +427,14 @@ func (c *baseClient) _generalProcessPipeline(
430427func (c * baseClient ) pipelineProcessCmds (
431428 ctx context.Context , cn * pool.Conn , cmds []Cmder ,
432429) (bool , error ) {
433- if err := cn .WithWriter (ctx , c .opt .WriteTimeout , func (wr * proto.Writer ) error {
430+ if err := cn .WithWriter (c . context ( ctx ) , c .opt .WriteTimeout , func (wr * proto.Writer ) error {
434431 return writeCmds (wr , cmds )
435432 }); err != nil {
436433 setCmdsErr (cmds , err )
437434 return true , err
438435 }
439436
440- if err := cn .WithReader (ctx , c .opt .ReadTimeout , func (rd * proto.Reader ) error {
437+ if err := cn .WithReader (c . context ( ctx ) , c .opt .ReadTimeout , func (rd * proto.Reader ) error {
441438 return pipelineReadCmds (rd , cmds )
442439 }); err != nil {
443440 return true , err
@@ -462,14 +459,14 @@ func pipelineReadCmds(rd *proto.Reader, cmds []Cmder) error {
462459func (c * baseClient ) txPipelineProcessCmds (
463460 ctx context.Context , cn * pool.Conn , cmds []Cmder ,
464461) (bool , error ) {
465- if err := cn .WithWriter (ctx , c .opt .WriteTimeout , func (wr * proto.Writer ) error {
462+ if err := cn .WithWriter (c . context ( ctx ) , c .opt .WriteTimeout , func (wr * proto.Writer ) error {
466463 return writeCmds (wr , cmds )
467464 }); err != nil {
468465 setCmdsErr (cmds , err )
469466 return true , err
470467 }
471468
472- if err := cn .WithReader (ctx , c .opt .ReadTimeout , func (rd * proto.Reader ) error {
469+ if err := cn .WithReader (c . context ( ctx ) , c .opt .ReadTimeout , func (rd * proto.Reader ) error {
473470 statusCmd := cmds [0 ].(* StatusCmd )
474471 // Trim multi and exec.
475472 trimmedCmds := cmds [1 : len (cmds )- 1 ]
@@ -527,6 +524,13 @@ func txPipelineReadQueued(rd *proto.Reader, statusCmd *StatusCmd, cmds []Cmder)
527524 return nil
528525}
529526
527+ func (c * baseClient ) context (ctx context.Context ) context.Context {
528+ if c .opt .ContextTimeoutEnabled {
529+ return ctx
530+ }
531+ return context .Background ()
532+ }
533+
530534//------------------------------------------------------------------------------
531535
532536// Client is a Redis client representing a pool of zero or more underlying connections.
0 commit comments