@@ -285,21 +285,22 @@ func (c *baseClient) _getConn(ctx context.Context) (*pool.Conn, error) {
285285return cn , nil
286286}
287287
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 )
288+ func (c * baseClient ) newReAuthCredentialsListener (poolCn * pool.Conn ) auth.CredentialsListener {
293289return auth .NewReAuthCredentialsListener (
294- c .reAuthConnection (ctx , conn ),
295- c .onAuthenticationErr (ctx , conn ),
290+ c .reAuthConnection (poolCn ),
291+ c .onAuthenticationErr (poolCn ),
296292)
297293}
298294
299- func (c * baseClient ) reAuthConnection (ctx context. Context , cn * Conn ) func (credentials auth.Credentials ) error {
295+ func (c * baseClient ) reAuthConnection (poolCn * pool. Conn ) func (credentials auth.Credentials ) error {
300296return func (credentials auth.Credentials ) error {
301297var err error
302298username , password := credentials .BasicAuth ()
299+ ctx := context .Background ()
300+ connPool := pool .NewSingleConnPool (c .connPool , poolCn )
301+ // hooksMixin are intentionally empty here
302+ cn := newConn (c .opt , connPool , nil )
303+
303304if username != "" {
304305err = cn .AuthACL (ctx , username , password ).Err ()
305306} else {
@@ -308,22 +309,13 @@ func (c *baseClient) reAuthConnection(ctx context.Context, cn *Conn) func(creden
308309return err
309310}
310311}
311- func (c * baseClient ) onAuthenticationErr (ctx context. Context , cn * Conn ) func (err error ) {
312+ func (c * baseClient ) onAuthenticationErr (poolCn * pool. Conn ) func (err error ) {
312313return func (err error ) {
313- // since the connection pool of the *Conn will actually return us the underlying pool.Conn,
314- // we can get it from the *Conn and remove it from the clients pool.
315314if err != nil {
316315if isBadConn (err , false , c .opt .Addr ) {
317- poolCn , getErr := cn .connPool .Get (ctx )
318- if getErr == nil {
319- c .connPool .Remove (ctx , poolCn , err )
320- } else {
321- // if we can't get the pool connection, we can only close the connection
322- if err := cn .Close (); err != nil {
323- log .Printf ("failed to close connection: %v" , err )
324- }
325- }
316+ c .connPool .CloseConn (poolCn )
326317}
318+ internal .Logger .Printf (context .Background (), "redis: re-authentication failed: %v" , err )
327319}
328320}
329321}
@@ -368,7 +360,7 @@ func (c *baseClient) initConn(ctx context.Context, cn *pool.Conn) error {
368360username , password := "" , ""
369361if c .opt .StreamingCredentialsProvider != nil {
370362credentials , unsubscribeFromCredentialsProvider , err := c .opt .StreamingCredentialsProvider .
371- Subscribe (c .newReAuthCredentialsListener (ctx , cn ))
363+ Subscribe (c .newReAuthCredentialsListener (cn ))
372364if err != nil {
373365return fmt .Errorf ("failed to subscribe to streaming credentials: %w" , err )
374366}
0 commit comments