WIP: initial prototype

- Expose connection info inside events
- Fixed minor bugs
This commit is contained in:
delvedor
2018-11-19 11:33:40 +01:00
parent d259b82763
commit 020165168c
4 changed files with 27 additions and 20 deletions

View File

@ -80,8 +80,7 @@ class Transport {
// handles request timeout
params.timeout = toMs(params.requestTimeout || this.requestTimeout)
// TODO: expose nicely the node metadata (also in response an error)
this.emit('request', params, connection)
this.emit('request', connection, params)
// perform the actual http request
const request = connection.request(params, (err, response) => {
@ -105,7 +104,7 @@ class Transport {
? err
: new ConnectionError(err.message, params)
this.emit('error', error, params)
this.emit('error', error, connection, params)
return callback(error, result)
}
@ -119,6 +118,7 @@ class Transport {
if (params.asStream === true) {
result.body = response
this.emit('response', connection, params, result)
callback(null, result)
return
}
@ -142,7 +142,7 @@ class Transport {
try {
result.body = this.serializer.deserialize(payload)
} catch (err) {
this.emit('error', err, params)
this.emit('error', err, connection, params)
return callback(err, result)
}
} else {
@ -171,7 +171,7 @@ class Transport {
this.connectionPool.markAlive(connection)
}
this.emit('response', params, result)
this.emit('response', connection, params, result)
if (ignoreStatusCode === false && statusCode >= 400) {
callback(new ResponseError(result), result)
} else {
@ -206,23 +206,25 @@ class Transport {
this._isSniffing = true
debug('Started sniffing request')
this.request({
const request = {
method: 'GET',
path: this.sniffEndpoint
}, (err, body) => {
}
this.request(request, (err, result) => {
this._isSniffing = false
if (this._sniffEnabled === true) {
this._nextSniff = Date.now() + this.sniffInterval
}
if (err != null) {
this.emit('error', err)
this.emit('error', err, null, request)
debug('Sniffing errored', err)
return callback(err)
}
debug('Sniffing ended successfully', body)
const hosts = this.connectionPool.nodesToHost(body.nodes)
debug('Sniffing ended successfully', result.body)
const hosts = this.connectionPool.nodesToHost(result.body.nodes)
this.connectionPool.update(hosts)
callback(null, hosts)