Refactored connection pool (#913)

* Refactored ConnectionPool
- Created BaseConnectionPool class
- Created CloudConnectionPool
- connection pool updates are immutable
- resurrect now happens inside getConnection()

* Rewritten connection pool(s) type definitions

* Updated test

* Fixed test

* Fix if check

* Removed old files

* Improve code coverage

* Updated license header

* Fix if check

* Improve code coverage

* Updated coverage script
This commit is contained in:
Tomas Della Vedova
2019-07-26 11:43:48 +02:00
committed by delvedor
parent 90be646658
commit 8e86450aeb
19 changed files with 1305 additions and 620 deletions

View File

@ -7,6 +7,7 @@
const { test } = require('tap')
const { URL } = require('url')
const { Client, ConnectionPool, Transport } = require('../../index')
const { CloudConnectionPool } = require('../../lib/pool')
const { buildServer } = require('../utils')
test('Configure host', t => {
@ -15,7 +16,7 @@ test('Configure host', t => {
node: 'http://localhost:9200'
})
const pool = client.connectionPool
t.match(pool.connections.get('http://localhost:9200/'), {
t.match(pool.connections.find(c => c.id === 'http://localhost:9200/'), {
url: new URL('http://localhost:9200'),
id: 'http://localhost:9200/',
ssl: null,
@ -36,7 +37,7 @@ test('Configure host', t => {
nodes: ['http://localhost:9200', 'http://localhost:9201']
})
const pool = client.connectionPool
t.match(pool.connections.get('http://localhost:9200/'), {
t.match(pool.connections.find(c => c.id === 'http://localhost:9200/'), {
url: new URL('http://localhost:9200'),
id: 'http://localhost:9200/',
ssl: null,
@ -49,7 +50,7 @@ test('Configure host', t => {
ml: false
}
})
t.match(pool.connections.get('http://localhost:9201/'), {
t.match(pool.connections.find(c => c.id === 'http://localhost:9201/'), {
url: new URL('http://localhost:9201'),
id: 'http://localhost:9201/',
ssl: null,
@ -80,7 +81,7 @@ test('Configure host', t => {
}
})
const pool = client.connectionPool
t.match(pool.connections.get('node'), {
t.match(pool.connections.find(c => c.id === 'node'), {
url: new URL('http://localhost:9200'),
id: 'node',
ssl: 'ssl',
@ -88,7 +89,7 @@ test('Configure host', t => {
resurrectTimeout: 0
})
t.deepEqual(pool.connections.get('node').roles, {
t.deepEqual(pool.connections.find(c => c.id === 'node').roles, {
master: true,
data: false,
ingest: false,
@ -121,7 +122,7 @@ test('Configure host', t => {
}]
})
const pool = client.connectionPool
t.match(pool.connections.get('node1'), {
t.match(pool.connections.find(c => c.id === 'node1'), {
url: new URL('http://localhost:9200'),
id: 'node1',
ssl: 'ssl',
@ -129,14 +130,14 @@ test('Configure host', t => {
resurrectTimeout: 0
})
t.deepEqual(pool.connections.get('node1').roles, {
t.deepEqual(pool.connections.find(c => c.id === 'node1').roles, {
master: true,
data: false,
ingest: false,
ml: false
})
t.match(pool.connections.get('node2'), {
t.match(pool.connections.find(c => c.id === 'node2'), {
url: new URL('http://localhost:9200'),
id: 'node2',
ssl: 'ssl',
@ -144,7 +145,7 @@ test('Configure host', t => {
resurrectTimeout: 0
})
t.deepEqual(pool.connections.get('node2').roles, {
t.deepEqual(pool.connections.find(c => c.id === 'node2').roles, {
master: false,
data: true,
ingest: false,
@ -163,7 +164,7 @@ test('Configure host', t => {
}
})
const pool = client.connectionPool
t.match(pool.connections.get('node'), {
t.match(pool.connections.find(c => c.id === 'node'), {
url: new URL('http://localhost:9200'),
headers: { 'x-foo': 'bar' }
})
@ -755,7 +756,7 @@ test('Extend client APIs', t => {
test('Elastic cloud config', t => {
t.test('Basic', t => {
t.plan(4)
t.plan(5)
const client = new Client({
cloud: {
// 'localhost$abcd$efgh'
@ -766,7 +767,8 @@ test('Elastic cloud config', t => {
})
const pool = client.connectionPool
t.match(pool.connections.get('https://abcd.localhost/'), {
t.ok(pool instanceof CloudConnectionPool)
t.match(pool.connections.find(c => c.id === 'https://abcd.localhost/'), {
url: new URL('https://elastic:changeme@abcd.localhost'),
id: 'https://abcd.localhost/',
headers: {
@ -789,7 +791,7 @@ test('Elastic cloud config', t => {
})
t.test('Auth as separate option', t => {
t.plan(4)
t.plan(5)
const client = new Client({
cloud: {
// 'localhost$abcd$efgh'
@ -802,7 +804,8 @@ test('Elastic cloud config', t => {
})
const pool = client.connectionPool
t.match(pool.connections.get('https://abcd.localhost/'), {
t.ok(pool instanceof CloudConnectionPool)
t.match(pool.connections.find(c => c.id === 'https://abcd.localhost/'), {
url: new URL('https://elastic:changeme@abcd.localhost'),
id: 'https://abcd.localhost/',
headers: {
@ -825,7 +828,7 @@ test('Elastic cloud config', t => {
})
t.test('Override default options', t => {
t.plan(3)
t.plan(4)
const client = new Client({
cloud: {
// 'localhost$abcd$efgh'
@ -840,6 +843,7 @@ test('Elastic cloud config', t => {
}
})
t.ok(client.connectionPool instanceof CloudConnectionPool)
t.strictEqual(client.transport.compression, false)
t.strictEqual(client.transport.suggestCompression, false)
t.deepEqual(client.connectionPool._ssl, { secureProtocol: 'TLSv1_1_method' })