This repository has been archived by the owner on Jan 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 112
/
request.js
174 lines (163 loc) · 5.35 KB
/
request.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
const http = require('http');
const https = require('https');
const url = require('url');
const log = require('./log');
/**
* @typedef {Object} ServiceEndpoint
* @property {boolean} useSSL
* @property {string} host
* @property {number} port
* @property {string} auth
* @property {string} httpProxy
* @property {number} cpuLimit
*/
/**
*
* @param {ServiceEndpoint} service
* @param {string}path
* @param {string} method
* @param {Object|string} data
* @param {dataCb} callback
*/
exports.create = (service, path, method, data, callback) => {
let protocol = service.useSSL ? https : http;
let buffer = null, err = null;
let reqOpts = { host: service.host, port: service.port, path, auth: service.auth, headers: {}, method };
if (service.httpProxy) {
let httpUrl = url.parse(service.httpProxy);
reqOpts.host = httpUrl.hostname;
reqOpts.port = httpUrl.port;
reqOpts.path = 'http://' + service.host + ':' + service.port + path;
reqOpts.headers.Host = service.host;
}
if (data) {
if (typeof data == 'object') {
data = JSON.stringify(data);
}
buffer = new Buffer(data, 'utf8');
reqOpts.headers['Content-Length'] = buffer.length;
}
let req = protocol.request(reqOpts, res => {
let buffers = [];
let nread = 0;
res.on('data', chunk => {
buffers.push(chunk);
nread += chunk.length;
});
res.on('end', () => {
if (!err) {
if (!buffers.length) {
return callback(null, '');
}
let data;
try {
data = JSON.parse(Buffer.concat(buffers).toString());
} catch(e) {
log.debug('Unable to parse json response from server', e);
return callback("There was an error trying to parse a json response from the server.");
}
return callback(null, data);
}
});
});
req.on('error', e => {
err = true;
// TODO pretty print errors, such as "can't connect"
switch (e.code) {
case 'ECONNREFUSED':
callback('Unable to connect to host ' + service.host + ' on port ' + service.port);
break;
default: callback(e);
}
});
req.end(buffer);
};
/**
*
* @param {Environment} env
* @param {ServiceEndpoint} service
* @param {errorCb} callback
* @param {number} [timeout=0] Numberr of seconds to wait before trying again (set automatically)
* @returns {*}
*/
exports.wait = (env, service, callback, timeout = 0) => {
if (service.cpuLimit>=100) {
return callback();
}
timeout = Math.min(timeout + 1, 30);
exports.create(service, '/_nodes/stats/process', 'GET', null, (err, nodesData) => {
if (err) {
return callback(err);
}
for (let nodeName in nodesData.nodes) {
let nodeCpu = nodesData.nodes[nodeName].process.cpu.percent;
if (nodeCpu > service.cpuLimit) {
let destination = (service.host == env.options.source.host) ? 'source' : 'target';
log.status('Waiting %s seconds for %s cpu to cool down. Current load is %s%%', timeout, destination, nodeCpu);
return setTimeout(exports.wait, timeout * 1000, env, service, callback, timeout);
}
}
callback();
});
};
exports.source = {
/**
* @param {Environment} env
* @param {string} path
* @param {*} data
* @param {dataCb} callback
*/
get: (env, path, data, callback) => {
if (typeof data == 'function') {
callback = data;
data = null;
}
exports.wait(env, env.options.source, err => err ? callback(err) : exports.create(env.options.source, path, 'GET', data, callback));
},
/**
* @param {Environment} env
* @param {string} path
* @param {*} data
* @param {dataCb} callback
*/
post: (env, path, data, callback) => {
exports.wait(env, env.options.source, err => err ? callback(err) : exports.create(env.options.source, path, 'POST', data, callback));
}
};
exports.target = {
/**
* @param {Environment} env
* @param {string} path
* @param {*} data
* @param {dataCb} callback
*/
get: (env, path, data, callback) => {
if (typeof data == 'function') {
callback = data;
data = null;
}
exports.wait(env, env.options.target, err => err ? callback(err) : exports.create(env.options.target, path, 'GET', data, callback));
},
/**
* @param {Environment} env
* @param {string} path
* @param {*} data
* @param {dataCb} callback
*/
post: (env, path, data, callback) => {
exports.wait(env, env.options.target, err => err ? callback(err) : exports.create(env.options.target, path, 'POST', data, callback));
},
/**
* @param {Environment} env
* @param {string} path
* @param {*} data
* @param {dataCb} callback
*/
put: (env, path, data, callback) => {
if (typeof data == 'function') {
callback = data;
data = null;
}
exports.wait(env, env.options.target, err => err ? callback(err) : exports.create(env.options.target, path, 'PUT', data, callback));
}
};