Skip to content

Commit

Permalink
updated dedupe function to check if the packet has been seen before a…
Browse files Browse the repository at this point in the history
…nd adds it temporary to memory for tracking duplicates
  • Loading branch information
meganv committed Aug 14, 2024
1 parent 1a191b0 commit 57d458d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ node_modules
.lock-wscript
.vscode
*code-workspace
.idea

# 0x
flamegraph.html
Expand Down
4 changes: 3 additions & 1 deletion aedes.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ const defaultOptions = {
trustedProxies: [],
queueLimit: 42,
maxClientsIdLength: 23,
keepaliveLimit: 0
keepaliveLimit: 0,
dedupeLimit: 100
}

function Aedes (opts) {
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions docs/Aedes.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
- `id` `<string>` aedes broker unique identifier. __Default__: `uuidv4()`
- `connectTimeout` `<number>` maximum waiting time in milliseconds waiting for a [`CONNECT`][CONNECT] packet. __Default__: `30000`
- `keepaliveLimit` `<number>` maximum client keep alive time allowed, 0 means no limit. __Default__: `0`
- `dedupeLimit` `<number>` 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 `<Aedes>`

Create a new Aedes server.
Expand Down
21 changes: 17 additions & 4 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions types/instance.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export interface AedesOptions {
keepaliveLimit?: number;
queueLimit?: number;
maxClientsIdLength?: number;
dedupeLimit?: number;
preConnect?: PreConnectHandler;
authenticate?: AuthenticateHandler;
authorizePublish?: AuthorizePublishHandler;
Expand Down

0 comments on commit 57d458d

Please sign in to comment.