WIP: initial prototype

- Added keep-alive Agent
- Added support for HTTPS
- Added log events
This commit is contained in:
delvedor
2018-10-19 15:04:07 +02:00
parent d0ac6b1f52
commit 68cca6069b
4 changed files with 74 additions and 29 deletions

View File

@ -1,5 +1,6 @@
'use strict'
const { URL } = require('url')
const debug = require('debug')('elasticsearch')
const Connection = require('./Connection')
@ -9,6 +10,7 @@ class ConnectionPool {
this.alive = []
this.dead = []
this.selector = opts.selector
this.sll = opts.sll
// the resurrect timeout is 60s
// we multiply it by 2 because the resurrect formula is
// `Math.pow(resurrectTimeout * 2, deadCount -1)`
@ -132,16 +134,17 @@ class ConnectionPool {
* @param {object|string} host
* @returns {ConnectionPool}
*/
addConnection (host) {
if (Array.isArray(host)) {
host.forEach(h => this.addConnection(h))
addConnection (opts) {
if (Array.isArray(opts)) {
opts.forEach(o => this.addConnection(o))
return
}
if (typeof host === 'string') {
host = this.urlToHost(host)
if (typeof opts === 'string') {
opts = this.urlToHost(opts)
}
const connection = new Connection(host)
Object.assign(opts, this.ssl)
const connection = new Connection(opts)
debug('Adding a new connection', connection)
this.connections.set(connection.id, connection)
this.alive.push(connection.id)
@ -156,6 +159,7 @@ class ConnectionPool {
*/
removeConnection (connection) {
debug('Removing connection', connection)
connection.close()
const { id } = connection
this.connections.delete(id)
var index = this.dead.indexOf(id)
@ -193,8 +197,14 @@ class ConnectionPool {
for (var i = 0, len = ids.length; i < len; i++) {
const node = nodes[ids[i]]
// If there is no protocol in
// the `publish_address` new URL wil throw
var address = node.http.publish_address
address = address.slice(0, 4) === 'http'
? address
: 'http://' + address
hosts.push({
url: node.http.publish_address,
host: new URL(address),
id: ids[i],
roles: node.roles.reduce((acc, role) => {
acc[role] = true
@ -214,8 +224,7 @@ class ConnectionPool {
*/
urlToHost (url) {
return {
id: url,
url
host: new URL(url)
}
}
}