forked from alertlogic/al-collector-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathal_util.js
138 lines (128 loc) · 3.8 KB
/
al_util.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
/* -----------------------------------------------------------------------------
* @copyright (C) 2017, Alert Logic, Inc
* @doc
*
* Helper utilities for Alert Logic collectors.
*
* @end
* -----------------------------------------------------------------------------
*/
const rp = require('request-promise-native');
const retry = require('retry');
let MAX_CONNS_PER_SERVICE = 128;
/**
* @default Refer to https://www.npmjs.com/package/retry
*/
let DEFAULT_RETRY = {
// Default values
// randomize: false
factor: 7,
minTimeout: 300,
retries: 2,
maxTimeout: 10000
};
/**
* @function Default retry callback.
* It doesn't retry 2XX, 3XX and 4XX.
* Keeps retrying 5XX HTTP responses and any system level errors
**/
var defaultRetryCb = function(err){
if (err &&
(err.statusCode >= 500 ||
(err.error && err.error.errno))) {
return true;
} else {
return false;
}
};
/**
* @class
* Rest client.
*
* @constructor
* @param {string} endpoint - hostname/address to sent HTTPS
* requests to.
*
*/
class RestServiceClient {
constructor(endpoint, retryOptions) {
'use strict';
this._host = endpoint;
this._url = 'https://' + endpoint;
this._pool = {
maxSockets: MAX_CONNS_PER_SERVICE
};
if (retryOptions) {
this._retryCb = retryOptions.retryCb ? retryOptions.retryCb : defaultRetryCb;
delete retryOptions.retryCb;
this._retryOptions = retryOptions ? retryOptions : DEFAULT_RETRY;
} else {
this._retryCb = defaultRetryCb;
this._retryOptions = DEFAULT_RETRY;
}
}
_initRequestOptions(method, path, extra) {
'use strict';
const defaultOptions = {
method: method,
url: this._url + path,
json: true,
headers: {},
pool: this._pool
};
const options = Object.assign({}, defaultOptions, extra);
const defaultHeaders = {
'Accept': 'application/json'
};
Object.assign(options.headers,
defaultHeaders,
extra.headers ? extra.headers : {});
return options;
}
request(method, path, extraOptions) {
'use strict';
const options = this._initRequestOptions(method, path, extraOptions);
const retryOptions = this._retryOptions;
var retryCb = this._retryCb;
var operation = retry.operation(retryOptions);
return new Promise(function(resolve, reject) {
operation.attempt(function(currentAttempt) {
rp(options)
.then( resp => {
// We need opertaion.retry here as we want to check if
// the maximum amount of retries has been reached
if (retryCb(resp) && operation.retry('retry')) {
return;
} else {
return resolve(resp);
}
})
.catch( err =>{
if (retryCb(err) && operation.retry(err)) {
return;
} else {
return reject(err);
}
});
});
});
}
post(path, extraOptions) {
return this.request('POST', path, extraOptions);
}
get(path, extraOptions) {
return this.request('GET', path, extraOptions);
}
deleteRequest(path, extraOptions) {
return this.request('DELETE', path, extraOptions);
}
get host() {
return this._host;
}
get url() {
return this._url;
}
}
module.exports = {
RestServiceClient: RestServiceClient
};