This document provides context for the Gemini Code Assistant to understand the IoTHub project.
IoTHub is a high-performance, extensible MQTT server built in Rust with Tokio. It's designed for scalability and reliability, featuring a modern, event-driven asynchronous architecture. The server supports multiple transport protocols and is engineered to handle a large number of concurrent connections with a strong focus on race-condition-free shutdown and robust session management.
The project development follows a milestone-based approach, incrementally adding features to ensure a stable foundation.
The architecture is based on a Server → Broker → Session hierarchy, designed for clear separation of concerns and scalability.
- Server: The central orchestrator. It manages the lifecycle of brokers, handles session registration, and coordinates a graceful, race-condition-free shutdown process using
CancellationToken. - Broker: A network listener for a specific protocol and address (e.g., TCP). It accepts incoming connections, creates
Sessiontasks, and tracks them as "half-connected" until the initial MQTT CONNECT packet is received. - Session: Represents a single connected MQTT client. It manages the client's entire lifecycle, from the initial anonymous state (
__anon_$uuid) to a client-identified state (__client_$clientId). It uses an event-driventokio::select!loop to handle incoming packets, outgoing messages, and shutdown signals. A key design feature is passing the network stream as a mutable reference to packet handlers to prevent deadlocks. - Router: Manages topic subscriptions and routes PUBLISH messages to the appropriate subscribed sessions. It uses the internal
sessionIdfor routing to avoid conflicts with client-provided IDs. - Transport Abstraction: A trait-based system (
AsyncListener,AsyncStream) allows the server to transparently support different network protocols like TCP, with plans for TLS and WebSocket.
CancellationTokenfor Shutdown: Replacedtokio::sync::Notifyto eliminate race conditions and ensure a reliable, stateful shutdown signal is propagated through all components (Server, Broker, Session).- Half-Connected Session Tracking: The
Brokertracks newly accepted connections in ahalf_connected_sessionsmap. This ensures that even sessions that fail to send a CONNECT packet are properly cleaned up. - Stream Deadlock Prevention: Packet handling functions within a
Sessionnow accept a mutable reference to the network stream (&mut dyn AsyncStream). This avoids scenarios where a function could try to acquire a lock on the stream that it already holds, preventing deadlocks. - Thread-Safe Cleanup: To avoid deadlocks during shutdown (e.g., a session trying to unregister itself while the server is iterating over the session map), the
unregister_sessionlogic was simplified to only handle removal from the server's state. The server now manages the shutdown of all components in a clear, sequential order.
iothub/
├── src/
│ ├── protocol/ # MQTT protocol implementation (packets, codecs)
│ ├── server.rs # Core server orchestration and lifecycle management
│ ├── broker.rs # Manages network listeners and half-connected sessions
│ ├── session.rs # Client session handling, state, and packet processing
│ ├── router.rs # Message routing and subscription management
│ ├── transport.rs # Transport abstraction (TCP, TLS, etc.)
│ ├── config.rs # Configuration loading and management
│ ├── storage/ # (Planned) Persistence layer
│ └── auth/ # (Planned) Authentication/Authorization logic
├── docs/ # Architecture, roadmap, and design documents
├── tests/ # Integration and end-to-end tests
└── ...
The project is currently in Milestone 1, focused on building a complete MQTT v3.1.1 server with QoS 0.
-
Completed Features:
- Event-driven architecture with
tokio::select!. CancellationToken-based race-free shutdown.- Half-connected session tracking and cleanup.
- Stream deadlock prevention.
- UNIX signal handling (SIGINT for graceful shutdown).
- Basic MQTT packet handling (CONNECT, PUBLISH, SUBSCRIBE, etc.).
- Event-driven architecture with
-
In Progress / Next Steps for Milestone 1:
- Implementing the core message routing system in
router.rs. - Full implementation of
clean_sessionlogic. - Handling of retained messages and Last Will and Testament (LWT).
- Implementing a keep-alive timeout mechanism.
- Implementing the core message routing system in
-
Future Milestones:
- M2: QoS 1 Support (in-memory).
- M3: QoS 2 Support & Persistent Storage.
- M4: Basic Authentication.
- M5: Enhanced Transport Layer (TLS, WebSocket).
- M6: Pluggable Architecture for auth and storage.
- M7: Production-readiness (metrics, logging, docs).
- Rust 1.75+
cargo build --releasecargo runcargo test