WIP: initial prototype
- Added client.close API - Added resurrect event - Improved resurrect ping strategy - Updated types
This commit is contained in:
2
lib/Connection.d.ts
vendored
2
lib/Connection.d.ts
vendored
@ -46,7 +46,7 @@ export default class Connection {
|
||||
_status: string;
|
||||
_agent: http.Agent;
|
||||
constructor(opts?: ConnectionOptions);
|
||||
request(params: any, callback: (err: Error | null, response: http.IncomingMessage | null) => void): http.ClientRequest;
|
||||
request(params: http.ClientRequestArgs, callback: (err: Error | null, response: http.IncomingMessage | null) => void): http.ClientRequest;
|
||||
close(): Connection;
|
||||
setRole(role: string, enabled: boolean): Connection;
|
||||
status: string;
|
||||
|
||||
@ -64,7 +64,7 @@ class Connection {
|
||||
ended = true
|
||||
this._openRequests--
|
||||
request.abort()
|
||||
callback(new TimeoutError('Request timed out', params))
|
||||
callback(new TimeoutError('Request timed out', params), null)
|
||||
}
|
||||
})
|
||||
|
||||
@ -73,7 +73,7 @@ class Connection {
|
||||
if (ended === false) {
|
||||
ended = true
|
||||
this._openRequests--
|
||||
callback(err)
|
||||
callback(err, null)
|
||||
}
|
||||
})
|
||||
|
||||
@ -95,7 +95,7 @@ class Connection {
|
||||
if (err != null && ended === false) {
|
||||
ended = true
|
||||
this._openRequests--
|
||||
callback(err)
|
||||
callback(err, null)
|
||||
}
|
||||
})
|
||||
} else {
|
||||
@ -152,6 +152,7 @@ class Connection {
|
||||
pathname: url.pathname,
|
||||
path: '',
|
||||
href: url.href,
|
||||
origin: url.origin,
|
||||
port: url.port,
|
||||
headers: this.headers,
|
||||
auth: !!url.username === true || !!url.password === true
|
||||
|
||||
6
lib/ConnectionPool.d.ts
vendored
6
lib/ConnectionPool.d.ts
vendored
@ -27,6 +27,12 @@ export interface getConnectionOptions {
|
||||
selector?: nodeSelectorFn;
|
||||
}
|
||||
|
||||
export interface ResurrectMeta {
|
||||
strategy: string;
|
||||
isAlive: boolean;
|
||||
connection: Connection;
|
||||
}
|
||||
|
||||
export default class ConnectionPool {
|
||||
static resurrectStrategies: {
|
||||
none: number;
|
||||
|
||||
@ -26,6 +26,7 @@ class ConnectionPool {
|
||||
this.randomizeHost = opts.randomizeHost === true
|
||||
this.nodeFilter = opts.nodeFilter || defaultNodeFilter
|
||||
this.Connection = opts.Connection
|
||||
this.emit = opts.emit || noop
|
||||
|
||||
if (typeof opts.nodeSelector === 'function') {
|
||||
this.nodeSelector = opts.nodeSelector
|
||||
@ -107,6 +108,7 @@ class ConnectionPool {
|
||||
*/
|
||||
resurrect (now = Date.now(), callback = noop) {
|
||||
if (this.resurrectStrategy === 0 || this.dead.length === 0) {
|
||||
debug('Nothing to resurrect')
|
||||
callback(null, null)
|
||||
return
|
||||
}
|
||||
@ -116,6 +118,7 @@ class ConnectionPool {
|
||||
const connection = this.connections.get(this.dead[0])
|
||||
if (now < connection.resurrectTimeout) {
|
||||
debug('Nothing to resurrect')
|
||||
callback(null, null)
|
||||
return
|
||||
}
|
||||
|
||||
@ -127,9 +130,11 @@ class ConnectionPool {
|
||||
method: 'HEAD',
|
||||
path: '/',
|
||||
timeout: this.pingTimeout
|
||||
}, (err, res) => {
|
||||
}, (err, response) => {
|
||||
var isAlive = true
|
||||
if (err != null) {
|
||||
const statusCode = response !== null ? response.statusCode : 0
|
||||
if (err != null ||
|
||||
(statusCode === 502 || statusCode === 503 || statusCode === 504)) {
|
||||
debug(`Resurrect: connection '${id}' is still dead`)
|
||||
this.markDead(connection)
|
||||
isAlive = false
|
||||
@ -137,6 +142,7 @@ class ConnectionPool {
|
||||
debug(`Resurrect: connection '${id}' is now alive`)
|
||||
this.markAlive(connection)
|
||||
}
|
||||
this.emit('resurrect', null, { strategy: 'ping', isAlive, connection })
|
||||
callback(isAlive, connection)
|
||||
})
|
||||
// optimistic strategy
|
||||
@ -144,6 +150,7 @@ class ConnectionPool {
|
||||
debug(`Resurrect: optimistic resurrection for connection '${id}'`)
|
||||
this.dead.splice(this.dead.indexOf(id), 1)
|
||||
connection.status = Connection.statuses.ALIVE
|
||||
this.emit('resurrect', null, { strategy: 'optimistic', isAlive: true, connection })
|
||||
// eslint-disable-next-line standard/no-callback-literal
|
||||
callback(true, connection)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user