mirror of
https://github.com/redis/go-redis.git
synced 2025-04-19 07:22:17 +03:00
Added MaxActiveConns (#2646)
* Added the ability to set a connection growth limit when there are not enough connections in the pool using MaxActiveConns * fix comment * fix * fix --------- Co-authored-by: ofekshenawa <104765379+ofekshenawa@users.noreply.github.com>
This commit is contained in:
parent
934c6a3fe0
commit
e23ea028bd
@ -15,6 +15,10 @@ var (
|
|||||||
// ErrClosed performs any operation on the closed client will return this error.
|
// ErrClosed performs any operation on the closed client will return this error.
|
||||||
ErrClosed = errors.New("redis: client is closed")
|
ErrClosed = errors.New("redis: client is closed")
|
||||||
|
|
||||||
|
// ErrPoolExhausted is returned from a pool connection method
|
||||||
|
// when the maximum number of database connections in the pool has been reached.
|
||||||
|
ErrPoolExhausted = errors.New("redis: connection pool exhausted")
|
||||||
|
|
||||||
// ErrPoolTimeout timed out waiting to get a connection from the connection pool.
|
// ErrPoolTimeout timed out waiting to get a connection from the connection pool.
|
||||||
ErrPoolTimeout = errors.New("redis: connection pool timeout")
|
ErrPoolTimeout = errors.New("redis: connection pool timeout")
|
||||||
)
|
)
|
||||||
@ -61,6 +65,7 @@ type Options struct {
|
|||||||
PoolTimeout time.Duration
|
PoolTimeout time.Duration
|
||||||
MinIdleConns int
|
MinIdleConns int
|
||||||
MaxIdleConns int
|
MaxIdleConns int
|
||||||
|
MaxActiveConns int
|
||||||
ConnMaxIdleTime time.Duration
|
ConnMaxIdleTime time.Duration
|
||||||
ConnMaxLifetime time.Duration
|
ConnMaxLifetime time.Duration
|
||||||
}
|
}
|
||||||
@ -159,6 +164,14 @@ func (p *ConnPool) NewConn(ctx context.Context) (*Conn, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *ConnPool) newConn(ctx context.Context, pooled bool) (*Conn, error) {
|
func (p *ConnPool) newConn(ctx context.Context, pooled bool) (*Conn, error) {
|
||||||
|
if p.closed() {
|
||||||
|
return nil, ErrClosed
|
||||||
|
}
|
||||||
|
|
||||||
|
if p.cfg.MaxActiveConns > 0 && p.poolSize >= p.cfg.MaxActiveConns {
|
||||||
|
return nil, ErrPoolExhausted
|
||||||
|
}
|
||||||
|
|
||||||
cn, err := p.dialConn(ctx, pooled)
|
cn, err := p.dialConn(ctx, pooled)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -167,12 +180,6 @@ func (p *ConnPool) newConn(ctx context.Context, pooled bool) (*Conn, error) {
|
|||||||
p.connsMu.Lock()
|
p.connsMu.Lock()
|
||||||
defer p.connsMu.Unlock()
|
defer p.connsMu.Unlock()
|
||||||
|
|
||||||
// It is not allowed to add new connections to the closed connection pool.
|
|
||||||
if p.closed() {
|
|
||||||
_ = cn.Close()
|
|
||||||
return nil, ErrClosed
|
|
||||||
}
|
|
||||||
|
|
||||||
p.conns = append(p.conns, cn)
|
p.conns = append(p.conns, cn)
|
||||||
if pooled {
|
if pooled {
|
||||||
// If pool is full remove the cn on next Put.
|
// If pool is full remove the cn on next Put.
|
||||||
|
@ -98,8 +98,10 @@ type Options struct {
|
|||||||
// Note that FIFO has slightly higher overhead compared to LIFO,
|
// Note that FIFO has slightly higher overhead compared to LIFO,
|
||||||
// but it helps closing idle connections faster reducing the pool size.
|
// but it helps closing idle connections faster reducing the pool size.
|
||||||
PoolFIFO bool
|
PoolFIFO bool
|
||||||
// Maximum number of socket connections.
|
// Base number of socket connections.
|
||||||
// Default is 10 connections per every available CPU as reported by runtime.GOMAXPROCS.
|
// Default is 10 connections per every available CPU as reported by runtime.GOMAXPROCS.
|
||||||
|
// If there is not enough connections in the pool, new connections will be allocated in excess of PoolSize,
|
||||||
|
// you can limit it through MaxActiveConns
|
||||||
PoolSize int
|
PoolSize int
|
||||||
// Amount of time client waits for connection if all connections
|
// Amount of time client waits for connection if all connections
|
||||||
// are busy before returning an error.
|
// are busy before returning an error.
|
||||||
@ -112,6 +114,9 @@ type Options struct {
|
|||||||
// Maximum number of idle connections.
|
// Maximum number of idle connections.
|
||||||
// Default is 0. the idle connections are not closed by default.
|
// Default is 0. the idle connections are not closed by default.
|
||||||
MaxIdleConns int
|
MaxIdleConns int
|
||||||
|
// Maximum number of connections allocated by the pool at a given time.
|
||||||
|
// When zero, there is no limit on the number of connections in the pool.
|
||||||
|
MaxActiveConns int
|
||||||
// ConnMaxIdleTime is the maximum amount of time a connection may be idle.
|
// ConnMaxIdleTime is the maximum amount of time a connection may be idle.
|
||||||
// Should be less than server's timeout.
|
// Should be less than server's timeout.
|
||||||
//
|
//
|
||||||
@ -502,6 +507,7 @@ func newConnPool(
|
|||||||
PoolTimeout: opt.PoolTimeout,
|
PoolTimeout: opt.PoolTimeout,
|
||||||
MinIdleConns: opt.MinIdleConns,
|
MinIdleConns: opt.MinIdleConns,
|
||||||
MaxIdleConns: opt.MaxIdleConns,
|
MaxIdleConns: opt.MaxIdleConns,
|
||||||
|
MaxActiveConns: opt.MaxActiveConns,
|
||||||
ConnMaxIdleTime: opt.ConnMaxIdleTime,
|
ConnMaxIdleTime: opt.ConnMaxIdleTime,
|
||||||
ConnMaxLifetime: opt.ConnMaxLifetime,
|
ConnMaxLifetime: opt.ConnMaxLifetime,
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user