From c6c07edf6f4d90695c2271ba0caf82096c30806c Mon Sep 17 00:00:00 2001 From: zcong1993 Date: Thu, 12 Oct 2023 00:29:46 +0800 Subject: [PATCH] feat: support set hooks option in new client API --- options.go | 3 +++ osscluster.go | 5 +++++ redis.go | 4 ++++ sentinel.go | 4 ++++ 4 files changed, 16 insertions(+) diff --git a/options.go b/options.go index ba65defd26..685ca1a9d4 100644 --- a/options.go +++ b/options.go @@ -144,6 +144,9 @@ type Options struct { // // Disable set-lib on connect. Default is false. DisableIndentity bool + + // Hooks are initial hooks in options like AddHook. + Hooks []Hook } func (opt *Options) init() { diff --git a/osscluster.go b/osscluster.go index b30e226372..7d54d230e0 100644 --- a/osscluster.go +++ b/osscluster.go @@ -85,6 +85,7 @@ type ClusterOptions struct { TLSConfig *tls.Config DisableIndentity bool // Disable set-lib on connect. Default is false. + Hooks []Hook } func (opt *ClusterOptions) init() { @@ -866,6 +867,10 @@ func NewClusterClient(opt *ClusterOptions) *ClusterClient { txPipeline: c.processTxPipeline, }) + for _, hook := range opt.Hooks { + c.AddHook(hook) + } + return c } diff --git a/redis.go b/redis.go index 9430eb75c2..31025bf6b4 100644 --- a/redis.go +++ b/redis.go @@ -796,6 +796,10 @@ func newConn(opt *Options, connPool pool.Pooler) *Conn { txPipeline: c.baseClient.processTxPipeline, }) + for _, hook := range opt.Hooks { + c.AddHook(hook) + } + return &c } diff --git a/sentinel.go b/sentinel.go index dbff406039..f170990d16 100644 --- a/sentinel.go +++ b/sentinel.go @@ -287,6 +287,10 @@ func NewSentinelClient(opt *Options) *SentinelClient { }) c.connPool = newConnPool(opt, c.dialHook) + for _, hook := range opt.Hooks { + c.AddHook(hook) + } + return c }