Skip to content

Commit e23ea02

Browse files
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 <[email protected]>
1 parent 934c6a3 commit e23ea02

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

internal/pool/pool.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ var (
1515
// ErrClosed performs any operation on the closed client will return this error.
1616
ErrClosed = errors.New("redis: client is closed")
1717

18+
// ErrPoolExhausted is returned from a pool connection method
19+
// when the maximum number of database connections in the pool has been reached.
20+
ErrPoolExhausted = errors.New("redis: connection pool exhausted")
21+
1822
// ErrPoolTimeout timed out waiting to get a connection from the connection pool.
1923
ErrPoolTimeout = errors.New("redis: connection pool timeout")
2024
)
@@ -61,6 +65,7 @@ type Options struct {
6165
PoolTimeout time.Duration
6266
MinIdleConns int
6367
MaxIdleConns int
68+
MaxActiveConns int
6469
ConnMaxIdleTime time.Duration
6570
ConnMaxLifetime time.Duration
6671
}
@@ -159,6 +164,14 @@ func (p *ConnPool) NewConn(ctx context.Context) (*Conn, error) {
159164
}
160165

161166
func (p *ConnPool) newConn(ctx context.Context, pooled bool) (*Conn, error) {
167+
if p.closed() {
168+
return nil, ErrClosed
169+
}
170+
171+
if p.cfg.MaxActiveConns > 0 && p.poolSize >= p.cfg.MaxActiveConns {
172+
return nil, ErrPoolExhausted
173+
}
174+
162175
cn, err := p.dialConn(ctx, pooled)
163176
if err != nil {
164177
return nil, err
@@ -167,12 +180,6 @@ func (p *ConnPool) newConn(ctx context.Context, pooled bool) (*Conn, error) {
167180
p.connsMu.Lock()
168181
defer p.connsMu.Unlock()
169182

170-
// It is not allowed to add new connections to the closed connection pool.
171-
if p.closed() {
172-
_ = cn.Close()
173-
return nil, ErrClosed
174-
}
175-
176183
p.conns = append(p.conns, cn)
177184
if pooled {
178185
// If pool is full remove the cn on next Put.

options.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,10 @@ type Options struct {
9898
// Note that FIFO has slightly higher overhead compared to LIFO,
9999
// but it helps closing idle connections faster reducing the pool size.
100100
PoolFIFO bool
101-
// Maximum number of socket connections.
101+
// Base number of socket connections.
102102
// Default is 10 connections per every available CPU as reported by runtime.GOMAXPROCS.
103+
// If there is not enough connections in the pool, new connections will be allocated in excess of PoolSize,
104+
// you can limit it through MaxActiveConns
103105
PoolSize int
104106
// Amount of time client waits for connection if all connections
105107
// are busy before returning an error.
@@ -112,6 +114,9 @@ type Options struct {
112114
// Maximum number of idle connections.
113115
// Default is 0. the idle connections are not closed by default.
114116
MaxIdleConns int
117+
// Maximum number of connections allocated by the pool at a given time.
118+
// When zero, there is no limit on the number of connections in the pool.
119+
MaxActiveConns int
115120
// ConnMaxIdleTime is the maximum amount of time a connection may be idle.
116121
// Should be less than server's timeout.
117122
//
@@ -502,6 +507,7 @@ func newConnPool(
502507
PoolTimeout: opt.PoolTimeout,
503508
MinIdleConns: opt.MinIdleConns,
504509
MaxIdleConns: opt.MaxIdleConns,
510+
MaxActiveConns: opt.MaxActiveConns,
505511
ConnMaxIdleTime: opt.ConnMaxIdleTime,
506512
ConnMaxLifetime: opt.ConnMaxLifetime,
507513
})

0 commit comments

Comments
 (0)