Subscribe to a topic. Returns (subscription_id, receiver, backlog). `since_seq`: replay messages starting from this sequence number. 0 = no replay, just new messages.
(
&self,
topic_name: &str,
since_seq: u64,
)
| 241 | /// `since_seq`: replay messages starting from this sequence number. |
| 242 | /// 0 = no replay, just new messages. |
| 243 | pub fn subscribe( |
| 244 | &self, |
| 245 | topic_name: &str, |
| 246 | since_seq: u64, |
| 247 | ) -> Result< |
| 248 | ( |
| 249 | u64, |
| 250 | tokio::sync::mpsc::Receiver<PubSubMessage>, |
| 251 | Vec<PubSubMessage>, |
| 252 | ), |
| 253 | TopicError, |
| 254 | > { |
| 255 | let topics = super::lock_utils::read_or_recover(self.topics.read(), "topics"); |
| 256 | let topic_mutex = topics |
| 257 | .get(topic_name) |
| 258 | .ok_or_else(|| TopicError::NotFound(topic_name.to_string()))?; |
| 259 | let mut topic = super::lock_utils::lock_or_recover(topic_mutex.lock(), "topic"); |
| 260 | |
| 261 | let sub_id = self.next_sub_id.fetch_add(1, Ordering::Relaxed); |
| 262 | let (tx, rx) = tokio::sync::mpsc::channel(256); |
| 263 | |
| 264 | let backlog = topic.get_backlog(since_seq); |
| 265 | |
| 266 | topic.subscribers.insert(sub_id, tx); |
| 267 | debug!( |
| 268 | topic = topic_name, |
| 269 | sub_id, |
| 270 | backlog = backlog.len(), |
| 271 | "subscribed" |
| 272 | ); |
| 273 | Ok((sub_id, rx, backlog)) |
| 274 | } |
| 275 | |
| 276 | /// Subscribe to a topic as a consumer group member. |
| 277 | /// |