reuse agent and test url compatibility

This commit is contained in:
arc-alex
2023-11-05 21:26:25 +01:00
parent 2b2c092d99
commit de93827e40
@@ -9,6 +9,8 @@ import { dbModel } from '../../models/dataModel.js';
import { validateHttpObject } from '../../utils/parserFunctions.js'; import { validateHttpObject } from '../../utils/parserFunctions.js';
import { logger } from '../../classes/Logger.js'; import { logger } from '../../classes/Logger.js';
import { URL } from 'node:url';
type Action = TimerLifeCycleKey | string; type Action = TimerLifeCycleKey | string;
/** /**
@@ -16,10 +18,11 @@ type Action = TimerLifeCycleKey | string;
* @class * @class
*/ */
export class HttpIntegration implements IIntegration { export class HttpIntegration implements IIntegration {
protected httpAgent: null | http.Agent;
subscriptions: Subscription; subscriptions: Subscription;
constructor() { constructor() {
// this.httpClient = null; this.httpAgent = null;
this.subscriptions = dbModel.http.subscriptions; this.subscriptions = dbModel.http.subscriptions;
} }
@@ -33,15 +36,18 @@ export class HttpIntegration implements IIntegration {
try { try {
// this allows re-calling the init function during runtime // this allows re-calling the init function during runtime
// this.httpClient?.close(); this.httpAgent?.destroy();
// this.httpAgent = new http.Agent({ keepAlive: true, timeout: 2000, maxSockets: 5, maxFreeSockets: 40 });
//TODO: find the correct settings
this.httpAgent = new http.Agent({ keepAlive: true, timeout: 1000, maxFreeSockets: 1, maxSockets: 1});
return { return {
success: true, success: true,
message: `HTTP integration client`, message: `HTTP integration client ready`,
}; };
} catch (error) { } catch (error) {
return { return {
success: false, success: false,
message: `Failed initialising HTTP: ${error}`, message: `Failed initialising HTTP integration: ${error}`,
}; };
} }
} }
@@ -53,7 +59,7 @@ export class HttpIntegration implements IIntegration {
} }
dispatch(action: Action, state?: object) { dispatch(action: Action, state?: object) {
if (false) { if (!this.httpAgent) {
return { return {
success: false, success: false,
message: 'Client not initialised', message: 'Client not initialised',
@@ -74,14 +80,19 @@ export class HttpIntegration implements IIntegration {
const { enabled, message } = sub; const { enabled, message } = sub;
if (enabled && message) { if (enabled && message) {
const parsedMessage = parseTemplateNested(message, state || {}); const parsedMessage = parseTemplateNested(message, state || {});
this.emit(parsedMessage); try {
const parsedUrl = new URL(parsedMessage);
this.emit(parsedUrl);
} catch (err) {
logger.error(LogOrigin.Tx, `HTTP Integration: ${err}`);
}
} }
}); });
} }
emit(path: string) { emit(path: URL) {
http http
.get(path, (res) => { .get(path, { agent: this.httpAgent }, (res) => {
if (res.statusCode < 300) { if (res.statusCode < 300) {
res.resume(); res.resume();
return { return {
@@ -103,10 +114,10 @@ export class HttpIntegration implements IIntegration {
shutdown() { shutdown() {
console.log('Shutting down HTTP integration'); console.log('Shutting down HTTP integration');
// if (this.httpClient) { if (this.httpAgent) {
// // this.httpClient?.close(); this.httpAgent?.destroy();
// this.httpClient = null; this.httpAgent = null;
// } }
} }
} }