Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: updated dedupe to check history of sent packets #972

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading