Coverage for src / zmqtt / _internal / state.py: 98%

55 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-11-12 08:49 +0000

1"""Session state and in-flight QoS tracking.""" 

2 

3import asyncio 

4from dataclasses import dataclass 

5from enum import Enum 

6 

7from zmqtt._internal.packets.publish import PubAck, PubComp, Publish 

8from zmqtt._internal.packets.subscribe import SubAck, UnsubAck 

9from zmqtt._internal.routing import InboundRecipient 

10from zmqtt._internal.subscription_index import SubscriptionIndex 

11 

12 

13class PacketIdPool: 

14 """Allocates 16-bit packet IDs (range 1-65535); reuses after release.""" 

15 

16 def __init__(self) -> None: 

17 self._next: int = 1 

18 self._in_use: set[int] = set() 

19 

20 def acquire(self) -> int: 

21 if len(self._in_use) >= 65535: # noqa: PLR2004 

22 msg = "All 65535 packet IDs are in use" 

23 raise RuntimeError(msg) 

24 while self._next in self._in_use: 

25 self._next = self._next % 65535 + 1 

26 pid = self._next 

27 self._in_use.add(pid) 

28 self._next = pid % 65535 + 1 

29 return pid 

30 

31 def release(self, pid: int) -> None: 

32 self._in_use.discard(pid) 

33 self._next = min(self._next, pid) 

34 

35 

36@dataclass(slots=True) 

37class QoS1Flight: 

38 packet_id: int 

39 publish: Publish 

40 future: asyncio.Future[PubAck] 

41 

42 

43class OutboundQoS2State(Enum): 

44 PENDING_PUBREC = "pending_pubrec" 

45 PENDING_PUBCOMP = "pending_pubcomp" 

46 

47 

48@dataclass(slots=True) 

49class OutboundQoS2Flight: 

50 packet_id: int 

51 publish: Publish 

52 state: OutboundQoS2State 

53 future: asyncio.Future[PubComp] 

54 

55 

56class InboundQoS2State(Enum): 

57 PENDING_PUBREL = "pending_pubrel" 

58 

59 

60@dataclass(slots=True) 

61class InboundQoS2Flight: 

62 packet_id: int 

63 recipient: InboundRecipient 

64 state: InboundQoS2State 

65 delivered: bool = False 

66 

67 

68class SessionState: 

69 """All mutable per-connection session state. No I/O.""" 

70 

71 def __init__(self) -> None: 

72 self.packet_ids: PacketIdPool = PacketIdPool() 

73 self.inflight_qos1: dict[int, QoS1Flight] = {} 

74 self.inflight_qos2_out: dict[int, OutboundQoS2Flight] = {} 

75 self.inflight_qos2_in: dict[int, InboundQoS2Flight] = {} 

76 # QoS 2 inbound: packet_ids received but not yet acked (PUBREC not sent) 

77 self.pending_ack_qos2_in: set[int] = set() 

78 self.subscriptions = SubscriptionIndex() 

79 # topic filter → subscription entry; registered before SUBSCRIBE is sent 

80 # pending protocol acks keyed by packet_id 

81 self.pending_subs: dict[int, asyncio.Future[SubAck]] = {} 

82 self.pending_unsubs: dict[int, asyncio.Future[UnsubAck]] = {} 

83 

84 def clear(self) -> None: 

85 """Reset all state; called on clean-session connect.""" 

86 self.packet_ids = PacketIdPool() 

87 self.inflight_qos1.clear() 

88 self.inflight_qos2_out.clear() 

89 self.inflight_qos2_in.clear() 

90 self.pending_ack_qos2_in.clear() 

91 self.subscriptions.clear() 

92 self.pending_subs.clear() 

93 self.pending_unsubs.clear()