Skip to content

Commit f2f3281

Browse files
authored
feat: add getUpstream() method to BalancedPool (#4586)
Add getUpstream(upstream) method that returns the Pool instance for a given upstream URL. This allows users to retrieve specific Pool instances for debugging, monitoring, or direct operations. - Implement getUpstream() method in BalancedPool class - Add comprehensive test coverage with TDD approach - Include TypeScript definitions and type tests - All tests pass with no lint errors Signed-off-by: Matteo Collina <[email protected]>
1 parent 148e0ef commit f2f3281

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

lib/dispatcher/balanced-pool.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,16 @@ class BalancedPool extends PoolBase {
140140
return this
141141
}
142142

143+
getUpstream (upstream) {
144+
const upstreamOrigin = parseOrigin(upstream).origin
145+
146+
return this[kClients].find((pool) => (
147+
pool[kUrl].origin === upstreamOrigin &&
148+
pool.closed !== true &&
149+
pool.destroyed !== true
150+
))
151+
}
152+
143153
get upstreams () {
144154
return this[kClients]
145155
.filter(dispatcher => dispatcher.closed !== true && dispatcher.destroyed !== true)

test/node-test/balanced-pool.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,52 @@ test('throws when upstream is missing', async (t) => {
252252
}
253253
})
254254

255+
test('getUpstream returns the correct Pool for given upstream', (t) => {
256+
const p = tspl(t, { plan: 4 })
257+
258+
const upstream1 = 'http://localhost:3001'
259+
const upstream2 = 'http://localhost:3002'
260+
261+
const pool = new BalancedPool()
262+
pool.addUpstream(upstream1)
263+
pool.addUpstream(upstream2)
264+
265+
const pool1 = pool.getUpstream(upstream1)
266+
const pool2 = pool.getUpstream(upstream2)
267+
268+
p.ok(pool1 instanceof Pool)
269+
p.ok(pool2 instanceof Pool)
270+
const { kUrl } = require('../../lib/core/symbols')
271+
p.strictEqual(pool1[kUrl].origin, upstream1)
272+
p.strictEqual(pool2[kUrl].origin, upstream2)
273+
})
274+
275+
test('getUpstream returns undefined for non-existent upstream', (t) => {
276+
const p = tspl(t, { plan: 1 })
277+
278+
const pool = new BalancedPool()
279+
pool.addUpstream('http://localhost:3001')
280+
281+
const result = pool.getUpstream('http://localhost:9999')
282+
p.strictEqual(result, undefined)
283+
})
284+
285+
test('getUpstream returns undefined for closed/destroyed upstream', (t) => {
286+
const p = tspl(t, { plan: 2 })
287+
288+
const upstream = 'http://localhost:3001'
289+
const pool = new BalancedPool()
290+
pool.addUpstream(upstream)
291+
292+
const upstreamPool = pool.getUpstream(upstream)
293+
p.ok(upstreamPool instanceof Pool)
294+
295+
upstreamPool.destroy()
296+
297+
const result = pool.getUpstream(upstream)
298+
p.strictEqual(result, undefined)
299+
})
300+
255301
class TestServer {
256302
constructor ({ config: { server, socketHangup, downOnRequests, socketHangupOnRequests }, onRequest }) {
257303
this.config = {

test/types/balanced-pool.test-d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Duplex, Readable, Writable } from 'node:stream'
22
import { expectAssignable } from 'tsd'
3-
import { Dispatcher, BalancedPool, Client } from '../..'
3+
import { Dispatcher, BalancedPool, Client, Pool } from '../..'
44
import { URL } from 'node:url'
55

66
expectAssignable<BalancedPool>(new BalancedPool(''))
@@ -24,6 +24,8 @@ expectAssignable<BalancedPool>(new BalancedPool([new URL('http://localhost:4242'
2424
expectAssignable<BalancedPool>(pool.removeUpstream('http://www.nodejs.org'))
2525
expectAssignable<BalancedPool>(pool.addUpstream(new URL('http://www.nodejs.org')))
2626
expectAssignable<BalancedPool>(pool.removeUpstream(new URL('http://www.nodejs.org')))
27+
expectAssignable<Pool | undefined>(pool.getUpstream('http://www.nodejs.org'))
28+
expectAssignable<Pool | undefined>(pool.getUpstream(new URL('http://www.nodejs.org')))
2729
expectAssignable<string[]>(pool.upstreams)
2830

2931
// request

types/balanced-pool.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ declare class BalancedPool extends Dispatcher {
1111

1212
addUpstream (upstream: string | URL): BalancedPool
1313
removeUpstream (upstream: string | URL): BalancedPool
14+
getUpstream (upstream: string | URL): Pool | undefined
1415
upstreams: Array<string>
1516

1617
/** `true` after `pool.close()` has been called. */

0 commit comments

Comments
 (0)