Skip to content

mqtt: MQTT 5 (first step) and session expiry - #35

Open
y001j wants to merge 1 commit into
masterfrom
feat/mqtt5
Open

y001j wants to merge 1 commit into
masterfrom
feat/mqtt5

Conversation

@y001j

@y001j y001j commented Nov 22, 2026

Copy link
Copy Markdown
Owner

MQTT 5 alongside 3.1.1/3.1 in the mqtt broker, plus session expiry for both versions.

Interop between versions: the protocol version is per connection and only changes the encoding, so 3.1.1 and 5 clients exchange messages; MQTT 5 message properties are forwarded to MQTT 5 subscribers and dropped for 3.1.1.

MQTT 5 features

  • Properties codec (duplicate / zero-value checks)
  • Reason codes on acks, SUBACK, UNSUBACK, CONNACK; DISCONNECT with reason before server-initiated closes (takeover 0x8E, keepalive 0x8D, malformed, protocol error, topic alias, …); unauthorized publish → PUBACK/PUBREC 0x87
  • Session expiry (CONNECT / DISCONNECT), message expiry (queued, inflight, retained; remaining interval forwarded)
  • User properties, content type, response topic, correlation data, payload format
  • Topic aliases (client → broker), receive maximum, maximum packet size (both directions), assigned client ID
  • Subscription options: No Local, Retain As Published, Retain Handling
  • Will delay (cancelled by reconnect, capped by session end), DISCONNECT with will message
  • Refused with reason codes: shared subscriptions (0x9E), subscription identifiers (0xA1), enhanced auth (0x8C)

Session expiry: Config.SessionExpiry removes sessions offline for longer (3.1.1 persistent sessions previously never expired) and caps MQTT 5 requests. Default 0 keeps 3.1.1 spec behaviour; README recommends setting it.

New API: Config.SessionExpiry, Config.TopicAliasMaximum, Broker.PublishMessage, Message.Expiry, Message.Properties, Properties, UserProperty, Stats.MessagesExpired, Stats.SessionsExpired.

Performance: A/B against master on the same VM (6 rounds, medians): 100 pubs→1 sub 1.03×, fan-out 10→100 1.02×, 1000 pubs 0.95× with overlapping ranges. Profiles show identical allocations per message and ~84% of CPU in syscalls in both.

Tests: portable codec tests; 9 MQTT 5 broker tests; both backends, -race; interop with mosquitto_pub/sub -V mqttv5 (properties, 3.1.1↔5, session resume, retained, will delay ≈2009 ms for 2 s, reconnect cancels will, shared subscription refused).

🤖 Generated with Claude Code

MQTT 5 alongside 3.1.1 and 3.1. The protocol version is per connection
and only changes the encoding, so clients of different versions talk
to each other; a QoS 0 packet is encoded at most once per version.

- Properties: full codec with duplicate/zero-value checks; message
  properties (payload format, content type, response topic, correlation
  data, user properties) travel with the message, reach OnPublish and
  are forwarded to MQTT 5 subscribers (dropped for 3.1.1).
- Reason codes on PUBACK/PUBREC/PUBCOMP (0x87 for unauthorized publishes,
  0x92 for unknown PUBREL), SUBACK, UNSUBACK (0x11 no subscription
  existed), CONNACK, and DISCONNECT before server-initiated closes:
  session taken over, keepalive timeout, malformed packet, protocol
  error, invalid topic alias, subscription identifiers.
- Session expiry (CONNECT and DISCONNECT, 0 cannot be raised on
  DISCONNECT), message expiry (queued, inflight and retained messages;
  forwarded with the remaining interval), topic aliases from clients,
  receive maximum (limits the inflight window), maximum packet size in
  both directions, assigned client IDs, subscription options No Local,
  Retain As Published and Retain Handling, will delay (cancelled by a
  reconnect, capped by the session end), DISCONNECT with will message.
- Not supported, refused with their reason codes: shared subscriptions
  (0x9E), subscription identifiers (0xA1), enhanced authentication
  (0x8C).

Session expiry for MQTT 3.1.1 too: Config.SessionExpiry removes sessions
offline for longer, with their subscriptions and queues (persistent
sessions never expired before), and caps what MQTT 5 clients ask for.

New API: Config.SessionExpiry, Config.TopicAliasMaximum,
Broker.PublishMessage, Message.Expiry, Message.Properties, Properties,
UserProperty, Stats.MessagesExpired, Stats.SessionsExpired.

The subscription trie carries per-subscription options and merges
overlapping subscriptions during the match; PUBLISH properties are only
allocated for MQTT 5 and outgoing PUBLISH buffers are sized up front,
so the 3.1.1 hot path makes the same allocations as before (A/B against
master: within noise in all three scenarios).

Tests: codec tests (portable), 9 MQTT 5 broker tests on both backends;
interop with mosquitto_pub/mosquitto_sub -V mqttv5 (properties, 3.1.1
<-> 5, session resume, retained, will delay, shared subscription
refused).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 22, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.88889% with 58 lines in your changes missing coverage. Please review.
✅ Project coverage is 76.70%. Comparing base (f92576a) to head (1922b7d).

Files with missing lines Patch % Lines
mqtt/broker.go 90.32% 30 Missing ⚠️
mqtt/packet.go 85.84% 15 Missing ⚠️
mqtt/props.go 86.04% 12 Missing ⚠️
mqtt/topic.go 95.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master      #35      +/-   ##
==========================================
+ Coverage   74.98%   76.70%   +1.72%     
==========================================
  Files          31       32       +1     
  Lines        3146     3516     +370     
==========================================
+ Hits         2359     2697     +338     
- Misses        787      819      +32     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant