Skip to content

Commit 4b37fed

Browse files
committed
fix(auth): streamline auth err proccess
1 parent a6a2c9d commit 4b37fed

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

redis.go

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -285,21 +285,23 @@ 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)
288+
func (c *baseClient) newReAuthCredentialsListener(ctx context.Context, poolCn *pool.Conn) auth.CredentialsListener {
292289
ctx = c.context(ctx)
293290
return auth.NewReAuthCredentialsListener(
294-
c.reAuthConnection(ctx, conn),
295-
c.onAuthenticationErr(ctx, conn),
291+
c.reAuthConnection(ctx, poolCn),
292+
c.onAuthenticationErr(ctx, poolCn),
296293
)
297294
}
298295

299-
func (c *baseClient) reAuthConnection(ctx context.Context, cn *Conn) func(credentials auth.Credentials) error {
296+
func (c *baseClient) reAuthConnection(ctx context.Context, poolCn *pool.Conn) func(credentials auth.Credentials) error {
300297
return func(credentials auth.Credentials) error {
301298
var err error
302299
username, password := credentials.BasicAuth()
300+
301+
connPool := pool.NewSingleConnPool(c.connPool, poolCn)
302+
// hooksMixin are intentionally empty here
303+
cn := newConn(c.opt, connPool, nil)
304+
303305
if username != "" {
304306
err = cn.AuthACL(ctx, username, password).Err()
305307
} else {
@@ -308,22 +310,13 @@ func (c *baseClient) reAuthConnection(ctx context.Context, cn *Conn) func(creden
308310
return err
309311
}
310312
}
311-
func (c *baseClient) onAuthenticationErr(ctx context.Context, cn *Conn) func(err error) {
313+
func (c *baseClient) onAuthenticationErr(ctx context.Context, poolCn *pool.Conn) func(err error) {
312314
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.
315315
if err != nil {
316316
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-
}
317+
c.connPool.CloseConn(poolCn)
326318
}
319+
internal.Logger.Printf(ctx, "redis: re-authentication failed: %v", err)
327320
}
328321
}
329322
}

0 commit comments

Comments
 (0)