Skip to content

Commit

Permalink
Added MaxActiveConns (#2646)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
nvorobev and ofekshenawa authored Sep 20, 2023
1 parent 934c6a3 commit e23ea02
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
19 changes: 13 additions & 6 deletions internal/pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ var (
// ErrClosed performs any operation on the closed client will return this error.
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 = errors.New("redis: connection pool timeout")
)
Expand Down Expand Up @@ -61,6 +65,7 @@ type Options struct {
PoolTimeout time.Duration
MinIdleConns int
MaxIdleConns int
MaxActiveConns int
ConnMaxIdleTime time.Duration
ConnMaxLifetime time.Duration
}
Expand Down Expand Up @@ -159,6 +164,14 @@ func (p *ConnPool) NewConn(ctx context.Context) (*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)
if err != nil {
return nil, err
Expand All @@ -167,12 +180,6 @@ func (p *ConnPool) newConn(ctx context.Context, pooled bool) (*Conn, error) {
p.connsMu.Lock()
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)
if pooled {
// If pool is full remove the cn on next Put.
Expand Down
8 changes: 7 additions & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ type Options struct {
// Note that FIFO has slightly higher overhead compared to LIFO,
// but it helps closing idle connections faster reducing the pool size.
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.
// 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
// Amount of time client waits for connection if all connections
// are busy before returning an error.
Expand All @@ -112,6 +114,9 @@ type Options struct {
// Maximum number of idle connections.
// Default is 0. the idle connections are not closed by default.
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.
// Should be less than server's timeout.
//
Expand Down Expand Up @@ -502,6 +507,7 @@ func newConnPool(
PoolTimeout: opt.PoolTimeout,
MinIdleConns: opt.MinIdleConns,
MaxIdleConns: opt.MaxIdleConns,
MaxActiveConns: opt.MaxActiveConns,
ConnMaxIdleTime: opt.ConnMaxIdleTime,
ConnMaxLifetime: opt.ConnMaxLifetime,
})
Expand Down

0 comments on commit e23ea02

Please sign in to comment.