Add OnConnect context

This commit is contained in:
Vladimir Mihailenco 2020-06-10 10:36:22 +03:00
parent e779df5ab1
commit ef82e3705c
7 changed files with 27 additions and 18 deletions

View File

@ -55,7 +55,7 @@ type ClusterOptions struct {
Dialer func(ctx context.Context, network, addr string) (net.Conn, error)
OnConnect func(*Conn) error
OnConnect func(ctx context.Context, cn *Conn) error
Username string
Password string

View File

@ -39,7 +39,7 @@ type Options struct {
Dialer func(ctx context.Context, network, addr string) (net.Conn, error)
// Hook that is called when new connection is established.
OnConnect func(*Conn) error
OnConnect func(ctx context.Context, cn *Conn) error
// Use the specified Username to authenticate the current connection with one of the connections defined in the ACL
// list when connecting to a Redis 6.0 instance, or greater, that is using the Redis ACL system.

View File

@ -269,7 +269,7 @@ func (c *baseClient) initConn(ctx context.Context, cn *pool.Conn) error {
}
if c.opt.OnConnect != nil {
return c.opt.OnConnect(conn)
return c.opt.OnConnect(ctx, conn)
}
return nil
}

View File

@ -372,7 +372,7 @@ var _ = Describe("Client OnConnect", func() {
BeforeEach(func() {
opt := redisOptions()
opt.DB = 0
opt.OnConnect = func(cn *redis.Conn) error {
opt.OnConnect = func(ctx context.Context, cn *redis.Conn) error {
return cn.ClientSetName(ctx, "on_connect").Err()
}

View File

@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"math/rand"
"net"
"strconv"
"sync"
"sync/atomic"
@ -62,7 +63,8 @@ type RingOptions struct {
// Following options are copied from Options struct.
OnConnect func(*Conn) error
Dialer func(ctx context.Context, network, addr string) (net.Conn, error)
OnConnect func(ctx context.Context, cn *Conn) error
Username string
DB int
@ -115,6 +117,7 @@ func (opt *RingOptions) init() {
func (opt *RingOptions) clientOptions() *Options {
return &Options{
Dialer: opt.Dialer,
OnConnect: opt.OnConnect,
DB: opt.DB,

View File

@ -28,7 +28,7 @@ type FailoverOptions struct {
// Following options are copied from Options struct.
Dialer func(ctx context.Context, network, addr string) (net.Conn, error)
OnConnect func(*Conn) error
OnConnect func(ctx context.Context, cn *Conn) error
Username string
Password string
@ -55,6 +55,7 @@ type FailoverOptions struct {
func (opt *FailoverOptions) options() *Options {
return &Options{
Addr: "FailoverClient",
Dialer: opt.Dialer,
OnConnect: opt.OnConnect,

View File

@ -21,21 +21,26 @@ type UniversalOptions struct {
// Common options.
Dialer func(ctx context.Context, network, addr string) (net.Conn, error)
OnConnect func(*Conn) error
OnConnect func(ctx context.Context, cn *Conn) error
Username string
Password string
MaxRetries int
MinRetryBackoff time.Duration
MaxRetryBackoff time.Duration
DialTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
PoolSize int
MinIdleConns int
MaxConnAge time.Duration
PoolTimeout time.Duration
IdleTimeout time.Duration
IdleCheckFrequency time.Duration
TLSConfig *tls.Config
// Only cluster clients.