influx 5.0.3 → 5.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/lib/src/backoff/backoff.d.ts +15 -15
- package/lib/src/backoff/backoff.js +2 -1
- package/lib/src/backoff/exponential.d.ts +52 -52
- package/lib/src/backoff/exponential.js +40 -39
- package/lib/src/builder.d.ts +265 -265
- package/lib/src/builder.js +366 -365
- package/lib/src/doc.d.ts +315 -310
- package/lib/src/doc.js +340 -329
- package/lib/src/grammar/ds.d.ts +45 -45
- package/lib/src/grammar/ds.js +56 -55
- package/lib/src/grammar/escape.d.ts +25 -25
- package/lib/src/grammar/escape.js +108 -107
- package/lib/src/grammar/index.d.ts +3 -3
- package/lib/src/grammar/index.js +8 -7
- package/lib/src/grammar/times.d.ts +74 -74
- package/lib/src/grammar/times.js +325 -318
- package/lib/src/host.d.ts +26 -24
- package/lib/src/host.js +34 -32
- package/lib/src/index.d.ts +659 -653
- package/lib/src/index.js +870 -866
- package/lib/src/pool.d.ts +149 -148
- package/lib/src/pool.js +344 -333
- package/lib/src/results.d.ts +118 -119
- package/lib/src/results.js +146 -139
- package/lib/src/schema.d.ts +58 -58
- package/lib/src/schema.js +117 -116
- package/package.json +10 -10
package/lib/src/pool.js
CHANGED
|
@@ -1,333 +1,344 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __extends = (this && this.__extends) || function (
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
var
|
|
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
|
-
function
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
function
|
|
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
|
-
this.
|
|
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
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
*
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
this.hostsAvailable
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
this.
|
|
316
|
-
|
|
317
|
-
};
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
this
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = Object.setPrototypeOf ||
|
|
4
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
5
|
+
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
|
6
|
+
return function (d, b) {
|
|
7
|
+
extendStatics(d, b);
|
|
8
|
+
function __() { this.constructor = d; }
|
|
9
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
10
|
+
};
|
|
11
|
+
})();
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
var exponential_1 = require("./backoff/exponential");
|
|
14
|
+
var host_1 = require("./host");
|
|
15
|
+
var http = require("http");
|
|
16
|
+
var https = require("https");
|
|
17
|
+
var querystring = require("querystring");
|
|
18
|
+
/**
|
|
19
|
+
* Status codes that will cause a host to be marked as 'failed' if we get
|
|
20
|
+
* them from a request to Influx.
|
|
21
|
+
* @type {Array}
|
|
22
|
+
*/
|
|
23
|
+
var resubmitErrorCodes = [
|
|
24
|
+
'ETIMEDOUT',
|
|
25
|
+
'ESOCKETTIMEDOUT',
|
|
26
|
+
'ECONNRESET',
|
|
27
|
+
'ECONNREFUSED',
|
|
28
|
+
'EHOSTUNREACH',
|
|
29
|
+
];
|
|
30
|
+
/**
|
|
31
|
+
* An ServiceNotAvailableError is returned as an error from requests that
|
|
32
|
+
* result in a > 500 error code.
|
|
33
|
+
*/
|
|
34
|
+
var ServiceNotAvailableError = (function (_super) {
|
|
35
|
+
__extends(ServiceNotAvailableError, _super);
|
|
36
|
+
function ServiceNotAvailableError(message) {
|
|
37
|
+
var _this = _super.call(this) || this;
|
|
38
|
+
_this.message = message;
|
|
39
|
+
Object.setPrototypeOf(_this, ServiceNotAvailableError.prototype);
|
|
40
|
+
return _this;
|
|
41
|
+
}
|
|
42
|
+
return ServiceNotAvailableError;
|
|
43
|
+
}(Error));
|
|
44
|
+
exports.ServiceNotAvailableError = ServiceNotAvailableError;
|
|
45
|
+
/**
|
|
46
|
+
* An RequestError is returned as an error from requests that
|
|
47
|
+
* result in a 300 <= error code <= 500.
|
|
48
|
+
*/
|
|
49
|
+
var RequestError = (function (_super) {
|
|
50
|
+
__extends(RequestError, _super);
|
|
51
|
+
function RequestError(req, res, body) {
|
|
52
|
+
var _this = _super.call(this) || this;
|
|
53
|
+
_this.req = req;
|
|
54
|
+
_this.res = res;
|
|
55
|
+
_this.message = "A " + res.statusCode + " " + res.statusMessage + " error occurred: " + body;
|
|
56
|
+
Object.setPrototypeOf(_this, RequestError.prototype);
|
|
57
|
+
return _this;
|
|
58
|
+
}
|
|
59
|
+
RequestError.Create = function (req, res, callback) {
|
|
60
|
+
var body = '';
|
|
61
|
+
res.on('data', function (str) { return body = body + str.toString(); });
|
|
62
|
+
res.on('end', function () { return callback(new RequestError(req, res, body)); });
|
|
63
|
+
};
|
|
64
|
+
return RequestError;
|
|
65
|
+
}(Error));
|
|
66
|
+
exports.RequestError = RequestError;
|
|
67
|
+
/**
|
|
68
|
+
* Creates a function generation that returns a wrapper which only allows
|
|
69
|
+
* through the first call of any function that it generated.
|
|
70
|
+
*/
|
|
71
|
+
function doOnce() {
|
|
72
|
+
var handled = false;
|
|
73
|
+
return function (fn) {
|
|
74
|
+
return function (arg) {
|
|
75
|
+
if (handled) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
handled = true;
|
|
79
|
+
fn(arg);
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
function setToArray(itemSet) {
|
|
84
|
+
var output = [];
|
|
85
|
+
itemSet.forEach(function (value) {
|
|
86
|
+
output.push(value);
|
|
87
|
+
});
|
|
88
|
+
return output;
|
|
89
|
+
}
|
|
90
|
+
var request = function (options, callback) {
|
|
91
|
+
if (options.protocol === 'https:') {
|
|
92
|
+
return https.request(options, callback);
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
return http.request(options, callback);
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
/**
|
|
99
|
+
*
|
|
100
|
+
* The Pool maintains a list available Influx hosts and dispatches requests
|
|
101
|
+
* to them. If there are errors connecting to hosts, it will disable that
|
|
102
|
+
* host for a period of time.
|
|
103
|
+
*/
|
|
104
|
+
var Pool = (function () {
|
|
105
|
+
/**
|
|
106
|
+
* Creates a new Pool instance.
|
|
107
|
+
* @param {IPoolOptions} options
|
|
108
|
+
*/
|
|
109
|
+
function Pool(options) {
|
|
110
|
+
this.options = Object.assign({
|
|
111
|
+
backoff: new exponential_1.ExponentialBackoff({
|
|
112
|
+
initial: 300,
|
|
113
|
+
max: 10 * 1000,
|
|
114
|
+
random: 1,
|
|
115
|
+
}),
|
|
116
|
+
maxRetries: 2,
|
|
117
|
+
requestTimeout: 30 * 1000,
|
|
118
|
+
}, options);
|
|
119
|
+
this.index = 0;
|
|
120
|
+
this.hostsAvailable = new Set();
|
|
121
|
+
this.hostsDisabled = new Set();
|
|
122
|
+
this.timeout = this.options.requestTimeout;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Returns a list of currently active hosts.
|
|
126
|
+
* @return {Host[]}
|
|
127
|
+
*/
|
|
128
|
+
Pool.prototype.getHostsAvailable = function () {
|
|
129
|
+
return setToArray(this.hostsAvailable);
|
|
130
|
+
};
|
|
131
|
+
/**
|
|
132
|
+
* Returns a list of hosts that are currently disabled due to network
|
|
133
|
+
* errors.
|
|
134
|
+
* @return {Host[]}
|
|
135
|
+
*/
|
|
136
|
+
Pool.prototype.getHostsDisabled = function () {
|
|
137
|
+
return setToArray(this.hostsDisabled);
|
|
138
|
+
};
|
|
139
|
+
/**
|
|
140
|
+
* Inserts a new host to the pool.
|
|
141
|
+
*/
|
|
142
|
+
Pool.prototype.addHost = function (url, options) {
|
|
143
|
+
if (options === void 0) { options = {}; }
|
|
144
|
+
var host = new host_1.Host(url, this.options.backoff.reset(), options);
|
|
145
|
+
this.hostsAvailable.add(host);
|
|
146
|
+
return host;
|
|
147
|
+
};
|
|
148
|
+
/**
|
|
149
|
+
* Returns true if there's any host available to by queried.
|
|
150
|
+
* @return {Boolean}
|
|
151
|
+
*/
|
|
152
|
+
Pool.prototype.hostIsAvailable = function () {
|
|
153
|
+
return this.hostsAvailable.size > 0;
|
|
154
|
+
};
|
|
155
|
+
/**
|
|
156
|
+
* Makes a request and calls back with the response, parsed as JSON.
|
|
157
|
+
* An error is returned on a non-2xx status code or on a parsing exception.
|
|
158
|
+
*/
|
|
159
|
+
Pool.prototype.json = function (options) {
|
|
160
|
+
return this.text(options).then(function (res) { return JSON.parse(res); });
|
|
161
|
+
};
|
|
162
|
+
/**
|
|
163
|
+
* Makes a request and resolves with the plain text response,
|
|
164
|
+
* if possible. An error is raised on a non-2xx status code.
|
|
165
|
+
*/
|
|
166
|
+
Pool.prototype.text = function (options) {
|
|
167
|
+
var _this = this;
|
|
168
|
+
return new Promise(function (resolve, reject) {
|
|
169
|
+
_this.stream(options, function (err, res) {
|
|
170
|
+
if (err) {
|
|
171
|
+
return reject(err);
|
|
172
|
+
}
|
|
173
|
+
var output = '';
|
|
174
|
+
res.on('data', function (str) { return output = output + str.toString(); });
|
|
175
|
+
res.on('end', function () { return resolve(output); });
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
};
|
|
179
|
+
/**
|
|
180
|
+
* Makes a request and discards any response body it receives.
|
|
181
|
+
* An error is returned on a non-2xx status code.
|
|
182
|
+
*/
|
|
183
|
+
Pool.prototype.discard = function (options) {
|
|
184
|
+
var _this = this;
|
|
185
|
+
return new Promise(function (resolve, reject) {
|
|
186
|
+
_this.stream(options, function (err, res) {
|
|
187
|
+
if (err) {
|
|
188
|
+
return reject(err);
|
|
189
|
+
}
|
|
190
|
+
res.on('data', function () { });
|
|
191
|
+
res.on('end', function () { return resolve(); });
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
};
|
|
195
|
+
/**
|
|
196
|
+
* Ping sends out a request to all available Influx servers, reporting on
|
|
197
|
+
* their response time and version number.
|
|
198
|
+
*/
|
|
199
|
+
Pool.prototype.ping = function (timeout, path) {
|
|
200
|
+
if (path === void 0) { path = '/ping'; }
|
|
201
|
+
var todo = [];
|
|
202
|
+
setToArray(this.hostsAvailable)
|
|
203
|
+
.concat(setToArray(this.hostsDisabled))
|
|
204
|
+
.forEach(function (host) {
|
|
205
|
+
var start = Date.now();
|
|
206
|
+
var url = host.url;
|
|
207
|
+
var once = doOnce();
|
|
208
|
+
return todo.push(new Promise(function (resolve) {
|
|
209
|
+
var req = request(Object.assign({
|
|
210
|
+
hostname: url.hostname,
|
|
211
|
+
method: 'GET',
|
|
212
|
+
path: path,
|
|
213
|
+
port: Number(url.port),
|
|
214
|
+
protocol: url.protocol,
|
|
215
|
+
timeout: timeout,
|
|
216
|
+
}, host.options), once(function (res) {
|
|
217
|
+
resolve({
|
|
218
|
+
url: url,
|
|
219
|
+
res: res,
|
|
220
|
+
online: res.statusCode < 300,
|
|
221
|
+
rtt: Date.now() - start,
|
|
222
|
+
version: res.headers['x-influxdb-version'],
|
|
223
|
+
});
|
|
224
|
+
}));
|
|
225
|
+
var fail = once(function () {
|
|
226
|
+
resolve({
|
|
227
|
+
online: false,
|
|
228
|
+
res: null,
|
|
229
|
+
rtt: Infinity,
|
|
230
|
+
url: url,
|
|
231
|
+
version: null,
|
|
232
|
+
});
|
|
233
|
+
});
|
|
234
|
+
// Support older Nodes and polyfills which don't allow .timeout() in
|
|
235
|
+
// the request options, wrapped in a conditional for even worse
|
|
236
|
+
// polyfills. See: https://github.com/node-influx/node-influx/issues/221
|
|
237
|
+
if (typeof req.setTimeout === 'function') {
|
|
238
|
+
req.setTimeout(timeout, fail); // tslint:disable-line
|
|
239
|
+
}
|
|
240
|
+
req.on('timeout', fail);
|
|
241
|
+
req.on('error', fail);
|
|
242
|
+
req.end();
|
|
243
|
+
}));
|
|
244
|
+
});
|
|
245
|
+
return Promise.all(todo);
|
|
246
|
+
};
|
|
247
|
+
/**
|
|
248
|
+
* Makes a request and calls back with the IncomingMessage stream,
|
|
249
|
+
* if possible. An error is returned on a non-2xx status code.
|
|
250
|
+
*/
|
|
251
|
+
Pool.prototype.stream = function (options, callback) {
|
|
252
|
+
var _this = this;
|
|
253
|
+
if (!this.hostIsAvailable()) {
|
|
254
|
+
return callback(new ServiceNotAvailableError('No host available'), null);
|
|
255
|
+
}
|
|
256
|
+
var path = options.path;
|
|
257
|
+
if (options.query) {
|
|
258
|
+
path += '?' + querystring.stringify(options.query);
|
|
259
|
+
}
|
|
260
|
+
var once = doOnce();
|
|
261
|
+
var host = this.getHost();
|
|
262
|
+
var req = request(Object.assign({
|
|
263
|
+
headers: { 'content-length': options.body ? new Buffer(options.body).length : 0 },
|
|
264
|
+
hostname: host.url.hostname,
|
|
265
|
+
method: options.method,
|
|
266
|
+
path: path,
|
|
267
|
+
port: Number(host.url.port),
|
|
268
|
+
protocol: host.url.protocol,
|
|
269
|
+
timeout: this.timeout,
|
|
270
|
+
}, host.options), once(function (res) {
|
|
271
|
+
if (res.statusCode >= 500) {
|
|
272
|
+
return _this.handleRequestError(new ServiceNotAvailableError(res.statusMessage), host, options, callback);
|
|
273
|
+
}
|
|
274
|
+
if (res.statusCode >= 300) {
|
|
275
|
+
return RequestError.Create(req, res, function (err) { return callback(err, res); });
|
|
276
|
+
}
|
|
277
|
+
host.success();
|
|
278
|
+
return callback(undefined, res);
|
|
279
|
+
}));
|
|
280
|
+
// Handle network or HTTP parsing errors:
|
|
281
|
+
req.on('error', once(function (err) {
|
|
282
|
+
_this.handleRequestError(err, host, options, callback);
|
|
283
|
+
}));
|
|
284
|
+
// Handle timeouts:
|
|
285
|
+
req.on('timeout', once(function () {
|
|
286
|
+
_this.handleRequestError(new ServiceNotAvailableError('Request timed out'), host, options, callback);
|
|
287
|
+
}));
|
|
288
|
+
// Support older Nodes and polyfills which don't allow .timeout() in the
|
|
289
|
+
// request options, wrapped in a conditional for even worse polyfills. See:
|
|
290
|
+
// https://github.com/node-influx/node-influx/issues/221
|
|
291
|
+
if (typeof req.setTimeout === 'function') {
|
|
292
|
+
req.setTimeout(this.timeout); // tslint:disable-line
|
|
293
|
+
}
|
|
294
|
+
// Write out the body:
|
|
295
|
+
if (options.body) {
|
|
296
|
+
req.write(options.body);
|
|
297
|
+
}
|
|
298
|
+
req.end();
|
|
299
|
+
};
|
|
300
|
+
/**
|
|
301
|
+
* Returns the next available host for querying.
|
|
302
|
+
* @return {Host}
|
|
303
|
+
*/
|
|
304
|
+
Pool.prototype.getHost = function () {
|
|
305
|
+
var available = setToArray(this.hostsAvailable);
|
|
306
|
+
var host = available[this.index];
|
|
307
|
+
this.index = (this.index + 1) % available.length;
|
|
308
|
+
return host;
|
|
309
|
+
};
|
|
310
|
+
/**
|
|
311
|
+
* Re-enables the provided host, returning it to the pool to query.
|
|
312
|
+
* @param {Host} host
|
|
313
|
+
*/
|
|
314
|
+
Pool.prototype.enableHost = function (host) {
|
|
315
|
+
this.hostsDisabled.delete(host);
|
|
316
|
+
this.hostsAvailable.add(host);
|
|
317
|
+
};
|
|
318
|
+
/**
|
|
319
|
+
* Disables the provided host, removing it from the query pool. It will be
|
|
320
|
+
* re-enabled after a backoff interval
|
|
321
|
+
*/
|
|
322
|
+
Pool.prototype.disableHost = function (host) {
|
|
323
|
+
var _this = this;
|
|
324
|
+
this.hostsAvailable.delete(host);
|
|
325
|
+
this.hostsDisabled.add(host);
|
|
326
|
+
this.index %= Math.max(1, this.hostsAvailable.size);
|
|
327
|
+
setTimeout(function () { return _this.enableHost(host); }, host.fail());
|
|
328
|
+
};
|
|
329
|
+
Pool.prototype.handleRequestError = function (err, host, options, callback) {
|
|
330
|
+
if (!(err instanceof ServiceNotAvailableError) &&
|
|
331
|
+
resubmitErrorCodes.indexOf(err.code) === -1) {
|
|
332
|
+
return callback(err, null);
|
|
333
|
+
}
|
|
334
|
+
this.disableHost(host);
|
|
335
|
+
var retries = options.retries || 0;
|
|
336
|
+
if (retries < this.options.maxRetries && this.hostIsAvailable()) {
|
|
337
|
+
options.retries = retries + 1;
|
|
338
|
+
return this.stream(options, callback);
|
|
339
|
+
}
|
|
340
|
+
callback(err, null);
|
|
341
|
+
};
|
|
342
|
+
return Pool;
|
|
343
|
+
}());
|
|
344
|
+
exports.Pool = Pool;
|