mochi-mqtt / server

The fully compliant, embeddable high-performance Go MQTT v5 server for IoT, smarthome, and pubsub
MIT License
1.29k stars 222 forks source link

Allow a Hook to reload an individual client session #402

Open snej opened 6 months ago

snej commented 6 months ago

In a clustered environment, client connections are distributed among multiple Server instances on different machines. After a client disconnects, leaving behind a persistent session state, its next login is likely to be on a different node. Because of this, in such a setup an individual Server instance should only keep Client instances corresponding to online client connections, and it should be able to reload an individual client's state (presumably from persistent storage) when that client reconnects.

This commit adds support for such an environment by adding a new hook StoredClientByID. An implementation finds and returns any persistent client data for a given session ID. In practice the only necessary information turned out to be the saved subscriptions and in-flight ack messages. The hook also returns the prior 'Remote' property since the server logs that.

The Server method inheritClientSession is extended to call this hook if there is no matching in-memory Client session. If the hook returns session data, it installs it into the Client object in the same way as the existing code.

At the end of the Server method attachClient, after disconnection, the existence of a StoredClientByID hook is checked; if present, the method expires the Client instance so it won't hang around in memory and so the next connection will go to the hook to reload state.

coveralls commented 6 months ago

Pull Request Test Coverage Report for Build 9038356648

Details


Changes Missing Coverage Covered Lines Changed/Added Lines %
hooks.go 0 15 0.0%
server.go 24 53 45.28%
<!-- Total: 24 68 35.29% -->
Totals Coverage Status
Change from base Build 8892157277: -0.7%
Covered Lines: 6163
Relevant Lines: 6293

💛 - Coveralls