Skip to content

Commit 11ec675

Browse files
authored
elasticio/elasticio#6359 https.globalAgent enable keepAlive (#194)
* elasticio/elasticio#6359 https.globalAgent enable keepAlive * elasticio/elasticio#6359 test keep alive is set
1 parent 16f5250 commit 11ec675

File tree

6 files changed

+104
-81
lines changed

6 files changed

+104
-81
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.6.29 (July 14, 2022)
2+
3+
* Enabled keep-alive for global HTTPS agent ([#6359](https://github.com/elasticio/elasticio/issues/6359))
4+
15
## 2.6.28 (June 21, 2022)
26

37
* Fix: "sailor-nodejs ignores errors from maester during lightweight message upload" [#6233](https://github.com/elasticio/elasticio/issues/6233))

mocha_spec/integration_component/component.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
"main": "./triggers/trigger_with_no_hooks.js",
1717
"title": "No hooks at all. The only process function."
1818
},
19+
"trigger_with_keep_alive": {
20+
"main": "./triggers/trigger_with_keep_alive.js",
21+
"title": "Returns info whether keep alive agent was used during HTTP request"
22+
},
1923
"fails_to_init": {
2024
"main": "./triggers/fails_to_init.js",
2125
"title": "Fails to init"
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const rp = require('request-promise-native');
2+
3+
exports.process = async function process(msg) {
4+
const options = {
5+
uri: `${msg.body.isHttps ? 'https' : 'http'}://api.acme.com/customers`,
6+
json: true,
7+
resolveWithFullResponse: true
8+
};
9+
10+
const response = await rp.get(options);
11+
this.emit('data', {
12+
id: 'f45be600-f770-11e6-b42d-b187bfbf19fd',
13+
body: {
14+
originalMsg: msg,
15+
customers: response.body,
16+
keepAlive: response.request.agent.keepAlive
17+
}
18+
});
19+
this.emit('end');
20+
};

mocha_spec/run.spec.js

Lines changed: 64 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -164,81 +164,6 @@ describe('Integration Test', () => {
164164
});
165165
});
166166

167-
it('should run trigger successfully', async () => {
168-
helpers.mockApiTaskStepResponse(env);
169-
170-
nock('https://api.acme.com')
171-
.post('/subscribe')
172-
.reply(200, {
173-
id: 'subscription_12345'
174-
})
175-
.get('/customers')
176-
.reply(200, customers);
177-
178-
await amqpHelper.publishMessage(inputMessage, {
179-
parentMessageId,
180-
threadId
181-
});
182-
runner.run(settings.readFrom(env), ipc);
183-
const { message, queueName } = await new Promise(resolve => amqpHelper.on(
184-
'data',
185-
(message, queueName) => resolve({ message, queueName })
186-
));
187-
188-
const { properties, content } = message;
189-
const { body } = encryptor.decryptMessageContent(content, encoding);
190-
expect(queueName).to.eql(amqpHelper.nextStepQueue);
191-
192-
expect(properties.headers.messageId).to.be.a('string');
193-
delete properties.headers.start;
194-
delete properties.headers.end;
195-
delete properties.headers.cid;
196-
delete properties.headers.messageId;
197-
198-
expect(properties.headers).to.deep.equal({
199-
execId: env.ELASTICIO_EXEC_ID,
200-
taskId: env.ELASTICIO_FLOW_ID,
201-
workspaceId: env.ELASTICIO_WORKSPACE_ID,
202-
containerId: env.ELASTICIO_CONTAINER_ID,
203-
userId: env.ELASTICIO_USER_ID,
204-
stepId: env.ELASTICIO_STEP_ID,
205-
compId: env.ELASTICIO_COMP_ID,
206-
function: env.ELASTICIO_FUNCTION,
207-
threadId,
208-
parentMessageId,
209-
protocolVersion: protocolVersion
210-
});
211-
212-
delete properties.headers;
213-
214-
expect(properties).to.deep.equal({
215-
contentType: 'application/json',
216-
contentEncoding: 'utf8',
217-
deliveryMode: undefined,
218-
priority: undefined,
219-
correlationId: undefined,
220-
replyTo: undefined,
221-
expiration: undefined,
222-
messageId: undefined,
223-
timestamp: undefined,
224-
type: undefined,
225-
userId: undefined,
226-
appId: undefined,
227-
clusterId: undefined
228-
});
229-
230-
expect(body).to.deep.equal({
231-
originalMsg: inputMessage,
232-
customers: customers,
233-
subscription: {
234-
id: 'subscription_12345',
235-
cfg: {
236-
apiKey: 'secret'
237-
}
238-
}
239-
});
240-
});
241-
242167
it('should run trigger successfully for input protocolVersion 2', async () => {
243168
helpers.mockApiTaskStepResponse(env);
244169

@@ -1250,6 +1175,70 @@ describe('Integration Test', () => {
12501175
});
12511176
}
12521177

1178+
it('should make all HTTP requests with keep alive agent', async () => {
1179+
env.ELASTICIO_STEP_ID = 'step_2';
1180+
env.ELASTICIO_FLOW_ID = '5559edd38968ec0736000003';
1181+
env.ELASTICIO_FUNCTION = 'trigger_with_keep_alive';
1182+
1183+
helpers.mockApiTaskStepResponse(env);
1184+
1185+
nock('http://api.acme.com')
1186+
.get('/customers')
1187+
.reply(200, customers);
1188+
1189+
await amqpHelper.publishMessage(inputMessage, {
1190+
parentMessageId,
1191+
threadId
1192+
});
1193+
runner.run(settings.readFrom(env), ipc);
1194+
const { message } = await new Promise(resolve => amqpHelper.on(
1195+
'data',
1196+
(message, queueName) => resolve({ message, queueName })
1197+
));
1198+
1199+
const { content } = message;
1200+
const { body } = encryptor.decryptMessageContent(content, 'base64');
1201+
1202+
expect(body).to.deep.equal({
1203+
originalMsg: inputMessage,
1204+
customers,
1205+
keepAlive: true
1206+
});
1207+
});
1208+
1209+
it('should make all HTTPS requests with keep alive agent', async () => {
1210+
env.ELASTICIO_STEP_ID = 'step_2';
1211+
env.ELASTICIO_FLOW_ID = '5559edd38968ec0736000003';
1212+
env.ELASTICIO_FUNCTION = 'trigger_with_keep_alive';
1213+
1214+
inputMessage.body.isHttps = true;
1215+
1216+
helpers.mockApiTaskStepResponse(env);
1217+
1218+
nock('https://api.acme.com')
1219+
.get('/customers')
1220+
.reply(200, customers);
1221+
1222+
await amqpHelper.publishMessage(inputMessage, {
1223+
parentMessageId,
1224+
threadId
1225+
});
1226+
runner.run(settings.readFrom(env), ipc);
1227+
const { message } = await new Promise(resolve => amqpHelper.on(
1228+
'data',
1229+
(message, queueName) => resolve({ message, queueName })
1230+
));
1231+
1232+
const { content } = message;
1233+
const { body } = encryptor.decryptMessageContent(content, 'base64');
1234+
1235+
expect(body).to.deep.equal({
1236+
originalMsg: inputMessage,
1237+
customers,
1238+
keepAlive: true
1239+
});
1240+
});
1241+
12531242
it('should fail if queue deleted', async () => {
12541243
helpers.mockApiTaskStepResponse(env);
12551244

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "elasticio-sailor-nodejs",
33
"description": "The official elastic.io library for bootstrapping and executing for Node.js connectors",
4-
"version": "2.6.28",
4+
"version": "2.6.29",
55
"main": "run.js",
66
"scripts": {
77
"audit": "better-npm-audit audit --level high --production",

run.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,21 @@ const settings = require('./lib/settings.js');
77
const { IPC } = require('./lib/ipc.js');
88
const Q = require('q');
99
const http = require('http');
10+
const https = require('https');
1011

1112
let sailor;
1213
let sailorInit;
1314
let disconnectRequired;
1415

15-
// miserable try to workaround issue described in https://github.com/elasticio/elasticio/issues/4874
16-
const { Agent } = http;
17-
http.globalAgent = new Agent({
18-
keepAlive: true
19-
});
16+
function prepareSandbox() {
17+
// enable keep alive by default to handle issues like https://github.com/elasticio/elasticio/issues/4874
18+
http.globalAgent = new http.Agent({
19+
keepAlive: true
20+
});
21+
https.globalAgent = new https.Agent({
22+
keepAlive: true
23+
});
24+
}
2025

2126
async function putOutToSea(settings, ipc) {
2227
ipc.send('init:started');
@@ -88,6 +93,7 @@ async function gracefulShutdown() {
8893
}
8994

9095
async function run(settings, ipc) {
96+
prepareSandbox();
9197
try {
9298
await putOutToSea(settings, ipc);
9399
logger.info('Fully initialized and waiting for messages');

0 commit comments

Comments
 (0)