Skip to content

Commit 6e58e79

Browse files
committed
fix(auth): streamline auth err proccess
1 parent a6a2c9d commit 6e58e79

File tree

1 file changed

+13
-21
lines changed

1 file changed

+13
-21
lines changed

redis.go

+13-21
Original file line numberDiff line numberDiff line change
@@ -285,21 +285,22 @@ func (c *baseClient) _getConn(ctx context.Context) (*pool.Conn, error) {
285285
return 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 {
293289
return 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 {
300296
return func(credentials auth.Credentials) error {
301297
var err error
302298
username, 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+
303304
if username != "" {
304305
err = cn.AuthACL(ctx, username, password).Err()
305306
} else {
@@ -308,22 +309,13 @@ func (c *baseClient) reAuthConnection(ctx context.Context, cn *Conn) func(creden
308309
return 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) {
312313
return 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.
315314
if err != nil {
316315
if 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 {
368360
username, password := "", ""
369361
if c.opt.StreamingCredentialsProvider != nil {
370362
credentials, unsubscribeFromCredentialsProvider, err := c.opt.StreamingCredentialsProvider.
371-
Subscribe(c.newReAuthCredentialsListener(ctx, cn))
363+
Subscribe(c.newReAuthCredentialsListener(cn))
372364
if err != nil {
373365
return fmt.Errorf("failed to subscribe to streaming credentials: %w", err)
374366
}

0 commit comments

Comments
 (0)