1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//! Broker trait and shared types — spec section 22.
//!
//! This module defines the [`Broker`] trait, which is the central async
//! interface for all topic-level operations in the system: publishing
//! messages, subscribing to topics, replaying historical messages,
//! retrieving snapshots, and managing subscriber/publisher lifecycles.
//!
//! Concrete implementations live in sibling modules:
//!
//! - [`InMemoryBroker`](crate::broker::InMemoryBroker) — single-process, all
//! storage backed by in-memory data structures. Ideal for development,
//! testing, and lightweight deployments that do not require persistence
//! across restarts.
//! - [`RemoteBroker`](super::RemoteBroker) — connects to an external broker
//! node over a framed TCP connection using the wire protocol defined in
//! [`crate::broker::wire`]. Suitable for distributed deployments where
//! the gateway and broker run in separate processes.
//! - [`ActorBroker`](super::ActorBroker) — delegates each topic to an
//! independent Tokio actor task via a [`TopicRegistry`](crate::actor::TopicRegistry),
//! allowing publishes to different topics to execute concurrently.
//!
//! # Frame serialization
//!
//! The helper function [`serialize_frame_for_fanout`] converts a [`Frame`]
//! and its broker-assigned offset into a [`Bytes`] buffer that can be
//! delivered to subscribers via the fanout engine. The wire layout is:
//!
//! ```text
//! [b"OFF:"][i64 BE offset][optional payload bytes...]
//! ```
use async_trait;
use Bytes;
use crate;
use crateResult;
use crateFrame;
/// The outcome of publishing a message to a topic.
///
/// Returned by [`Broker::publish`] to inform the caller whether the
/// message was accepted and what offset was assigned. If the message's
/// deduplication key had already been seen within the configured
/// deduplication window, the `duplicate` flag is set to `true`.
/// The central async trait for all topic-level broker operations.
///
/// Implementations must be both [`Send`] and [`Sync`] so they can be
/// shared across async tasks (e.g. behind an `Arc<dyn Broker>`).
///
/// # Error handling
///
/// All fallible methods return [`crate::error::Result`], which wraps
/// [`crate::error::RiftError`]. Callers should inspect the error variant
/// to distinguish between client-side issues (e.g. missing required
/// fields, payload too large) and system-level failures (e.g. storage
/// errors, timeouts).
/// Serialize a frame for fanout delivery to subscribers.
///
/// Produces a [`Bytes`] buffer containing the broker-assigned offset
/// followed by the frame's payload. The format is:
///
/// ```text
/// [b"OFF:"][i64 big-endian offset][payload bytes (if present)]
/// ```
///
/// This encoding allows subscribers to extract the offset prefix with
/// a simple 12-byte header read (`"OFF:"` = 4 bytes + 8-byte `i64`)
/// and then process the remaining payload bytes.
///
/// # Arguments
///
/// * `frame` — The original [`Frame`] whose payload will be included.
/// * `offset` — The monotonic offset assigned by the broker for this
/// message within its topic.
/// A type alias for the subscription handle returned by the broker.
///
/// This is a re-export of [`crate::broker::fanout::Subscription`],
/// which contains the subscription ID, topic name, intent, and
/// cancellation state.
pub type BrokerSubscription = crateSubscription;