From 57d458d65b208b978acce9326309a0cbafe23c5b Mon Sep 17 00:00:00 2001 From: meganv Date: Wed, 14 Aug 2024 17:31:51 -0400 Subject: [PATCH] updated dedupe function to check if the packet has been seen before and adds it temporary to memory for tracking duplicates --- .gitignore | 1 + aedes.js | 4 +++- docs/Aedes.md | 1 + lib/client.js | 21 +++++++++++++++++---- types/instance.d.ts | 1 + 5 files changed, 23 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index c5e98066..9569c3c3 100644 --- a/.gitignore +++ b/.gitignore @@ -30,6 +30,7 @@ node_modules .lock-wscript .vscode *code-workspace +.idea # 0x flamegraph.html diff --git a/aedes.js b/aedes.js index 956b7409..f9bcfeb4 100644 --- a/aedes.js +++ b/aedes.js @@ -30,7 +30,8 @@ const defaultOptions = { trustedProxies: [], queueLimit: 42, maxClientsIdLength: 23, - keepaliveLimit: 0 + keepaliveLimit: 0, + dedupeLimit: 100 } function Aedes (opts) { @@ -47,6 +48,7 @@ function Aedes (opts) { // internal track for last brokerCounter this.counter = 0 this.queueLimit = opts.queueLimit + this.dedupeLimit = opts.dedupeLimit this.connectTimeout = opts.connectTimeout this.keepaliveLimit = opts.keepaliveLimit this.maxClientsIdLength = opts.maxClientsIdLength diff --git a/docs/Aedes.md b/docs/Aedes.md index 46bc4dc0..906a4049 100644 --- a/docs/Aedes.md +++ b/docs/Aedes.md @@ -43,6 +43,7 @@ - `id` `` aedes broker unique identifier. __Default__: `uuidv4()` - `connectTimeout` `` maximum waiting time in milliseconds waiting for a [`CONNECT`][CONNECT] packet. __Default__: `30000` - `keepaliveLimit` `` maximum client keep alive time allowed, 0 means no limit. __Default__: `0` + - `dedupeLimit` `` the maximum number of packets that are checked for duplication. This is used to prevent the broker from resending the same packet multiple times. __Default__: `100` - Returns `` Create a new Aedes server. diff --git a/lib/client.js b/lib/client.js index e5ee42d0..7849e77f 100644 --- a/lib/client.js +++ b/lib/client.js @@ -167,11 +167,24 @@ function dedupe (client, packet) { } const duplicates = client.duplicates const counter = packet.brokerCounter - const result = (duplicates[id] || 0) < counter - if (result) { - duplicates[id] = counter + + if (!duplicates[id]) { + duplicates[id] = { seen: new Set(), order: [] } + } + + const entry = duplicates[id] + if (entry.seen.has(counter)) { + return false + } + + entry.seen.add(counter) + entry.order.push(counter) + + if (entry.order.length > client.broker.dedupeLimit) { + const oldest = entry.order.shift() + entry.seen.delete(oldest) } - return result + return true } function writeQoS (err, client, packet) { diff --git a/types/instance.d.ts b/types/instance.d.ts index 22bd3aef..04d40cdd 100644 --- a/types/instance.d.ts +++ b/types/instance.d.ts @@ -78,6 +78,7 @@ export interface AedesOptions { keepaliveLimit?: number; queueLimit?: number; maxClientsIdLength?: number; + dedupeLimit?: number; preConnect?: PreConnectHandler; authenticate?: AuthenticateHandler; authorizePublish?: AuthorizePublishHandler;