This repository has been archived by the owner on Sep 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathdiscover-providers.js
179 lines (158 loc) · 5.97 KB
/
discover-providers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
'use strict';
var _ = require('underscore');
var Backoff = require('./lib/backoff');
/**
* A DiscoverProvider provides a list of peers for a ringpop.
* @typedef {function} DiscoverProvider
* @param {DiscoverProvider~callback} callback - the callback when peers are discover.
*/
/**
* This callback is used from a DiscoverProvider to async provide the hosts.
* @callback DiscoverProvider~callback
* @param {object} err Indicates an error during discovery when not null
* @param {string[]} hosts The list with hosts
*/
/**
* Create a new DiscoverProvider using a static host list.
* @param {string[]} hosts The array of hosts.
* @returns {DiscoverProvider}
*/
function createStaticHostsProvider(hosts) {
return function staticHostsProvider(callback) {
callback(null, hosts);
};
}
/**
* Create a new DiscoverProvider using a host-file.
* @param {string} hostsFile The path to a json file containing an array of strings.
* @returns {DiscoverProvider}
*/
function createJsonFileDiscoverProvider(hostsFile) {
var fs = require('fs');
return function jsonFileProvider(callback) {
fs.readFile(hostsFile, function onFileRead(err, data) {
if (err) {
callback(err);
return;
}
var hosts;
try {
hosts = JSON.parse(data.toString());
} catch (e) {
callback(e);
return;
}
callback(null, hosts);
return;
});
};
}
/**
* Wrap a innerDiscoverProvider with a retrying mechanism.
*
* @param {Object} opts the options used as a Backoff policy {@see Backoff}
* @param {Object} [opts.backoff] a mocked backoff for testing
* @param {DiscoverProvider} innerDiscoverProvider The discover provider to wrap
*
* @returns {DiscoverProvider} The retrying discover provider.
*/
function retryDiscoverProvider(opts, innerDiscoverProvider) {
if (!opts) {
return innerDiscoverProvider;
}
return function retryingDiscoverProvider(callback) {
var policy;
var defaultPolicy = {
minDelay: 500,
maxDelay: 15000,
timeout: 60000
};
if (opts === true) {
policy = defaultPolicy;
} else {
policy = _.defaults({}, opts, defaultPolicy);
}
var backoff = opts.backoff || new Backoff(policy);
var lastError = null;
attempt(null);
function attempt(fail) {
if (fail) {
fail.lastError = lastError;
return setImmediate(callback, fail);
}
innerDiscoverProvider(function onTry(err, hosts) {
// We got results!
if (!err && hosts !== null && hosts.length > 0) {
return setImmediate(callback, null, hosts);
}
lastError = err;
backoff.retry(attempt);
});
}
};
}
/**
* Creates a DiscoverProvider from the provided options.
*
* @param opts: an object configuring the DiscoverProvider:
* - if opts or opts.discoverProvider is a function, it's returned as the {DiscoverProvider}.
* - if opts or opts.hosts is an array, a static hosts discover provider is returned (see {createStaticHostsProvider})
* - if opts or opts.bootstrapFile is a string, a json file discover provider is returned (see {createJsonFileDiscoverProvider})
* - if opts.retry is set, the discover provider is wrapped using retryDiscoverProvider (see {retryDiscoverProvider}).
*
* @returns {DiscoverProvider} The discover provider.
*/
function createFromOpts(opts) {
opts = opts || {};
if (typeof opts === 'function') {
return opts;
}
if (typeof opts === 'string') {
return createJsonFileDiscoverProvider(opts);
}
if (Array.isArray(opts)) {
return createStaticHostsProvider(opts);
}
var discoverProvider;
if (opts.discoverProvider) {
discoverProvider = opts.discoverProvider;
} else if (opts.hosts) {
discoverProvider = createStaticHostsProvider(opts.hosts);
} else if (opts.bootstrapFile && typeof opts.bootstrapFile === 'string') {
discoverProvider = createJsonFileDiscoverProvider(opts.bootstrapFile);
} else if (opts.bootstrapFile && Array.isArray(opts.bootstrapFile)) {
/* This weird case is added for backwards compatibility :( */
discoverProvider = createStaticHostsProvider(opts.bootstrapFile);
} else {
return null;
}
if (opts.retry) {
discoverProvider = retryDiscoverProvider(opts.retry, discoverProvider);
}
return discoverProvider;
}
module.exports = {
createFromOpts: createFromOpts,
createStaticHostsProvider: createStaticHostsProvider,
createJsonFileDiscoverProvider: createJsonFileDiscoverProvider,
retryDiscoverProvider: retryDiscoverProvider
};