mirror of
https://github.com/openstf/stf
synced 2025-10-04 10:19:30 +02:00
The srv resolver should be easier to use now.
This commit is contained in:
parent
23621d0bdd
commit
f0aae7d4e5
2 changed files with 30 additions and 11 deletions
|
@ -13,13 +13,12 @@ function connect() {
|
||||||
var options = {
|
var options = {
|
||||||
// These environment variables are exposed when we --link to a
|
// These environment variables are exposed when we --link to a
|
||||||
// RethinkDB container.
|
// RethinkDB container.
|
||||||
host: process.env.RETHINKDB_PORT_28015_TCP_ADDR || '127.0.0.1'
|
url: process.env.RETHINKDB_PORT_28015_TCP || 'tcp://127.0.0.1:28015'
|
||||||
, port: process.env.RETHINKDB_PORT_28015_TCP_PORT || 28015
|
|
||||||
, db: process.env.RETHINKDB_ENV_DATABASE || 'stf'
|
, db: process.env.RETHINKDB_ENV_DATABASE || 'stf'
|
||||||
, authKey: process.env.RETHINKDB_ENV_AUTHKEY
|
, authKey: process.env.RETHINKDB_ENV_AUTHKEY
|
||||||
}
|
}
|
||||||
|
|
||||||
return srv.resolve(options.host, options.port)
|
return srv.resolve(options.url)
|
||||||
.then(function(records) {
|
.then(function(records) {
|
||||||
function next() {
|
function next() {
|
||||||
var record = records.shift()
|
var record = records.shift()
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
|
var url = require('url')
|
||||||
|
var util = require('util')
|
||||||
|
|
||||||
var Promise = require('bluebird')
|
var Promise = require('bluebird')
|
||||||
var dns = Promise.promisifyAll(require('dns'))
|
var dns = Promise.promisifyAll(require('dns'))
|
||||||
|
|
||||||
|
@ -58,22 +61,39 @@ function flatten(groupedRecords) {
|
||||||
}, [])
|
}, [])
|
||||||
}
|
}
|
||||||
|
|
||||||
var RE_SRV = /^srv:(.*)$/
|
|
||||||
|
|
||||||
module.exports.sort = function(records) {
|
module.exports.sort = function(records) {
|
||||||
return flatten(groupByPriority(records).map(shuffleWeighted))
|
return flatten(groupByPriority(records).map(shuffleWeighted))
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports.resolve = function(domain, defaultPort) {
|
module.exports.resolve = function(domain) {
|
||||||
var match
|
var parsedUrl = url.parse(domain)
|
||||||
if ((match = RE_SRV.exec(domain))) {
|
|
||||||
return dns.resolveSrvAsync(match[1])
|
if (!parsedUrl.protocol) {
|
||||||
|
return Promise.reject(new Error(
|
||||||
|
'Must include protocol in "%s"'
|
||||||
|
, domain
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
if (/^srv\+/.test(parsedUrl.protocol)) {
|
||||||
|
parsedUrl.protocol = parsedUrl.protocol.substr(4)
|
||||||
|
return dns.resolveSrvAsync(parsedUrl.hostname)
|
||||||
.then(module.exports.sort)
|
.then(module.exports.sort)
|
||||||
|
.then(function(records) {
|
||||||
|
return records.map(function(record) {
|
||||||
|
parsedUrl.host = util.format('%s:%d', record.name, record.port)
|
||||||
|
parsedUrl.hostname = record.name
|
||||||
|
parsedUrl.port = record.port
|
||||||
|
record.url = url.format(parsedUrl)
|
||||||
|
return record
|
||||||
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return Promise.resolve([{
|
return Promise.resolve([{
|
||||||
name: domain
|
url: domain
|
||||||
, port: defaultPort
|
, name: parsedUrl.hostname
|
||||||
|
, port: parsedUrl.port
|
||||||
}])
|
}])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue