From 8ece28552122f6fe57d2bfa76e32dc21c6f13282 Mon Sep 17 00:00:00 2001 From: Rakhi Mundhada Date: Thu, 25 Jul 2024 14:59:19 +0530 Subject: [PATCH] To handle max socket warning while writing the item on DDb --- package.json | 3 ++- paws_collector.js | 24 ++++++++++++++++++++---- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 6de11fd4..17d72f6d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@alertlogic/paws-collector", - "version": "2.2.4", + "version": "2.2.5", "license": "MIT", "description": "Alert Logic AWS based API Poll Log Collector Library", "repository": { @@ -29,6 +29,7 @@ "@aws-sdk/client-s3": "^3.600.0", "@aws-sdk/client-sqs": "^3.600.0", "@aws-sdk/client-ssm": "^3.600.0", + "@smithy/node-http-handler":"3.1.3", "clone": "*", "jshint": "^2.13.6", "mocha": "^10.4.0", diff --git a/paws_collector.js b/paws_collector.js index 426f4389..247f1217 100644 --- a/paws_collector.js +++ b/paws_collector.js @@ -27,6 +27,9 @@ const { { SSM } = require("@aws-sdk/client-ssm"); +const { NodeHttpHandler } = require("@smithy/node-http-handler"); +const http = require("http"); +const https = require("https"); const fs = require('fs'); const moment = require('moment'); const ddLambda = require('datadog-lambda-js'); @@ -52,10 +55,23 @@ const ERROR_CODE_COMPLETED_STATE = 'COMPLETED_STATE'; const SQS_VISIBILITY_TIMEOUT = 900; const DDB_TTL_DAYS = 14; const DEDUP_LOG_TTL_SECONDS = 86400; -const DDB_OPTIONS = { - maxRetries: 10, - ConsistentRead: true +let maxSocket = process.env.maxSocket ? parseInt(process.env.maxSocket, 10) : 100; + +const agent = { + maxSockets: maxSocket, + keepAlive: true }; + +// Configure the NodeHttpHandler with the custom agents +const nodeHttpHandler = new NodeHttpHandler({ + httpAgent: new http.Agent(agent), + httpsAgent: new https.Agent(agent) +}); + +const DDB_OPTIONS = { + maxAttempts: 10, + requestHandler: nodeHttpHandler +} const DDB_DELETE_BATCH_OPTIONS = { maxBatchSize: 25, maxBatchSizeBytes: 16 * 1024 * 1024 @@ -925,7 +941,7 @@ class PawsCollector extends AlAwsCollector { */ removeDuplicatedItem(logs, paramName, asyncCallback) { let collector = this; - const ddb = new DynamoDB(); + const ddb = new DynamoDB(DDB_OPTIONS); let uniqueLogs = []; var promises = []; let duplicateCount = 0;