mirror of
https://github.com/redis/go-redis.git
synced 2025-07-31 05:04:23 +03:00
Pass context to Dialer
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
package pool
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
"sync"
|
||||
@ -36,7 +37,7 @@ type Pooler interface {
|
||||
NewConn() (*Conn, error)
|
||||
CloseConn(*Conn) error
|
||||
|
||||
Get() (*Conn, error)
|
||||
Get(context.Context) (*Conn, error)
|
||||
Put(*Conn)
|
||||
Remove(*Conn)
|
||||
|
||||
@ -48,7 +49,7 @@ type Pooler interface {
|
||||
}
|
||||
|
||||
type Options struct {
|
||||
Dialer func() (net.Conn, error)
|
||||
Dialer func(c context.Context) (net.Conn, error)
|
||||
OnClose func(*Conn) error
|
||||
|
||||
PoolSize int
|
||||
@ -114,7 +115,7 @@ func (p *ConnPool) checkMinIdleConns() {
|
||||
}
|
||||
|
||||
func (p *ConnPool) addIdleConn() {
|
||||
cn, err := p.newConn(true)
|
||||
cn, err := p.newConn(nil, true)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@ -126,11 +127,11 @@ func (p *ConnPool) addIdleConn() {
|
||||
}
|
||||
|
||||
func (p *ConnPool) NewConn() (*Conn, error) {
|
||||
return p._NewConn(false)
|
||||
return p._NewConn(nil, false)
|
||||
}
|
||||
|
||||
func (p *ConnPool) _NewConn(pooled bool) (*Conn, error) {
|
||||
cn, err := p.newConn(pooled)
|
||||
func (p *ConnPool) _NewConn(c context.Context, pooled bool) (*Conn, error) {
|
||||
cn, err := p.newConn(c, pooled)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -148,7 +149,7 @@ func (p *ConnPool) _NewConn(pooled bool) (*Conn, error) {
|
||||
return cn, nil
|
||||
}
|
||||
|
||||
func (p *ConnPool) newConn(pooled bool) (*Conn, error) {
|
||||
func (p *ConnPool) newConn(c context.Context, pooled bool) (*Conn, error) {
|
||||
if p.closed() {
|
||||
return nil, ErrClosed
|
||||
}
|
||||
@ -157,7 +158,7 @@ func (p *ConnPool) newConn(pooled bool) (*Conn, error) {
|
||||
return nil, p.getLastDialError()
|
||||
}
|
||||
|
||||
netConn, err := p.opt.Dialer()
|
||||
netConn, err := p.opt.Dialer(c)
|
||||
if err != nil {
|
||||
p.setLastDialError(err)
|
||||
if atomic.AddUint32(&p.dialErrorsNum, 1) == uint32(p.opt.PoolSize) {
|
||||
@ -177,7 +178,7 @@ func (p *ConnPool) tryDial() {
|
||||
return
|
||||
}
|
||||
|
||||
conn, err := p.opt.Dialer()
|
||||
conn, err := p.opt.Dialer(nil)
|
||||
if err != nil {
|
||||
p.setLastDialError(err)
|
||||
time.Sleep(time.Second)
|
||||
@ -204,7 +205,7 @@ func (p *ConnPool) getLastDialError() error {
|
||||
}
|
||||
|
||||
// Get returns existed connection from the pool or creates a new one.
|
||||
func (p *ConnPool) Get() (*Conn, error) {
|
||||
func (p *ConnPool) Get(c context.Context) (*Conn, error) {
|
||||
if p.closed() {
|
||||
return nil, ErrClosed
|
||||
}
|
||||
@ -234,7 +235,7 @@ func (p *ConnPool) Get() (*Conn, error) {
|
||||
|
||||
atomic.AddUint32(&p.stats.Misses, 1)
|
||||
|
||||
newcn, err := p._NewConn(true)
|
||||
newcn, err := p._NewConn(c, true)
|
||||
if err != nil {
|
||||
p.freeTurn()
|
||||
return nil, err
|
||||
|
Reference in New Issue
Block a user