1
0
Fork 0
mirror of https://github.com/openstf/stf synced 2025-10-04 02:09:32 +02:00

Make all zmq endpoints use srv records.

This commit is contained in:
Simo Kinnunen 2014-12-17 21:01:20 +09:00
parent f0aae7d4e5
commit e43ae34d4b
8 changed files with 118 additions and 32 deletions

View file

@ -4,6 +4,8 @@ var util = require('util')
var Promise = require('bluebird')
var dns = Promise.promisifyAll(require('dns'))
var srv = module.exports = Object.create(null)
function groupByPriority(records) {
function sortByPriority(a, b) {
return a.priority - b.priority
@ -61,11 +63,21 @@ function flatten(groupedRecords) {
}, [])
}
module.exports.sort = function(records) {
function NEXT() {
Error.call(this)
this.name = 'NEXT'
Error.captureStackTrace(this, NEXT)
}
util.inherits(NEXT, Error)
srv.NEXT = NEXT
srv.sort = function(records) {
return flatten(groupByPriority(records).map(shuffleWeighted))
}
module.exports.resolve = function(domain) {
srv.resolve = function(domain) {
var parsedUrl = url.parse(domain)
if (!parsedUrl.protocol) {
@ -97,3 +109,17 @@ module.exports.resolve = function(domain) {
}])
}
}
srv.attempt = function(records, fn) {
function next(i) {
if (i >= records.length) {
throw new Error('No more records left to try')
}
return fn(records[i]).catch(srv.NEXT, function() {
return next(++i)
})
}
return next(0)
}