Skip to content

Commit 2d8fa02

Browse files
authored
fix: fix #2681 (#2998)
Signed-off-by: monkey92t <[email protected]>
1 parent 0f0a284 commit 2d8fa02

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

options.go

+6
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ type Options struct {
6161
// before reconnecting. It should return the current username and password.
6262
CredentialsProvider func() (username string, password string)
6363

64+
// CredentialsProviderContext is an enhanced parameter of CredentialsProvider,
65+
// done to maintain API compatibility. In the future,
66+
// there might be a merge between CredentialsProviderContext and CredentialsProvider.
67+
// There will be a conflict between them; if CredentialsProviderContext exists, we will ignore CredentialsProvider.
68+
CredentialsProviderContext func(ctx context.Context) (username string, password string, err error)
69+
6470
// Database to be selected after connecting to the server.
6571
DB int
6672

osscluster.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,11 @@ type ClusterOptions struct {
6262

6363
OnConnect func(ctx context.Context, cn *Conn) error
6464

65-
Protocol int
66-
Username string
67-
Password string
68-
CredentialsProvider func() (username string, password string)
65+
Protocol int
66+
Username string
67+
Password string
68+
CredentialsProvider func() (username string, password string)
69+
CredentialsProviderContext func(ctx context.Context) (username string, password string, err error)
6970

7071
MaxRetries int
7172
MinRetryBackoff time.Duration
@@ -272,10 +273,11 @@ func (opt *ClusterOptions) clientOptions() *Options {
272273
Dialer: opt.Dialer,
273274
OnConnect: opt.OnConnect,
274275

275-
Protocol: opt.Protocol,
276-
Username: opt.Username,
277-
Password: opt.Password,
278-
CredentialsProvider: opt.CredentialsProvider,
276+
Protocol: opt.Protocol,
277+
Username: opt.Username,
278+
Password: opt.Password,
279+
CredentialsProvider: opt.CredentialsProvider,
280+
CredentialsProviderContext: opt.CredentialsProviderContext,
279281

280282
MaxRetries: opt.MaxRetries,
281283
MinRetryBackoff: opt.MinRetryBackoff,

redis.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,13 @@ func (c *baseClient) initConn(ctx context.Context, cn *pool.Conn) error {
283283
}
284284
cn.Inited = true
285285

286+
var err error
286287
username, password := c.opt.Username, c.opt.Password
287-
if c.opt.CredentialsProvider != nil {
288+
if c.opt.CredentialsProviderContext != nil {
289+
if username, password, err = c.opt.CredentialsProviderContext(ctx); err != nil {
290+
return err
291+
}
292+
} else if c.opt.CredentialsProvider != nil {
288293
username, password = c.opt.CredentialsProvider()
289294
}
290295

@@ -300,7 +305,7 @@ func (c *baseClient) initConn(ctx context.Context, cn *pool.Conn) error {
300305

301306
// for redis-server versions that do not support the HELLO command,
302307
// RESP2 will continue to be used.
303-
if err := conn.Hello(ctx, protocol, username, password, "").Err(); err == nil {
308+
if err = conn.Hello(ctx, protocol, username, password, "").Err(); err == nil {
304309
auth = true
305310
} else if !isRedisError(err) {
306311
// When the server responds with the RESP protocol and the result is not a normal
@@ -313,7 +318,7 @@ func (c *baseClient) initConn(ctx context.Context, cn *pool.Conn) error {
313318
return err
314319
}
315320

316-
_, err := conn.Pipelined(ctx, func(pipe Pipeliner) error {
321+
_, err = conn.Pipelined(ctx, func(pipe Pipeliner) error {
317322
if !auth && password != "" {
318323
if username != "" {
319324
pipe.AuthACL(ctx, username, password)

0 commit comments

Comments
 (0)