Skip to content

Commit ff40638

Browse files
authored
Support Min connection pool parameter #3009 (#3438)
* Support Min connection pool parameter #3009 * Remove extraneous change * streamline code
1 parent 229de30 commit ff40638

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

packages/pg-pool/index.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class Pool extends EventEmitter {
8787
}
8888

8989
this.options.max = this.options.max || this.options.poolSize || 10
90+
this.options.min = this.options.min || 0
9091
this.options.maxUses = this.options.maxUses || Infinity
9192
this.options.allowExitOnIdle = this.options.allowExitOnIdle || false
9293
this.options.maxLifetimeSeconds = this.options.maxLifetimeSeconds || 0
@@ -111,6 +112,10 @@ class Pool extends EventEmitter {
111112
return this._clients.length >= this.options.max
112113
}
113114

115+
_isAboveMin() {
116+
return this._clients.length > this.options.min
117+
}
118+
114119
_pulseQueue() {
115120
this.log('pulse queue')
116121
if (this.ended) {
@@ -362,7 +367,7 @@ class Pool extends EventEmitter {
362367

363368
// idle timeout
364369
let tid
365-
if (this.options.idleTimeoutMillis) {
370+
if (this.options.idleTimeoutMillis && this._isAboveMin()) {
366371
tid = setTimeout(() => {
367372
this.log('remove idle client')
368373
this._remove(client)

packages/pg-pool/test/sizing.js

+68
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,72 @@ describe('pool size of 1', () => {
5555
return yield pool.end()
5656
})
5757
)
58+
59+
it(
60+
'does not remove clients when at or below min',
61+
co.wrap(function* () {
62+
const pool = new Pool({ max: 1, min: 1, idleTimeoutMillis: 10 })
63+
const client = yield pool.connect()
64+
client.release()
65+
yield new Promise((resolve) => setTimeout(resolve, 20))
66+
expect(pool.idleCount).to.equal(1)
67+
return yield pool.end()
68+
})
69+
)
70+
71+
it(
72+
'does remove clients when at or below min if maxUses is reached',
73+
co.wrap(function* () {
74+
const pool = new Pool({ max: 1, min: 1, idleTimeoutMillis: 10, maxUses: 1 })
75+
const client = yield pool.connect()
76+
client.release()
77+
yield new Promise((resolve) => setTimeout(resolve, 20))
78+
expect(pool.idleCount).to.equal(0)
79+
return yield pool.end()
80+
})
81+
)
82+
83+
it(
84+
'does remove clients when at or below min if maxLifetimeSeconds is reached',
85+
co.wrap(function* () {
86+
const pool = new Pool({ max: 1, min: 1, idleTimeoutMillis: 10, maxLifetimeSeconds: 1 })
87+
const client = yield pool.connect()
88+
client.release()
89+
yield new Promise((resolve) => setTimeout(resolve, 1020))
90+
expect(pool.idleCount).to.equal(0)
91+
return yield pool.end()
92+
})
93+
)
94+
})
95+
96+
describe('pool size of 2', () => {
97+
it(
98+
'does not remove clients when at or below min',
99+
co.wrap(function* () {
100+
const pool = new Pool({ max: 2, min: 2, idleTimeoutMillis: 10 })
101+
const client = yield pool.connect()
102+
const client2 = yield pool.connect()
103+
client.release()
104+
yield new Promise((resolve) => setTimeout(resolve, 20))
105+
client2.release()
106+
yield new Promise((resolve) => setTimeout(resolve, 20))
107+
expect(pool.idleCount).to.equal(2)
108+
return yield pool.end()
109+
})
110+
)
111+
112+
it(
113+
'does remove clients when above min',
114+
co.wrap(function* () {
115+
const pool = new Pool({ max: 2, min: 1, idleTimeoutMillis: 10 })
116+
const client = yield pool.connect()
117+
const client2 = yield pool.connect()
118+
client.release()
119+
yield new Promise((resolve) => setTimeout(resolve, 20))
120+
client2.release()
121+
yield new Promise((resolve) => setTimeout(resolve, 20))
122+
expect(pool.idleCount).to.equal(1)
123+
return yield pool.end()
124+
})
125+
)
58126
})

0 commit comments

Comments
 (0)