@@ -45,13 +45,15 @@ func (hs *hooks) AddHook(hook Hook) {
4545 hs .hooks = append (hs .hooks , hook )
4646}
4747
48- func (hs hooks ) process (ctx context.Context , cmd Cmder , fn func (Cmder ) error ) error {
48+ func (hs hooks ) process (
49+ ctx context.Context , cmd Cmder , fn func (context.Context , Cmder ) error ,
50+ ) error {
4951 ctx , err := hs .beforeProcess (ctx , cmd )
5052 if err != nil {
5153 return err
5254 }
5355
54- cmdErr := fn (cmd )
56+ cmdErr := fn (ctx , cmd )
5557
5658 _ , err = hs .afterProcess (ctx , cmd )
5759 if err != nil {
@@ -83,13 +85,15 @@ func (hs hooks) afterProcess(ctx context.Context, cmd Cmder) (context.Context, e
8385 return ctx , nil
8486}
8587
86- func (hs hooks ) processPipeline (ctx context.Context , cmds []Cmder , fn func ([]Cmder ) error ) error {
88+ func (hs hooks ) processPipeline (
89+ ctx context.Context , cmds []Cmder , fn func (context.Context , []Cmder ) error ,
90+ ) error {
8791 ctx , err := hs .beforeProcessPipeline (ctx , cmds )
8892 if err != nil {
8993 return err
9094 }
9195
92- cmdsErr := fn (cmds )
96+ cmdsErr := fn (ctx , cmds )
9397
9498 _ , err = hs .afterProcessPipeline (ctx , cmds )
9599 if err != nil {
@@ -246,14 +250,7 @@ func (c *baseClient) initConn(cn *pool.Conn) error {
246250 return nil
247251}
248252
249- // Do creates a Cmd from the args and processes the cmd.
250- func (c * baseClient ) Do (args ... interface {}) * Cmd {
251- cmd := NewCmd (args ... )
252- _ = c .process (cmd )
253- return cmd
254- }
255-
256- func (c * baseClient ) process (cmd Cmder ) error {
253+ func (c * baseClient ) process (ctx context.Context , cmd Cmder ) error {
257254 for attempt := 0 ; attempt <= c .opt .MaxRetries ; attempt ++ {
258255 if attempt > 0 {
259256 time .Sleep (c .retryBackoff (attempt ))
@@ -328,11 +325,11 @@ func (c *baseClient) getAddr() string {
328325 return c .opt .Addr
329326}
330327
331- func (c * baseClient ) processPipeline (cmds []Cmder ) error {
328+ func (c * baseClient ) processPipeline (ctx context. Context , cmds []Cmder ) error {
332329 return c .generalProcessPipeline (cmds , c .pipelineProcessCmds )
333330}
334331
335- func (c * baseClient ) processTxPipeline (cmds []Cmder ) error {
332+ func (c * baseClient ) processTxPipeline (ctx context. Context , cmds []Cmder ) error {
336333 return c .generalProcessPipeline (cmds , c .txPipelineProcessCmds )
337334}
338335
@@ -503,16 +500,31 @@ func (c *Client) WithContext(ctx context.Context) *Client {
503500 return & clone
504501}
505502
503+ // Do creates a Cmd from the args and processes the cmd.
504+ func (c * Client ) Do (args ... interface {}) * Cmd {
505+ return c .DoContext (c .ctx , args ... )
506+ }
507+
508+ func (c * Client ) DoContext (ctx context.Context , args ... interface {}) * Cmd {
509+ cmd := NewCmd (args ... )
510+ _ = c .ProcessContext (ctx , cmd )
511+ return cmd
512+ }
513+
506514func (c * Client ) Process (cmd Cmder ) error {
507- return c .hooks . process (c .ctx , cmd , c . baseClient . process )
515+ return c .ProcessContext (c .ctx , cmd )
508516}
509517
510- func (c * Client ) processPipeline ( cmds [] Cmder ) error {
511- return c .hooks .processPipeline ( c . ctx , cmds , c .baseClient .processPipeline )
518+ func (c * Client ) ProcessContext ( ctx context. Context , cmd Cmder ) error {
519+ return c .hooks .process ( ctx , cmd , c .baseClient .process )
512520}
513521
514- func (c * Client ) processTxPipeline (cmds []Cmder ) error {
515- return c .hooks .processPipeline (c .ctx , cmds , c .baseClient .processTxPipeline )
522+ func (c * Client ) processPipeline (ctx context.Context , cmds []Cmder ) error {
523+ return c .hooks .processPipeline (ctx , cmds , c .baseClient .processPipeline )
524+ }
525+
526+ func (c * Client ) processTxPipeline (ctx context.Context , cmds []Cmder ) error {
527+ return c .hooks .processPipeline (ctx , cmds , c .baseClient .processTxPipeline )
516528}
517529
518530// Options returns read-only Options that were used to create the client.
@@ -637,7 +649,11 @@ func newConn(opt *Options, cn *pool.Conn) *Conn {
637649}
638650
639651func (c * Conn ) Process (cmd Cmder ) error {
640- return c .baseClient .process (cmd )
652+ return c .ProcessContext (context .TODO (), cmd )
653+ }
654+
655+ func (c * Conn ) ProcessContext (ctx context.Context , cmd Cmder ) error {
656+ return c .baseClient .process (ctx , cmd )
641657}
642658
643659func (c * Conn ) Pipelined (fn func (Pipeliner ) error ) ([]Cmder , error ) {
0 commit comments