Skip to content

Commit

Permalink
feat(pool): forward connect and disconnect events (#510)
Browse files Browse the repository at this point in the history
Make the `Pool` forward the `connect` and `disconnect` events emitted by its
clients. The `Client` instance that emitted the event is passed as
argument.
  • Loading branch information
dnlup authored Jan 6, 2021
1 parent b9a19da commit 5e51cc4
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,14 @@ Calls [`client.close(callback)`](#close) on all the clients.
Calls [`client.destroy(err, callback)`](#destroy) on all the clients.
#### Events
* `'connect'`, emitted when a client has connected, the `Client`
instance is passed as argument.
* `'disconnect'`, emitted when a client has disconnected, the `Client`
instance is passed as argument.
<a name='errors'></a>
### `undici.errors`
Expand Down
15 changes: 14 additions & 1 deletion lib/pool.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const EventEmitter = require('events')
const Client = require('./core/client')
const {
ClientClosedError,
Expand All @@ -14,11 +15,13 @@ const kDestroyed = Symbol('destroyed')
const kClosedPromise = Symbol('closed promise')
const kClosedResolve = Symbol('closed resolve')

class Pool {
class Pool extends EventEmitter {
constructor (url, {
connections,
...options
} = {}) {
super()

if (connections != null && (!Number.isFinite(connections) || connections <= 0)) {
throw new InvalidArgumentError('invalid connections')
}
Expand Down Expand Up @@ -50,8 +53,18 @@ class Pool {
}
}

function onConnect () {
pool.emit('connect', this)
}

function onDisconnect () {
pool.emit('disconnect', this)
}

for (const client of this[kClients]) {
client.on('drain', onDrain)
client.on('connect', onConnect)
client.on('disconnect', onDisconnect)
}
}

Expand Down
42 changes: 41 additions & 1 deletion test/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const proxyquire = require('proxyquire')
const { test } = require('tap')
const undici = require('..')
const { Pool, errors } = require('..')
const { Client, Pool, errors } = require('..')
const { createServer } = require('http')
const { EventEmitter } = require('events')
const { promisify } = require('util')
Expand All @@ -12,6 +12,46 @@ const eos = require('stream').finished
const net = require('net')
const EE = require('events')

test('connect/disconnect event(s)', (t) => {
const clients = 2

t.plan(clients * 3)

const server = createServer((req, res) => {
res.writeHead(200, {
Connection: 'keep-alive',
'Keep-Alive': 'timeout=1s'
})
res.end('ok')
})
t.tearDown(server.close.bind(server))

server.listen(0, () => {
const pool = new Pool(`http://localhost:${server.address().port}`, {
connections: clients,
keepAliveTimeoutThreshold: 100
})
t.tearDown(pool.close.bind(pool))

pool.on('connect', (client) => {
t.strictEqual(client instanceof Client, true)
})
pool.on('disconnect', (client) => {
t.strictEqual(client instanceof Client, true)
})

for (let i = 0; i < clients; i++) {
pool.request({
path: '/',
method: 'GET'
}, (err, { headers, body }) => {
t.error(err)
body.resume()
})
}
})
})

test('basic get', (t) => {
t.plan(9)

Expand Down

0 comments on commit 5e51cc4

Please sign in to comment.