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,31 +1,47 @@
'use strict'
const assert = require('assert')
const debug = require('debug')('elasticsearch')
const { Agent: HttpAgent } = require('http')
const { Agent: HttpsAgent } = require('https')
const { resolve } = require('url')
const debug = require('debug')('elasticsearch')
const makeRequest = require('simple-get')
class Connection {
constructor (opts = {}) {
assert(opts.url, 'Missing url')
assert(opts.host, 'Missing host data')
this.url = opts.url
this.id = opts.id || opts.url
this.host = opts.host
this.ssl = opts.host.ssl || opts.ssl || null
this.id = opts.id || opts.host.href
this.deadCount = 0
this.resurrectTimeout = 0
this._status = opts.status || Connection.statuses.ALIVE
this.roles = opts.roles || defaultRoles
const agentOptions = Object.assign({}, {
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: Infinity,
maxFreeSockets: 256,
timeout: 60000
}, opts.host.agent || opts.agent)
this._agent = this.host.protocol === 'http:'
? new HttpAgent(agentOptions)
: new HttpsAgent(Object.assign({}, agentOptions, this.ssl))
}
request (params, callback) {
params.url = resolve(this.url, params.path)
params.url = resolve(this.host.href, params.path)
params.agent = this._agent
debug('Starting a new request', params)
return makeRequest(params, callback)
}
close () {
debug('Closing connection')
debug('Closing connection', this.id)
this._agent.destroy()
}
setRole (role, enabled) {