| 456 | } |
| 457 | } |
| 458 | func (c *baseClient) onAuthenticationErr() func(poolCn *pool.Conn, err error) { |
| 459 | return func(poolCn *pool.Conn, err error) { |
| 460 | if err != nil { |
| 461 | if isBadConn(err, false, c.opt.Addr) { |
| 462 | // Close the connection to force a reconnection. |
| 463 | // Re-auth happens on connections that were idle in the pool (the pool hook |
| 464 | // waits for IDLE state before transitioning to UNUSABLE for re-auth). |
| 465 | // From metrics perspective, the connection was never "used" by a client. |
| 466 | // Note: Using context.Background() as this callback doesn't have access to caller's context. |
| 467 | err := c.connPool.CloseConn(context.Background(), poolCn, pool.CloseReasonAuthError, pool.MetricStateIdle) |
| 468 | if err != nil { |
| 469 | internal.Logger.Printf(context.Background(), "redis: failed to close connection: %v", err) |
| 470 | // try to close the network connection directly |
| 471 | // so that no resource is leaked |
| 472 | err := poolCn.Close() |
| 473 | if err != nil { |
| 474 | internal.Logger.Printf(context.Background(), "redis: failed to close network connection: %v", err) |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | internal.Logger.Printf(context.Background(), "redis: re-authentication failed: %v", err) |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | func (c *baseClient) initConn(ctx context.Context, cn *pool.Conn) error { |
| 484 | // This function is called in two scenarios: |