Updated Connection Pool
- Merge nodes with same url but different id during update - Cache auth data if provided when adding a connection
This commit is contained in:
@ -11,6 +11,7 @@ class ConnectionPool {
|
||||
this.connections = new Map()
|
||||
this.dead = []
|
||||
this.selector = opts.selector
|
||||
this._auth = null
|
||||
this._ssl = opts.ssl
|
||||
this._agent = opts.agent
|
||||
// the resurrect timeout is 60s
|
||||
@ -200,10 +201,26 @@ class ConnectionPool {
|
||||
if (typeof opts === 'string') {
|
||||
opts = this.urlToHost(opts)
|
||||
}
|
||||
// if a given node has auth data we store it in the connection pool,
|
||||
// so if we add new nodes without auth data (after a sniff for example)
|
||||
// we can add it to them once the connection instance has been created
|
||||
if (opts.url.username !== '' && opts.url.password !== '') {
|
||||
this._auth = {
|
||||
username: opts.url.username,
|
||||
password: opts.url.password
|
||||
}
|
||||
}
|
||||
if (opts.ssl == null) opts.ssl = this._ssl
|
||||
if (opts.agent == null) opts.agent = this._agent
|
||||
|
||||
const connection = new this.Connection(opts)
|
||||
if (connection.url.username === '' &&
|
||||
connection.url.password === '' &&
|
||||
this._auth != null) {
|
||||
connection.url.username = this._auth.username
|
||||
connection.url.password = this._auth.password
|
||||
}
|
||||
|
||||
debug('Adding a new connection', connection)
|
||||
if (this.connections.has(connection.id)) {
|
||||
throw new Error(`Connection with id '${connection.id}' is already present`)
|
||||
@ -267,6 +284,18 @@ class ConnectionPool {
|
||||
if (oldConnection.status === Connection.statuses.DEAD) {
|
||||
this.markAlive(oldConnection)
|
||||
}
|
||||
// in case the user has passed a single url (or an array of urls),
|
||||
// the connection id will be the full href; to avoid closing valid connections
|
||||
// because are not present in the pool, we check also the node url,
|
||||
// and if is already present we update its id with the ES provided one.
|
||||
} else if (this.connections.has(connection.url.href) === true) {
|
||||
const oldConnection = this.connections.get(connection.url.href)
|
||||
this.connections.delete(connection.url.href)
|
||||
oldConnection.id = connection.id
|
||||
this.connections.set(connection.id, oldConnection)
|
||||
if (oldConnection.status === Connection.statuses.DEAD) {
|
||||
this.markAlive(oldConnection)
|
||||
}
|
||||
} else {
|
||||
this.addConnection(connection)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user