Skip to content

Commit 90bfdb3

Browse files
committed
better conn usable timeout
1 parent 6c54ab5 commit 90bfdb3

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

auth/conn_reauth_credentials_listener.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,18 @@ import (
1313
// - reAuth: a function that takes the new credentials and returns an error if any.
1414
// - onErr: a function that takes an error and handles it.
1515
// - conn: the connection to re-authenticate.
16+
// - checkUsableTimeout: the timeout to wait for the connection to be usable - default is 1 second.
1617
type ConnReAuthCredentialsListener struct {
18+
// reAuth is called when the credentials are updated.
1719
reAuth func(conn *pool.Conn, credentials Credentials) error
18-
onErr func(conn *pool.Conn, err error)
19-
conn *pool.Conn
20+
// onErr is called when an error occurs.
21+
onErr func(conn *pool.Conn, err error)
22+
// conn is the connection to re-authenticate.
23+
conn *pool.Conn
24+
// checkUsableTimeout is the timeout to wait for the connection to be usable
25+
// when the credentials are updated.
26+
// default is 1 second
27+
checkUsableTimeout time.Duration
2028
}
2129

2230
// OnNext is called when the credentials are updated.
@@ -32,7 +40,9 @@ func (c *ConnReAuthCredentialsListener) OnNext(credentials Credentials) {
3240
}
3341

3442
var err error
35-
timeout := time.After(1 * time.Second)
43+
44+
// this hard-coded timeout is not ideal
45+
timeout := time.After(c.checkUsableTimeout)
3646
// wait for the connection to be usable
3747
// this is important because the connection pool may be in the process of reconnecting the connection
3848
// and we don't want to interfere with that process
@@ -68,15 +78,22 @@ func (c *ConnReAuthCredentialsListener) OnError(err error) {
6878
c.onErr(c.conn, err)
6979
}
7080

81+
// SetCheckUsableTimeout sets the timeout for the connection to be usable.
82+
func (c *ConnReAuthCredentialsListener) SetCheckUsableTimeout(timeout time.Duration) {
83+
c.checkUsableTimeout = timeout
84+
}
85+
7186
// NewConnReAuthCredentialsListener creates a new ConnReAuthCredentialsListener.
7287
// Implements the auth.CredentialsListener interface.
7388
func NewConnReAuthCredentialsListener(conn *pool.Conn, reAuth func(conn *pool.Conn, credentials Credentials) error, onErr func(conn *pool.Conn, err error)) *ConnReAuthCredentialsListener {
7489
return &ConnReAuthCredentialsListener{
75-
conn: conn,
76-
reAuth: reAuth,
77-
onErr: onErr,
90+
conn: conn,
91+
reAuth: reAuth,
92+
onErr: onErr,
93+
checkUsableTimeout: 1 * time.Second,
7894
}
7995
}
8096

97+
8198
// Ensure ConnReAuthCredentialsListener implements the CredentialsListener interface.
8299
var _ CredentialsListener = (*ConnReAuthCredentialsListener)(nil)

error.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,12 @@ func isRedisError(err error) bool {
108108

109109
func isBadConn(err error, allowTimeout bool, addr string) bool {
110110
switch err {
111-
case nil:
112-
return false
113-
case context.Canceled, context.DeadlineExceeded:
114-
return true
111+
case nil:
112+
return false
113+
case context.Canceled, context.DeadlineExceeded:
114+
return true
115115
case pool.ErrConnUnusableTimeout:
116-
return true
116+
return true
117117
}
118118

119119
if isRedisError(err) {

redis.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,12 @@ func (c *baseClient) connReAuthCredentialsListener(poolCn *pool.Conn) (auth.Cred
319319
c.reAuthConnection(),
320320
c.onAuthenticationErr(),
321321
)
322+
// main case where the connection can be stuck in the listener for a long time is when we have a handoff
323+
// so we set the checkUsableTimeout to the handoff timeout if maintnotifications are enabled
324+
// the default timeout if no maintnotifications config is provided is 1 second
325+
if c.opt.MaintNotificationsConfig != nil && c.opt.MaintNotificationsConfig.Mode != maintnotifications.ModeDisabled {
326+
newCredListener.SetCheckUsableTimeout(c.opt.MaintNotificationsConfig.HandoffTimeout)
327+
}
322328
c.credListeners[poolCn] = newCredListener
323329
return newCredListener, func() {
324330
c.removeCredListener(poolCn)

0 commit comments

Comments
 (0)