@@ -29,9 +29,9 @@ func SetLogger(logger internal.Logging) {
2929//------------------------------------------------------------------------------
3030
3131type Hook interface {
32- DialHook (hook DialHook ) DialHook
33- ProcessHook (hook ProcessHook ) ProcessHook
34- ProcessPipelineHook (hook ProcessPipelineHook ) ProcessPipelineHook
32+ DialHook (next DialHook ) DialHook
33+ ProcessHook (next ProcessHook ) ProcessHook
34+ ProcessPipelineHook (next ProcessPipelineHook ) ProcessPipelineHook
3535}
3636
3737type (
@@ -48,6 +48,43 @@ type hooks struct {
4848 processTxPipelineHook ProcessPipelineHook
4949}
5050
51+ // AddHook is to add a hook to the queue.
52+ // Hook is a function executed during network connection, command execution, and pipeline,
53+ // it is a first-in-last-out stack queue (FILO).
54+ // The first to be added to the queue is the execution function of the redis command (the last to be executed).
55+ // You need to execute the next hook in each hook, unless you want to terminate the execution of the command.
56+ // For example, you added hook-1, hook-2:
57+ //
58+ // client.AddHook(hook-1, hook-2)
59+ //
60+ // hook-1:
61+ //
62+ // func (Hook1) ProcessHook(next redis.ProcessHook) redis.ProcessHook {
63+ // return func(ctx context.Context, cmd Cmder) error {
64+ // print("hook-1 start")
65+ // next(ctx, cmd)
66+ // print("hook-1 end")
67+ // return nil
68+ // }
69+ // }
70+ //
71+ // hook-2:
72+ //
73+ // func (Hook2) ProcessHook(next redis.ProcessHook) redis.ProcessHook {
74+ // return func(ctx context.Context, cmd redis.Cmder) error {
75+ // print("hook-2 start")
76+ // next(ctx, cmd)
77+ // print("hook-2 end")
78+ // return nil
79+ // }
80+ // }
81+ //
82+ // The execution sequence is:
83+ //
84+ // hook-2 start -> hook-1 start -> exec redis cmd -> hook-1 end -> hook-2 end
85+ //
86+ // Please note: "next(ctx, cmd)" is very important, it will call the next hook,
87+ // if "next(ctx, cmd)" is not executed in hook-1, the redis command will not be executed.
5188func (hs * hooks ) AddHook (hook Hook ) {
5289 hs .slice = append (hs .slice , hook )
5390 hs .dialHook = hook .DialHook (hs .dialHook )
@@ -575,7 +612,7 @@ func (c *Client) Conn() *Conn {
575612 return newConn (c .opt , pool .NewStickyConnPool (c .connPool ))
576613}
577614
578- // Do creates a Cmd from the args and processes the cmd.
615+ // Do create a Cmd from the args and processes the cmd.
579616func (c * Client ) Do (ctx context.Context , args ... interface {}) * Cmd {
580617 cmd := NewCmd (ctx , args ... )
581618 _ = c .Process (ctx , cmd )
@@ -648,26 +685,26 @@ func (c *Client) pubSub() *PubSub {
648685// subscription may not be active immediately. To force the connection to wait,
649686// you may call the Receive() method on the returned *PubSub like so:
650687//
651- // sub := client.Subscribe(queryResp)
652- // iface, err := sub.Receive()
653- // if err != nil {
654- // // handle error
655- // }
688+ // sub := client.Subscribe(queryResp)
689+ // iface, err := sub.Receive()
690+ // if err != nil {
691+ // // handle error
692+ // }
656693//
657- // // Should be *Subscription, but others are possible if other actions have been
658- // // taken on sub since it was created.
659- // switch iface.(type) {
660- // case *Subscription:
661- // // subscribe succeeded
662- // case *Message:
663- // // received first message
664- // case *Pong:
665- // // pong received
666- // default:
667- // // handle error
668- // }
694+ // // Should be *Subscription, but others are possible if other actions have been
695+ // // taken on sub since it was created.
696+ // switch iface.(type) {
697+ // case *Subscription:
698+ // // subscribe succeeded
699+ // case *Message:
700+ // // received first message
701+ // case *Pong:
702+ // // pong received
703+ // default:
704+ // // handle error
705+ // }
669706//
670- // ch := sub.Channel()
707+ // ch := sub.Channel()
671708func (c * Client ) Subscribe (ctx context.Context , channels ... string ) * PubSub {
672709 pubsub := c .pubSub ()
673710 if len (channels ) > 0 {
0 commit comments