Skip to content

Commit

Permalink
feat: add opts.keepaliveLimit (#930)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gianluca-Casagrande-Stiga authored Jan 12, 2024
1 parent f625f1b commit e1ee60c
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 2 deletions.
4 changes: 3 additions & 1 deletion aedes.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ const defaultOptions = {
trustProxy: false,
trustedProxies: [],
queueLimit: 42,
maxClientsIdLength: 23
maxClientsIdLength: 23,
keepaliveLimit: 0
}

function Aedes (opts) {
Expand All @@ -47,6 +48,7 @@ function Aedes (opts) {
this.counter = 0
this.queueLimit = opts.queueLimit
this.connectTimeout = opts.connectTimeout
this.keepaliveLimit = opts.keepaliveLimit
this.maxClientsIdLength = opts.maxClientsIdLength
this.mq = opts.mq || mqemitter({
concurrency: opts.concurrency,
Expand Down
1 change: 1 addition & 0 deletions docs/Aedes.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
- `heartbeatInterval` `<number>` an interval in millisconds at which server beats its health signal in `$SYS/<aedes.id>/heartbeat` topic. __Default__: `60000`
- `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`
- Returns `<Aedes>`

Create a new Aedes server.
Expand Down
8 changes: 7 additions & 1 deletion lib/handlers/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ const errorMessages = [
'identifier rejected',
'Server unavailable',
'bad user name or password',
'not authorized'
'not authorized',
'keep alive limit exceeded'
]

function handleConnect (client, packet, done) {
Expand Down Expand Up @@ -66,9 +67,14 @@ function init (client, packet, done) {
if (packet.protocolVersion === 3 && clientId.length > client.broker.maxClientsIdLength) {
returnCode = 2
}
// check if the client keepalive is compatible with broker settings
if (client.broker.keepaliveLimit && (!packet.keepalive || packet.keepalive > client.broker.keepaliveLimit)) {
returnCode = 6
}
if (returnCode > 0) {
const error = new Error(errorMessages[returnCode])
error.errorCode = returnCode
console.error(error)
doConnack(
{ client, returnCode, sessionPresent: false },
done.bind(this, error))
Expand Down
34 changes: 34 additions & 0 deletions test/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,40 @@ test('reject client requested for unsupported protocol version', function (t) {
})
})

test('reject clients that exceed the keepalive limit', function (t) {
t.plan(3)

const broker = aedes({
keepaliveLimit: 100
})
t.teardown(broker.close.bind(broker))

const s = setup(broker)

s.inStream.write({
cmd: 'connect',
keepalive: 150
})
s.outStream.on('data', function (packet) {
console.log(packet)
t.same(packet, {
cmd: 'connack',
returnCode: 6,
length: 2,
qos: 0,
retain: false,
dup: false,
topic: null,
payload: null,
sessionPresent: false
}, 'unsuccessful connack, keep alive limit exceeded')
})
broker.on('connectionError', function (client, err) {
t.equal(err.message, 'keep alive limit exceeded')
t.equal(broker.connectedClients, 0)
})
})

// Guarded in mqtt-packet
test('reject clients with no clientId running on MQTT 3.1.0', function (t) {
t.plan(3)
Expand Down
1 change: 1 addition & 0 deletions test/types/aedes.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ broker = new Aedes({
heartbeatInterval: 60000,
connectTimeout: 30000,
maxClientsIdLength: 23,
keepaliveLimit: 0,
preConnect: (client: Client, packet: ConnectPacket, callback) => {
if (client.req) {
callback(new Error('not websocket stream'), false)
Expand Down
1 change: 1 addition & 0 deletions types/instance.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export interface AedesOptions {
concurrency?: number;
heartbeatInterval?: number;
connectTimeout?: number;
keepaliveLimit?: number;
queueLimit?: number;
maxClientsIdLength?: number;
preConnect?: PreConnectHandler;
Expand Down

0 comments on commit e1ee60c

Please sign in to comment.