Support for publish_address as hostname/ip:port (#804)

This commit is contained in:
Tomas Della Vedova
2019-04-08 17:13:44 +02:00
committed by delvedor
parent 77fcca871f
commit 02c656c364
6 changed files with 159 additions and 37 deletions

View File

@ -127,7 +127,7 @@ export default class ConnectionPool {
* @param {object} nodes
* @returns {array} hosts
*/
nodesToHost(nodes: any): any[];
nodesToHost(nodes: any, protocol: string): any[];
/**
* Transforms an url string to a host object
*

View File

@ -332,18 +332,33 @@ class ConnectionPool {
* @param {object} nodes
* @returns {array} hosts
*/
nodesToHost (nodes) {
nodesToHost (nodes, protocol) {
const ids = Object.keys(nodes)
const hosts = []
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
// the `publish_address` new URL will throw
// the publish_address can have two forms:
// - ip:port
// - hostname/ip:port
// if we encounter the second case, we should
// use the hostname instead of the ip
var address = node.http.publish_address
const hostAndIpRegex = /^[a-z0-9_.-]*\/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/gi
const match = address.match(hostAndIpRegex)
if (match !== null) {
const ipRegex = /\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/
const ip = address.match(ipRegex)[0]
// extract the hostname, the -1 at the end removes the final /
const hostname = address.slice(0, address.indexOf(ip) - 1)
const port = address.split(':')[1]
address = `${hostname}:${port}`
}
address = address.slice(0, 4) === 'http'
? address
: 'http://' + address
: `${protocol}//${address}`
const roles = node.roles.reduce((acc, role) => {
acc[role] = true
return acc

View File

@ -353,7 +353,8 @@ class Transport {
}
debug('Sniffing ended successfully', result.body)
const hosts = this.connectionPool.nodesToHost(result.body.nodes)
const protocol = result.meta.connection.url.protocol || 'http:'
const hosts = this.connectionPool.nodesToHost(result.body.nodes, protocol)
this.connectionPool.update(hosts)
result.meta.sniff = { hosts, reason }