@@ -285,10 +285,14 @@ func (c *baseClient) _getConn(ctx context.Context) (*pool.Conn, error) {
285
285
return cn , nil
286
286
}
287
287
288
- func (c * baseClient ) newReAuthCredentialsListener (ctx context.Context , conn * Conn ) auth.CredentialsListener {
288
+ func (c * baseClient ) newReAuthCredentialsListener (ctx context.Context , cn * pool.Conn ) auth.CredentialsListener {
289
+ connPool := pool .NewSingleConnPool (c .connPool , cn )
290
+ // hooksMixin are intentionally empty here
291
+ conn := newConn (c .opt , connPool , nil )
292
+ ctx = c .context (ctx )
289
293
return auth .NewReAuthCredentialsListener (
290
- c .reAuthConnection (c . context ( ctx ) , conn ),
291
- c .onAuthenticationErr (c . context ( ctx ) , conn ),
294
+ c .reAuthConnection (ctx , conn ),
295
+ c .onAuthenticationErr (ctx , conn ),
292
296
)
293
297
}
294
298
@@ -313,11 +317,11 @@ func (c *baseClient) onAuthenticationErr(ctx context.Context, cn *Conn) func(err
313
317
poolCn , getErr := cn .connPool .Get (ctx )
314
318
if getErr == nil {
315
319
c .connPool .Remove (ctx , poolCn , err )
316
- } else {
317
- // if we can't get the pool connection, we can only close the connection
318
- if err := cn . Close (); err != nil {
319
- log . Printf ( "failed to close connection: %v" , err )
320
- }
320
+ }
321
+
322
+ // if we can't get the pool connection, we can only close the connection
323
+ if err := cn . Close (); err != nil {
324
+ log . Printf ( "failed to close connection: %v" , err )
321
325
}
322
326
}
323
327
}
@@ -353,8 +357,7 @@ func (c *baseClient) initConn(ctx context.Context, cn *pool.Conn) error {
353
357
var err error
354
358
cn .Inited = true
355
359
connPool := pool .NewSingleConnPool (c .connPool , cn )
356
-
357
- conn := newConn (c .opt , connPool , c .hooksMixin )
360
+ conn := newConn (c .opt , connPool , & c .hooksMixin )
358
361
359
362
protocol := c .opt .Protocol
360
363
// By default, use RESP3 in current version.
@@ -364,12 +367,13 @@ func (c *baseClient) initConn(ctx context.Context, cn *pool.Conn) error {
364
367
365
368
username , password := "" , ""
366
369
if c .opt .StreamingCredentialsProvider != nil {
367
- credentials , cancelCredentialsProvider , err := c .opt .StreamingCredentialsProvider .
368
- Subscribe (c .newReAuthCredentialsListener (ctx , conn ))
370
+ credentials , unsubscribeFromCredentialsProvider , err := c .opt .StreamingCredentialsProvider .
371
+ Subscribe (c .newReAuthCredentialsListener (ctx , cn ))
369
372
if err != nil {
370
373
return fmt .Errorf ("failed to subscribe to streaming credentials: %w" , err )
371
374
}
372
- c .onClose = c .wrappedOnClose (cancelCredentialsProvider )
375
+ c .onClose = c .wrappedOnClose (unsubscribeFromCredentialsProvider )
376
+ cn .SetOnClose (unsubscribeFromCredentialsProvider )
373
377
username , password = credentials .BasicAuth ()
374
378
} else if c .opt .CredentialsProviderContext != nil {
375
379
username , password , err = c .opt .CredentialsProviderContext (ctx )
@@ -770,7 +774,7 @@ func (c *Client) WithTimeout(timeout time.Duration) *Client {
770
774
}
771
775
772
776
func (c * Client ) Conn () * Conn {
773
- return newConn (c .opt , pool .NewStickyConnPool (c .connPool ), c .hooksMixin )
777
+ return newConn (c .opt , pool .NewStickyConnPool (c .connPool ), & c .hooksMixin )
774
778
}
775
779
776
780
// Do create a Cmd from the args and processes the cmd.
@@ -908,15 +912,18 @@ type Conn struct {
908
912
// newConn is a helper func to create a new Conn instance.
909
913
// the Conn instance is not thread-safe and should not be shared between goroutines.
910
914
// the parentHooks will be cloned, no need to clone before passing it.
911
- func newConn (opt * Options , connPool pool.Pooler , parentHooks hooksMixin ) * Conn {
915
+ func newConn (opt * Options , connPool pool.Pooler , parentHooks * hooksMixin ) * Conn {
912
916
c := Conn {
913
917
baseClient : baseClient {
914
- opt : opt ,
915
- connPool : connPool ,
916
- hooksMixin : parentHooks .clone (),
918
+ opt : opt ,
919
+ connPool : connPool ,
917
920
},
918
921
}
919
922
923
+ if parentHooks != nil {
924
+ c .hooksMixin = parentHooks .clone ()
925
+ }
926
+
920
927
c .cmdable = c .Process
921
928
c .statefulCmdable = c .Process
922
929
c .initHooks (hooks {
0 commit comments