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) 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 Username string
Password string Password string

View File

@ -39,7 +39,7 @@ type Options struct {
Dialer func(ctx context.Context, network, addr string) (net.Conn, error) Dialer func(ctx context.Context, network, addr string) (net.Conn, error)
// Hook that is called when new connection is established. // 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 // 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. // 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 { if c.opt.OnConnect != nil {
return c.opt.OnConnect(conn) return c.opt.OnConnect(ctx, conn)
} }
return nil return nil
} }

View File

@ -372,7 +372,7 @@ var _ = Describe("Client OnConnect", func() {
BeforeEach(func() { BeforeEach(func() {
opt := redisOptions() opt := redisOptions()
opt.DB = 0 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() return cn.ClientSetName(ctx, "on_connect").Err()
} }

View File

@ -5,6 +5,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"math/rand" "math/rand"
"net"
"strconv" "strconv"
"sync" "sync"
"sync/atomic" "sync/atomic"
@ -62,7 +63,8 @@ type RingOptions struct {
// Following options are copied from Options 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 Username string
DB int DB int
@ -115,6 +117,7 @@ func (opt *RingOptions) init() {
func (opt *RingOptions) clientOptions() *Options { func (opt *RingOptions) clientOptions() *Options {
return &Options{ return &Options{
Dialer: opt.Dialer,
OnConnect: opt.OnConnect, OnConnect: opt.OnConnect,
DB: opt.DB, DB: opt.DB,

View File

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

View File

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