| 1 |
'use strict' |
| 2 |
|
| 3 |
const { |
| 4 |
PoolBase, |
| 5 |
kClients, |
| 6 |
kNeedDrain, |
| 7 |
kAddClient, |
| 8 |
kGetDispatcher, |
| 9 |
kHasDispatcher, |
| 10 |
kRemoveClient |
| 11 |
} = require('./pool-base') |
| 12 |
const Client = require('./client') |
| 13 |
const { |
| 14 |
InvalidArgumentError |
| 15 |
} = require('../core/errors') |
| 16 |
const util = require('../core/util') |
| 17 |
const { kUrl } = require('../core/symbols') |
| 18 |
const buildConnector = require('../core/connect') |
| 19 |
|
| 20 |
const kOptions = Symbol('options') |
| 21 |
const kConnections = Symbol('connections') |
| 22 |
const kFactory = Symbol('factory') |
| 23 |
|
| 24 |
function defaultFactory (origin, opts) { |
| 25 |
return new Client(origin, opts) |
| 26 |
} |
| 27 |
|
| 28 |
class Pool extends PoolBase { |
| 29 |
constructor (origin, { |
| 30 |
connections, |
| 31 |
factory = defaultFactory, |
| 32 |
connect, |
| 33 |
connectTimeout, |
| 34 |
tls, |
| 35 |
maxCachedSessions, |
| 36 |
socketPath, |
| 37 |
autoSelectFamily, |
| 38 |
autoSelectFamilyAttemptTimeout, |
| 39 |
allowH2, |
| 40 |
useH2c, |
| 41 |
clientTtl, |
| 42 |
...options |
| 43 |
} = {}) { |
| 44 |
if (connections != null && (!Number.isFinite(connections) || connections < 0)) { |
| 45 |
throw new InvalidArgumentError('invalid connections') |
| 46 |
} |
| 47 |
|
| 48 |
if (typeof factory !== 'function') { |
| 49 |
throw new InvalidArgumentError('factory must be a function.') |
| 50 |
} |
| 51 |
|
| 52 |
if (connect != null && typeof connect !== 'function' && typeof connect !== 'object') { |
| 53 |
throw new InvalidArgumentError('connect must be a function or an object') |
| 54 |
} |
| 55 |
|
| 56 |
if (typeof connect !== 'function') { |
| 57 |
connect = buildConnector({ |
| 58 |
...tls, |
| 59 |
maxCachedSessions, |
| 60 |
allowH2, |
| 61 |
useH2c, |
| 62 |
socketPath, |
| 63 |
timeout: connectTimeout, |
| 64 |
...(typeof autoSelectFamily === 'boolean' ? { autoSelectFamily, autoSelectFamilyAttemptTimeout } : undefined), |
| 65 |
...connect |
| 66 |
}) |
| 67 |
} |
| 68 |
|
| 69 |
super(options) |
| 70 |
|
| 71 |
this[kConnections] = connections || null |
| 72 |
this[kUrl] = util.parseOrigin(origin) |
| 73 |
this[kOptions] = { ...util.deepClone(options), connect, allowH2, useH2c, clientTtl, socketPath } |
| 74 |
this[kFactory] = factory |
| 75 |
|
| 76 |
this.on('connect', (origin, targets) => { |
| 77 |
if (clientTtl != null && clientTtl > 0) { |
| 78 |
for (const target of targets) { |
| 79 |
Object.assign(target, { ttl: Date.now() }) |
| 80 |
} |
| 81 |
} |
| 82 |
}) |
| 83 |
|
| 84 |
this.on('connectionError', (origin, targets, error) => { |
| 85 |
// If a connection error occurs, we remove the client from the pool, |
| 86 |
// and emit a connectionError event. They will not be re-used. |
| 87 |
// Fixes https://github.com/nodejs/undici/issues/3895 |
| 88 |
for (const target of targets) { |
| 89 |
// Do not use kRemoveClient here, as it will close the client, |
| 90 |
// but the client cannot be closed in this state. |
| 91 |
const idx = this[kClients].indexOf(target) |
| 92 |
if (idx !== -1) { |
| 93 |
this[kClients].splice(idx, 1) |
| 94 |
} |
| 95 |
} |
| 96 |
}) |
| 97 |
} |
| 98 |
|
| 99 |
[kGetDispatcher] () { |
| 100 |
const clientTtlOption = this[kOptions].clientTtl |
| 101 |
for (let i = 0; i < this[kClients].length; i++) { |
| 102 |
const client = this[kClients][i] |
| 103 |
|
| 104 |
// check ttl of client and if it's stale, remove it from the pool |
| 105 |
if (clientTtlOption != null && clientTtlOption > 0 && client.ttl && ((Date.now() - client.ttl) > clientTtlOption)) { |
| 106 |
this[kRemoveClient](client) |
| 107 |
i-- |
| 108 |
} else if (!client[kNeedDrain]) { |
| 109 |
return client |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
if (!this[kConnections] || this[kClients].length < this[kConnections]) { |
| 114 |
const dispatcher = this[kFactory](this[kUrl], this[kOptions]) |
| 115 |
this[kAddClient](dispatcher) |
| 116 |
return dispatcher |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
[kHasDispatcher] () { |
| 121 |
const clientTtlOption = this[kOptions].clientTtl |
| 122 |
for (let i = 0; i < this[kClients].length; i++) { |
| 123 |
const client = this[kClients][i] |
| 124 |
|
| 125 |
if (clientTtlOption != null && clientTtlOption > 0 && client.ttl && ((Date.now() - client.ttl) > clientTtlOption)) { |
| 126 |
this[kRemoveClient](client) |
| 127 |
i-- |
| 128 |
} else if (!client[kNeedDrain]) { |
| 129 |
return true |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
if (!this[kConnections] || this[kClients].length < this[kConnections]) { |
| 134 |
const dispatcher = this[kFactory](this[kUrl], this[kOptions]) |
| 135 |
this[kAddClient](dispatcher) |
| 136 |
return true |
| 137 |
} |
| 138 |
|
| 139 |
return false |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
module.exports = Pool |