@@ -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.
1617type 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.
7388func 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.
8299var _ CredentialsListener = (* ConnReAuthCredentialsListener )(nil )
0 commit comments