Skip to content

Commit

Permalink
Add OnConnect context
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Jun 10, 2020
1 parent e779df5 commit ef82e37
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 18 deletions.
2 changes: 1 addition & 1 deletion cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
5 changes: 4 additions & 1 deletion ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"math/rand"
"net"
"strconv"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -115,6 +117,7 @@ func (opt *RingOptions) init() {

func (opt *RingOptions) clientOptions() *Options {
return &Options{
Dialer: opt.Dialer,
OnConnect: opt.OnConnect,

DB: opt.DB,
Expand Down
5 changes: 3 additions & 2 deletions sentinel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -54,7 +54,8 @@ type FailoverOptions struct {

func (opt *FailoverOptions) options() *Options {
return &Options{
Addr: "FailoverClient",
Addr: "FailoverClient",

Dialer: opt.Dialer,
OnConnect: opt.OnConnect,

Expand Down
27 changes: 16 additions & 11 deletions universal.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,28 @@ type UniversalOptions struct {

// Common options.

Dialer func(ctx context.Context, network, addr string) (net.Conn, error)
OnConnect func(*Conn) error
Username string
Password string
MaxRetries int
MinRetryBackoff time.Duration
MaxRetryBackoff time.Duration
DialTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
Dialer func(ctx context.Context, network, addr string) (net.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

TLSConfig *tls.Config

// Only cluster clients.

Expand Down

0 comments on commit ef82e37

Please sign in to comment.