mirror of
https://github.com/redis/go-redis.git
synced 2025-04-17 20:17:02 +03:00
parent
0f0a28464c
commit
2d8fa02ac2
@ -61,6 +61,12 @@ type Options struct {
|
|||||||
// before reconnecting. It should return the current username and password.
|
// before reconnecting. It should return the current username and password.
|
||||||
CredentialsProvider func() (username string, password string)
|
CredentialsProvider func() (username string, password string)
|
||||||
|
|
||||||
|
// CredentialsProviderContext is an enhanced parameter of CredentialsProvider,
|
||||||
|
// done to maintain API compatibility. In the future,
|
||||||
|
// there might be a merge between CredentialsProviderContext and CredentialsProvider.
|
||||||
|
// There will be a conflict between them; if CredentialsProviderContext exists, we will ignore CredentialsProvider.
|
||||||
|
CredentialsProviderContext func(ctx context.Context) (username string, password string, err error)
|
||||||
|
|
||||||
// Database to be selected after connecting to the server.
|
// Database to be selected after connecting to the server.
|
||||||
DB int
|
DB int
|
||||||
|
|
||||||
|
@ -62,10 +62,11 @@ type ClusterOptions struct {
|
|||||||
|
|
||||||
OnConnect func(ctx context.Context, cn *Conn) error
|
OnConnect func(ctx context.Context, cn *Conn) error
|
||||||
|
|
||||||
Protocol int
|
Protocol int
|
||||||
Username string
|
Username string
|
||||||
Password string
|
Password string
|
||||||
CredentialsProvider func() (username string, password string)
|
CredentialsProvider func() (username string, password string)
|
||||||
|
CredentialsProviderContext func(ctx context.Context) (username string, password string, err error)
|
||||||
|
|
||||||
MaxRetries int
|
MaxRetries int
|
||||||
MinRetryBackoff time.Duration
|
MinRetryBackoff time.Duration
|
||||||
@ -272,10 +273,11 @@ func (opt *ClusterOptions) clientOptions() *Options {
|
|||||||
Dialer: opt.Dialer,
|
Dialer: opt.Dialer,
|
||||||
OnConnect: opt.OnConnect,
|
OnConnect: opt.OnConnect,
|
||||||
|
|
||||||
Protocol: opt.Protocol,
|
Protocol: opt.Protocol,
|
||||||
Username: opt.Username,
|
Username: opt.Username,
|
||||||
Password: opt.Password,
|
Password: opt.Password,
|
||||||
CredentialsProvider: opt.CredentialsProvider,
|
CredentialsProvider: opt.CredentialsProvider,
|
||||||
|
CredentialsProviderContext: opt.CredentialsProviderContext,
|
||||||
|
|
||||||
MaxRetries: opt.MaxRetries,
|
MaxRetries: opt.MaxRetries,
|
||||||
MinRetryBackoff: opt.MinRetryBackoff,
|
MinRetryBackoff: opt.MinRetryBackoff,
|
||||||
|
11
redis.go
11
redis.go
@ -283,8 +283,13 @@ func (c *baseClient) initConn(ctx context.Context, cn *pool.Conn) error {
|
|||||||
}
|
}
|
||||||
cn.Inited = true
|
cn.Inited = true
|
||||||
|
|
||||||
|
var err error
|
||||||
username, password := c.opt.Username, c.opt.Password
|
username, password := c.opt.Username, c.opt.Password
|
||||||
if c.opt.CredentialsProvider != nil {
|
if c.opt.CredentialsProviderContext != nil {
|
||||||
|
if username, password, err = c.opt.CredentialsProviderContext(ctx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else if c.opt.CredentialsProvider != nil {
|
||||||
username, password = c.opt.CredentialsProvider()
|
username, password = c.opt.CredentialsProvider()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -300,7 +305,7 @@ func (c *baseClient) initConn(ctx context.Context, cn *pool.Conn) error {
|
|||||||
|
|
||||||
// for redis-server versions that do not support the HELLO command,
|
// for redis-server versions that do not support the HELLO command,
|
||||||
// RESP2 will continue to be used.
|
// RESP2 will continue to be used.
|
||||||
if err := conn.Hello(ctx, protocol, username, password, "").Err(); err == nil {
|
if err = conn.Hello(ctx, protocol, username, password, "").Err(); err == nil {
|
||||||
auth = true
|
auth = true
|
||||||
} else if !isRedisError(err) {
|
} else if !isRedisError(err) {
|
||||||
// When the server responds with the RESP protocol and the result is not a normal
|
// 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 {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := conn.Pipelined(ctx, func(pipe Pipeliner) error {
|
_, err = conn.Pipelined(ctx, func(pipe Pipeliner) error {
|
||||||
if !auth && password != "" {
|
if !auth && password != "" {
|
||||||
if username != "" {
|
if username != "" {
|
||||||
pipe.AuthACL(ctx, username, password)
|
pipe.AuthACL(ctx, username, password)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user