use std::collections::HashMap;
use std::sync::{Arc, Mutex, OnceLock};
use super::codec::Message;
use super::protocol::{ChannelsBatch, ChannelsMessage, ChannelsPublish, ChannelsSubscribe, Empty, Failure, method};
use super::{Link, Route};
use crate::Store;
use crate::channels::{Hub, Log, LogKey, remote_branch};
use crate::sync::lock;
use super::Answered;
const BATCH: usize = 256;
const BATCH_HEAD: usize = 32;
const OUTPUT_BOUND: usize = 8 << 20;
const TURN: usize = 4;
pub(crate) fn route(called: u16) -> Route {
match called {
method::CHANNELS_SUBSCRIBE => Route::Subscribe,
_ => Route::Inline,
}
}
pub(crate) fn call(store: &Store, called: u16, body: &[u8]) -> Answered {
match called {
method::CHANNELS_PUBLISH => {
let Published { hub, name, branch, value } = published(store, body)?;
hub.publish(&name, branch, value)?;
Ok(Empty {}.encode())
}
other => Err(Failure::unimplemented(format!("method {other:#06x}"))),
}
}
#[cfg(feature = "sql")]
pub(crate) fn call_in(store: &Store, tx: &crate::Transaction<'_>, called: u16, body: &[u8]) -> Answered {
if called != method::CHANNELS_PUBLISH {
return Err(Failure::invalid(format!("method {called:#06x} does not run inside a transaction")));
}
let Published { hub, name, branch, value } = published(store, body)?;
tx.after_commit(Box::new(move || {
if let Err(error) = hub.publish(&name, branch, value) {
tracing::warn!(target: "tinystore", channel = %name, %error, "a committed message was not handed over");
}
}));
Ok(Empty {}.encode())
}
struct Published {
hub: Arc<Hub>,
name: Arc<str>,
branch: Vec<String>,
value: Vec<u8>,
}
fn published(store: &Store, body: &[u8]) -> Result<Published, Failure> {
let publish = ChannelsPublish::decode(body)?;
let (name, branch) = remote_branch(&publish.name, publish.under)?;
if let Err(error) = serde_json::from_str::<serde::de::IgnoredAny>(&publish.value) {
return Err(Failure::invalid(format!("channel {name}: a message that is not JSON: {error}")));
}
if publish.value.len() > 1 << 20 {
return Err(Failure::invalid(format!(
"channel {name}: a message of {} bytes, past 1 MiB",
publish.value.len()
)));
}
Ok(Published { hub: Hub::of(store)?, name, branch, value: publish.value.into_bytes() })
}
#[derive(Default)]
pub(crate) struct Subscriptions {
by_stream: Mutex<HashMap<u32, Arc<Subscribed>>>,
}
pub(crate) fn subscribe(
store: &Store,
subscriptions: &Subscriptions,
stream: u32,
body: &[u8],
link: Link,
) -> Result<Arc<Subscribed>, Failure> {
let subscribe = ChannelsSubscribe::decode(body)?;
let (channel, branch) = remote_branch(&subscribe.name, subscribe.under)?;
let hub = Hub::of(store)?;
let key = LogKey { channel, branch, recursive: subscribe.recursive };
let draining = Draining { credit: link.credit, scheduled: true, ended: false };
let subscribed = Arc::new(Subscribed { hub, key, place: OnceLock::new(), link, draining: Mutex::new(draining) });
let woken = Arc::downgrade(&subscribed);
let wake = Arc::new(move || {
if let Some(subscribed) = woken.upgrade() {
subscribed.wake();
}
});
let place = subscribed.hub.join(&subscribed.key, wake)?;
let _ = subscribed.place.set(place);
lock(&subscriptions.by_stream).insert(stream, Arc::clone(&subscribed));
Ok(subscribed)
}
pub(crate) struct Subscribed {
hub: Arc<Hub>,
key: LogKey,
place: OnceLock<(Arc<Log>, u64)>,
link: Link,
draining: Mutex<Draining>,
}
struct Draining {
credit: u64,
scheduled: bool,
ended: bool,
}
impl Subscribed {
pub(crate) fn begin(self: &Arc<Self>) {
lock(&self.draining).scheduled = false;
self.wake();
}
fn wake(self: &Arc<Self>) {
{
let mut draining = lock(&self.draining);
if draining.scheduled || draining.ended {
return;
}
draining.scheduled = true;
}
let drain = Arc::clone(self);
(self.link.spawn)(Box::new(move || drain.drain()));
}
fn drain(self: &Arc<Self>) {
let Some((log, reader)) = self.place.get() else { return };
for sent in 0.. {
if sent == TURN {
let drain = Arc::clone(self);
return (self.link.spawn)(Box::new(move || drain.drain()));
}
if (self.link.owed)() > OUTPUT_BOUND {
return self.wait_for_output();
}
let (credit, budget) = {
let draining = lock(&self.draining);
if draining.ended {
return;
}
(draining.credit, usize::try_from(draining.credit).unwrap_or(usize::MAX).min(self.link.max_body))
};
if budget < BATCH_HEAD {
if self.rest(log, *reader, Some(credit)) {
return;
}
continue;
}
let (taken, waits) = log.take_within(*reader, BATCH, budget - BATCH_HEAD);
if taken.missed == 0 && taken.messages.is_empty() {
if waits && budget == self.link.max_body {
log.skip(*reader);
continue;
}
if self.rest(log, *reader, waits.then_some(credit)) {
return;
}
continue;
}
let messages = taken.messages.iter().map(|message| ChannelsMessage {
under: message.branch.clone(),
value: String::from_utf8_lossy(&message.value).into_owned(),
});
let body = ChannelsBatch { missed: taken.missed, messages: messages.collect() }.encode();
let mut draining = lock(&self.draining);
if draining.ended {
return;
}
draining.credit -= body.len() as u64;
(self.link.send)(body);
}
}
fn wait_for_output(self: &Arc<Self>) {
lock(&self.draining).scheduled = false;
let woken = Arc::downgrade(self);
(self.link.when_drained)(Box::new(move || {
if let Some(subscribed) = woken.upgrade() {
subscribed.wake();
}
}));
}
fn rest(&self, log: &Log, reader: u64, credit: Option<u64>) -> bool {
let mut draining = lock(&self.draining);
let stays = match credit {
Some(seen) => draining.credit == seen,
None => !log.holds_for(reader),
};
if stays {
draining.scheduled = false;
}
stays
}
fn grant(self: &Arc<Self>, credit: u32) {
lock(&self.draining).credit += u64::from(credit);
self.wake();
}
fn end(&self) -> bool {
if std::mem::replace(&mut lock(&self.draining).ended, true) {
return false;
}
if let Some((_, reader)) = self.place.get() {
self.hub.leave(&self.key, *reader);
}
true
}
fn close(&self, last: Answered) {
if self.end() {
(self.link.finish)(last);
}
}
}
impl Subscriptions {
pub(crate) fn grant(&self, stream: u32, credit: u32) {
let subscribed = lock(&self.by_stream).get(&stream).cloned();
if let Some(subscribed) = subscribed {
subscribed.grant(credit);
}
}
pub(crate) fn cancel(&self, stream: u32) -> bool {
let subscribed = lock(&self.by_stream).remove(&stream);
let Some(subscribed) = subscribed else { return false };
subscribed.close(Err(Failure::cancelled("the client cancelled its subscription")));
true
}
pub(crate) fn go_away(&self, why: &str) {
let all: Vec<_> = lock(&self.by_stream).drain().map(|(_, subscribed)| subscribed).collect();
for subscribed in all {
subscribed.close(Err(Failure::unavailable(format!("{why}; subscribe again on another connection"))));
}
}
pub(crate) fn end(&self) {
let all: Vec<_> = lock(&self.by_stream).drain().map(|(_, subscribed)| subscribed).collect();
for subscribed in all {
subscribed.end();
}
}
}
#[cfg(test)]
#[path = "channels_tests.rs"]
mod tests;