use std::collections::VecDeque;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;
use bytestring::ByteString;
use serde::Serialize;
use web_time::Instant;
const MAX_MESSAGES: usize = 100_000;
const MAX_BYTES: usize = 16 * 1024 * 1024;
const MAX_AGE: Duration = Duration::from_secs(3600);
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct Offline {
#[cfg(not(target_arch = "wasm32"))]
pub path: Option<std::path::PathBuf>,
#[cfg(target_arch = "wasm32")]
pub database: Option<String>,
pub max_messages: usize,
pub max_bytes: usize,
pub max_age: Duration,
}
impl Offline {
pub fn in_memory() -> Self {
Self {
#[cfg(not(target_arch = "wasm32"))]
path: None,
#[cfg(target_arch = "wasm32")]
database: None,
max_messages: MAX_MESSAGES,
max_bytes: MAX_BYTES,
max_age: MAX_AGE,
}
}
#[cfg(not(target_arch = "wasm32"))]
pub fn in_file(path: impl Into<std::path::PathBuf>) -> Self {
Self {
path: Some(path.into()),
..Self::in_memory()
}
}
#[cfg(target_arch = "wasm32")]
pub fn in_indexed_db(name: impl Into<String>) -> Self {
Self {
database: Some(name.into()),
..Self::in_memory()
}
}
}
impl Default for Offline {
fn default() -> Self {
Self::in_memory()
}
}
pub(crate) const HEADROOM: usize = 25;
pub(crate) type Report = Arc<dyn Fn(&'static str, String) + Send + Sync>;
pub(crate) type Wake = Arc<dyn Fn() + Send + Sync>;
pub(crate) struct Room {
count: AtomicUsize,
bytes: AtomicUsize,
max_messages: usize,
max_bytes: usize,
waiting: AtomicUsize,
freed: tokio::sync::Notify,
closed: std::sync::atomic::AtomicBool,
}
impl Room {
pub(crate) fn new(offline: &Offline) -> Self {
Self {
count: AtomicUsize::new(0),
bytes: AtomicUsize::new(0),
max_messages: offline.max_messages.max(1),
max_bytes: offline.max_bytes.max(1),
waiting: AtomicUsize::new(0),
freed: tokio::sync::Notify::new(),
closed: std::sync::atomic::AtomicBool::new(false),
}
}
pub(crate) async fn wait_take(&self, bytes: usize) -> Result<(), crate::Error> {
if bytes > self.max_bytes {
return Err(crate::Error::Full);
}
loop {
if self.closed.load(Ordering::Acquire) {
return Err(crate::Error::Closed);
}
if self.take(bytes) {
return Ok(());
}
let freed = self.freed.notified();
tokio::pin!(freed);
freed.as_mut().enable();
self.waiting.fetch_add(1, Ordering::AcqRel);
let taken = !self.closed.load(Ordering::Acquire) && self.take(bytes);
if !taken {
freed.await;
}
self.waiting.fetch_sub(1, Ordering::AcqRel);
if taken {
return Ok(());
}
}
}
pub(crate) fn close(&self) {
self.closed.store(true, Ordering::Release);
self.freed.notify_waiters();
}
pub(crate) fn take(&self, bytes: usize) -> bool {
if self.count.fetch_add(1, Ordering::AcqRel) >= self.max_messages {
self.count.fetch_sub(1, Ordering::AcqRel);
return false;
}
if self.bytes.fetch_add(bytes, Ordering::AcqRel) + bytes > self.max_bytes {
self.bytes.fetch_sub(bytes, Ordering::AcqRel);
self.count.fetch_sub(1, Ordering::AcqRel);
return false;
}
true
}
#[cfg_attr(target_arch = "wasm32", allow(dead_code))]
fn force(&self, bytes: usize) {
self.count.fetch_add(1, Ordering::AcqRel);
self.bytes.fetch_add(bytes, Ordering::AcqRel);
}
pub(crate) fn give_back(&self, bytes: usize) {
self.give_back_many(1, bytes);
}
pub(crate) fn give_back_many(&self, count: usize, bytes: usize) {
if count == 0 {
return;
}
self.count.fetch_sub(count, Ordering::AcqRel);
self.bytes.fetch_sub(bytes, Ordering::AcqRel);
if self.waiting.load(Ordering::Acquire) > 0 {
self.freed.notify_waiters();
}
}
pub(crate) fn count(&self) -> usize {
self.count.load(Ordering::Acquire)
}
}
#[derive(Clone, Debug)]
struct Record {
s: u64,
id: Box<str>,
#[cfg_attr(target_arch = "wasm32", allow(dead_code))]
at: u64,
until: u64,
owned: bool,
requires: Option<Box<[String]>>,
text: ByteString,
size: usize,
tag: u64,
}
pub(crate) trait Tags: Send + Sync {
fn outcomes(&self, outcomes: Vec<Tagged>);
fn holds_back(&self, tag: u64, topics: &[String]) -> bool;
}
#[derive(Debug, PartialEq, Eq)]
pub(crate) struct Tagged {
pub(crate) tag: u64,
pub(crate) id: String,
pub(crate) reason: Option<String>,
}
#[derive(Debug, Default, PartialEq, Eq)]
pub(crate) struct Outcomes {
pub(crate) delivered: Vec<String>,
pub(crate) not_delivered: Vec<(String, Vec<String>)>,
pub(crate) tagged: Vec<Tagged>,
}
impl Outcomes {
fn merge(&mut self, other: Self) {
self.delivered.extend(other.delivered);
for (why, ids) in other.not_delivered {
match self.not_delivered.iter_mut().find(|(w, _)| *w == why) {
Some((_, mine)) => mine.extend(ids),
None => self.not_delivered.push((why, ids)),
}
}
self.tagged.extend(other.tagged);
}
pub(crate) fn deliver(&mut self, id: Box<str>, tag: u64) {
if tag == 0 {
self.delivered.push(id.into());
} else {
self.tagged.push(Tagged {
tag,
id: id.into(),
reason: None,
});
}
}
pub(crate) fn refuse(&mut self, why: &str, id: Box<str>, tag: u64) {
if tag != 0 {
self.tagged.push(Tagged {
tag,
id: id.into(),
reason: Some(why.to_owned()),
});
return;
}
match self.not_delivered.iter_mut().find(|(w, _)| w == why) {
Some((_, ids)) => ids.push(id.into()),
None => self.not_delivered.push((why.to_owned(), vec![id.into()])),
}
}
}
pub(crate) struct Outbox {
records: VecDeque<Record>,
next: u64,
cursor: usize,
producer: Box<str>,
node: Option<Box<str>>,
written: u64,
promised: u64,
flushed: u64,
user: Option<Option<String>>,
ready: bool,
answered: bool,
plain: bool,
limbo: VecDeque<(u64, Box<str>, u64)>,
earliest: u64,
deadline: Option<Instant>,
max_age_ms: u64,
room: Arc<Room>,
log: Option<Log>,
#[cfg(target_arch = "wasm32")]
database: Option<String>,
owed: bool,
heal_at: u64,
heal: Option<Instant>,
report: Report,
wake: Wake,
}
#[cfg(not(target_arch = "wasm32"))]
use file::Log;
#[cfg(target_arch = "wasm32")]
use idb::Log;
#[cfg(target_arch = "wasm32")]
mod idb;
impl Drop for Outbox {
fn drop(&mut self) {
if self.owed || self.log.as_ref().is_some_and(Log::take_broken) {
self.rewrite();
} else if let Some(log) = &mut self.log {
log.flush(self.promised);
}
}
}
struct Read {
producer: Box<str>,
node: Option<Box<str>>,
written: u64,
next: u64,
user: Option<String>,
records: VecDeque<Record>,
}
#[derive(Serialize)]
#[serde(rename_all = "snake_case")]
enum LineRef<'a> {
Written(u64),
Ack(u64),
Drop(&'a [u64]),
User(Option<&'a str>),
Node(&'a str),
}
#[derive(Serialize, serde::Deserialize)]
#[cfg_attr(target_arch = "wasm32", allow(dead_code))]
struct Head<'a> {
wse_outbox: u32,
#[serde(borrow)]
salt: std::borrow::Cow<'a, str>,
#[serde(borrow)]
producer: std::borrow::Cow<'a, str>,
next: u64,
}
impl Outbox {
pub(crate) fn open(offline: &Offline, room: Arc<Room>, report: Report, wake: Wake) -> Self {
let max_age_ms = u64::try_from(offline.max_age.as_millis()).unwrap_or(u64::MAX);
#[allow(unused_mut)]
let mut outbox = Self {
records: VecDeque::new(),
next: 1,
cursor: 0,
producer: new_id().into(),
node: None,
written: 0,
promised: 0,
flushed: 0,
user: None,
ready: false,
answered: false,
plain: false,
limbo: VecDeque::new(),
earliest: u64::MAX,
deadline: None,
max_age_ms,
room,
log: None,
#[cfg(target_arch = "wasm32")]
database: offline.database.clone(),
owed: false,
heal_at: 0,
heal: None,
report,
wake,
};
#[cfg(not(target_arch = "wasm32"))]
if let Some(path) = &offline.path {
match file::Log::open(path, Arc::clone(&outbox.report), Arc::clone(&outbox.wake)) {
Ok((log, read)) => {
if let Some(read) = read {
outbox.restore(read);
}
outbox.log = Some(log);
outbox.rewrite();
}
Err(why) => (outbox.report)("open", why),
}
}
outbox
}
#[cfg(target_arch = "wasm32")]
pub(crate) async fn open_store(&mut self) {
let Some(name) = self.database.take() else {
return;
};
match idb::open(&name, Arc::clone(&self.report), Arc::clone(&self.wake)).await {
Ok((log, read)) => {
self.restore(read);
self.log = Some(log);
}
Err(why) => (self.report)("open", why),
}
}
fn restore(&mut self, read: Read) {
self.producer = read.producer;
self.node = read.node;
self.written = read.written;
self.promised = read.written;
self.user = read.user.map(Some);
self.next = read.next;
for record in read.records {
self.room.force(record.size);
self.earliest = self.earliest.min(record.until);
self.records.push_back(record);
}
self.set_deadline(now_ms());
}
pub(crate) fn hello(&self) -> serde_json::Value {
serde_json::json!({
"producer": &*self.producer,
"node": self.node.as_deref(),
"written": self.written,
"from": self.records.front().map_or(self.next, |r| r.s),
})
}
#[cfg(test)]
pub(crate) fn len(&self) -> usize {
self.records.len()
}
pub(crate) fn deadline(&self) -> Option<Instant> {
match (self.deadline, self.heal) {
(Some(a), Some(b)) => Some(a.min(b)),
(a, b) => a.or(b),
}
}
pub(crate) fn heal(&mut self, now: Instant) {
if self.heal.is_some_and(|at| at <= now) {
self.heal = None;
self.flush();
}
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn add(
&mut self,
id: Box<str>,
text: String,
size: usize,
ttl: Option<Duration>,
now: u64,
requires: Option<Box<[String]>>,
tag: u64,
) -> u64 {
let s = self.next;
self.next += 1;
let wait = ttl.map_or(self.max_age_ms, |ttl| {
u64::try_from(ttl.as_millis()).unwrap_or(u64::MAX)
});
let record = Record {
s,
id,
at: now,
until: now.saturating_add(wait),
owned: matches!(self.user, Some(Some(_))),
requires,
text: number_in_place(s, text),
size,
tag,
};
if record.until < self.earliest {
self.earliest = record.until;
self.set_deadline(now);
}
if let Some(log) = &mut self.log {
log.push_add(&record);
}
self.records.push_back(record);
self.changed();
s
}
pub(crate) fn connected(&mut self, user: Option<&str>, now: u64) -> Outcomes {
let mut outcomes = Outcomes::default();
self.ready = true;
if let Some(last) = &self.user
&& last.as_deref() != user
{
let written = self.written;
self.remove_where("user_changed", &mut outcomes, |_, r| {
r.owned || r.s <= written
});
self.user = Some(user.map(str::to_owned));
self.put(&LineRef::User(user));
} else if self.user.is_none() {
self.user = Some(user.map(str::to_owned));
self.put(&LineRef::User(user));
}
if self.earliest <= now {
self.expire(now, &mut outcomes);
}
self.changed();
outcomes
}
pub(crate) fn server_answers(&mut self, answers: bool) {
self.plain = !answers;
}
pub(crate) fn answered(
&mut self,
acked: u64,
refused: &[(u64, u64, &str)],
node: &str,
unsure: u64,
) -> Outcomes {
let mut outcomes = Outcomes::default();
for (from, to, why) in refused {
outcomes.merge(self.refused(*from, *to, why));
}
outcomes.merge(self.acked(acked));
let may_have_gone = acked.max(self.written.min(unsure));
if may_have_gone < self.written {
self.written = may_have_gone;
self.rearm(now_ms());
}
self.answered = true;
if self.node.as_deref() != Some(node) {
self.node = Some(node.into());
self.put(&LineRef::Node(node));
}
self.changed();
outcomes
}
pub(crate) fn acked(&mut self, s: u64) -> Outcomes {
let mut outcomes = Outcomes::default();
let (gone, bytes) = self.take_front(s, &mut outcomes);
self.room.give_back_many(gone, bytes);
self.cursor = self.cursor.saturating_sub(gone);
while self.limbo.front().is_some_and(|(n, _, _)| *n <= s) {
if let Some((_, id, tag)) = self.limbo.pop_front() {
outcomes.deliver(id, tag);
}
}
if gone > 0 {
self.put(&LineRef::Ack(s));
self.changed();
}
outcomes
}
pub(crate) fn refused(&mut self, from: u64, to: u64, why: &str) -> Outcomes {
let mut outcomes = Outcomes::default();
self.remove_where(why, &mut outcomes, |_, r| (from..=to).contains(&r.s));
self.limbo.retain_mut(|(n, id, tag)| {
let keep = !(from..=to).contains(n);
if !keep {
outcomes.refuse(why, std::mem::take(id), *tag);
}
keep
});
self.changed();
outcomes
}
pub(crate) fn due(
&mut self,
now: u64,
outcomes: &mut Outcomes,
continuous: impl Fn(&[String], u64) -> bool,
mut out: impl FnMut(u64, ByteString),
) {
if self.ready && (self.answered || self.plain) {
if self.earliest <= now {
self.expire(now, outcomes);
}
if self.cursor < self.records.len() {
let numbered = self.answered;
let stored = self
.log
.as_ref()
.filter(|log| numbered && !log.behind())
.map(Log::written);
let mut ahead = false;
let mut last = 0;
for record in self.records.range(self.cursor..) {
if record
.requires
.as_deref()
.is_some_and(|t| !continuous(t, record.tag))
{
break;
}
if stored.is_some_and(|stored| record.s > stored) {
ahead = true;
break;
}
let text = if numbered {
record.text.clone()
} else {
unnumbered(&record.text).into()
};
out(record.s, text);
last = record.s;
self.cursor += 1;
}
if numbered && last > self.written {
self.written = last;
}
let kept = self.records.back().map_or(0, |r| r.s);
if ahead && kept > self.promised {
self.promised = kept;
self.put(&LineRef::Written(kept));
}
}
}
self.flush();
}
pub(crate) fn wrote(&mut self, last: u64) -> Outcomes {
let mut outcomes = Outcomes::default();
self.flushed = self.flushed.max(last);
if self.plain && !self.answered {
let (gone, bytes) = self.take_front(last, &mut outcomes);
self.room.give_back_many(gone, bytes);
self.cursor = self.cursor.saturating_sub(gone);
if gone > 0 {
self.put(&LineRef::Ack(last));
self.changed();
}
}
outcomes
}
fn take_front(&mut self, s: u64, outcomes: &mut Outcomes) -> (usize, usize) {
let (mut gone, mut bytes) = (0, 0);
while self.records.front().is_some_and(|r| r.s <= s) {
if let Some(record) = self.records.pop_front() {
bytes += record.size;
outcomes.deliver(record.id, record.tag);
gone += 1;
}
}
(gone, bytes)
}
pub(crate) fn owed(&self) -> bool {
if self.answered {
!self.limbo.is_empty() || self.records.front().is_some_and(|r| r.s <= self.flushed)
} else {
self.ready && !self.plain && !self.records.is_empty()
}
}
pub(crate) fn close(&mut self) -> Outcomes {
let mut outcomes = self.disconnected();
if self.log.is_none() {
let (count, mut bytes) = (self.records.len(), 0);
for record in self.records.drain(..) {
bytes += record.size;
outcomes.refuse("closed", record.id, record.tag);
}
self.room.give_back_many(count, bytes);
self.earliest = u64::MAX;
self.deadline = None;
}
outcomes
}
pub(crate) fn disconnected(&mut self) -> Outcomes {
let mut outcomes = Outcomes::default();
self.ready = false;
self.answered = false;
self.plain = false;
self.cursor = 0;
self.flushed = 0;
for (_, id, tag) in self.limbo.drain(..) {
outcomes.refuse("cleared", id, tag);
}
self.flush();
outcomes
}
pub(crate) fn clear(&mut self) -> Outcomes {
let mut outcomes = Outcomes::default();
let in_hand = if self.answered { self.flushed } else { 0 };
let mut numbers = Vec::with_capacity(self.records.len());
let mut bytes = 0;
for mut record in self.records.drain(..) {
bytes += record.size;
numbers.push(record.s);
let id = std::mem::take(&mut record.id);
if record.s <= in_hand {
self.limbo.push_back((record.s, id, record.tag));
} else {
outcomes.refuse("cleared", id, record.tag);
}
}
self.room.give_back_many(numbers.len(), bytes);
self.cursor = 0;
self.earliest = u64::MAX;
self.deadline = None;
if !numbers.is_empty() {
self.put(&LineRef::Drop(&numbers));
}
self.user = None;
self.put(&LineRef::User(None));
self.changed();
outcomes
}
pub(crate) fn expire(&mut self, now: u64, outcomes: &mut Outcomes) {
let (written, cursor) = (self.written, self.cursor);
self.remove_where("expired", outcomes, |at, r| {
at >= cursor && r.s > written && r.until < now
});
self.rearm(now);
self.changed();
}
fn rearm(&mut self, now: u64) {
let first = self.records.partition_point(|r| r.s <= self.written);
self.earliest = self
.records
.range(first..)
.map(|r| r.until)
.min()
.unwrap_or(u64::MAX);
self.set_deadline(now);
}
fn set_deadline(&mut self, now: u64) {
self.deadline = if self.earliest == u64::MAX {
None
} else {
Instant::now().checked_add(Duration::from_millis(self.earliest.saturating_sub(now)))
};
}
fn remove_where(
&mut self,
why: &str,
outcomes: &mut Outcomes,
mut gone: impl FnMut(usize, &Record) -> bool,
) {
let mut numbers = Vec::new();
let cursor = self.cursor;
let (mut at, mut before, mut bytes) = (0, 0, 0);
self.records.retain_mut(|r| {
let keep = !gone(at, r);
if !keep {
bytes += r.size;
outcomes.refuse(why, std::mem::take(&mut r.id), r.tag);
numbers.push(r.s);
if at < cursor {
before += 1;
}
}
at += 1;
keep
});
self.cursor -= before;
self.room.give_back_many(numbers.len(), bytes);
if !numbers.is_empty() {
self.put(&LineRef::Drop(&numbers));
}
}
fn put(&mut self, line: &LineRef<'_>) {
if let Some(log) = &mut self.log {
log.push(line);
}
}
fn changed(&mut self) {
if !self.ready {
self.flush();
}
}
pub(crate) fn flush(&mut self) {
if let Some(log) = &mut self.log {
self.owed |= log.take_broken();
if self.owed {
let now = now_ms();
if now >= self.heal_at {
self.owed = false;
self.heal_at = now.saturating_add(1000);
self.heal = None;
self.rewrite();
} else {
log.forget();
self.heal = Some(Instant::now() + Duration::from_millis(self.heal_at - now));
}
} else if log.lines > 2 * self.records.len() + 64 {
self.rewrite();
} else {
log.flush(self.promised);
}
}
}
#[cfg(not(target_arch = "wasm32"))]
fn rewrite(&mut self) {
let Some(log) = &mut self.log else {
return;
};
let salt = new_id();
let head = Head {
wse_outbox: 1,
salt: salt.as_str().into(),
producer: (&*self.producer).into(),
next: self.next,
};
let mut out = Vec::with_capacity(
256 + self
.records
.iter()
.map(|r| r.text.len() + 128)
.sum::<usize>(),
);
file::encode(&mut out, b"", &head);
let mut lines = 1;
let salted = salt.as_bytes();
if let Some(node) = &self.node {
file::encode(&mut out, salted, &LineRef::Node(node));
lines += 1;
}
let mark = self.written.max(self.promised);
self.promised = mark;
if mark > 0 {
file::encode(&mut out, salted, &LineRef::Written(mark));
lines += 1;
}
if let Some(Some(user)) = &self.user {
file::encode(&mut out, salted, &LineRef::User(Some(user)));
lines += 1;
}
for record in &self.records {
file::encode_add(&mut out, salted, record);
lines += 1;
}
log.rewrite(out, salt, lines, mark);
}
#[cfg(target_arch = "wasm32")]
fn rewrite(&mut self) {
let Some(log) = &mut self.log else {
return;
};
let user = match &self.user {
Some(Some(user)) => Some(user.as_str()),
_ => None,
};
let mark = self.written.max(self.promised);
self.promised = mark;
log.rewrite(self.next, mark, self.node.as_deref(), user, &self.records);
}
}
fn number_in_place(s: u64, mut text: String) -> ByteString {
let bytes = text.as_bytes();
if bytes.len() <= HEADROOM
|| bytes[HEADROOM] != b'{'
|| bytes[..HEADROOM].iter().any(|b| *b != b' ')
{
return numbered(s, &text).into();
}
let mut digits = [0u8; 20];
let mut at = digits.len();
let mut n = s;
loop {
at -= 1;
digits[at] = b'0' + (n % 10) as u8;
n /= 10;
if n == 0 {
break;
}
}
let number = &digits[at..];
let start = HEADROOM - 5 - number.len();
let empty = bytes[HEADROOM + 1..]
.iter()
.find(|b| !b.is_ascii_whitespace())
== Some(&b'}');
let mut prefix = [b' '; HEADROOM + 1];
prefix[start..start + 5].copy_from_slice(b"{\"s\":");
prefix[start + 5..HEADROOM].copy_from_slice(number);
prefix[HEADROOM] = if empty { b' ' } else { b',' };
if let Ok(prefix) = std::str::from_utf8(&prefix) {
text.replace_range(..=HEADROOM, prefix);
}
let mut text = ByteString::from(text);
text.advance(start);
text
}
fn numbered(s: u64, text: &str) -> String {
let rest = text.trim_start();
let rest = rest.strip_prefix('{').unwrap_or(rest);
let mut out = String::with_capacity(text.len() + 24);
out.push_str("{\"s\":");
push_number(&mut out, s);
if !rest.trim_start().starts_with('}') {
out.push(',');
}
out.push_str(rest);
out
}
fn unnumbered(text: &str) -> String {
let Some(rest) = text.strip_prefix("{\"s\":") else {
return text.to_owned();
};
let rest = rest.trim_start_matches(|c: char| c.is_ascii_digit());
let rest = rest.strip_prefix(',').unwrap_or(rest);
let mut out = String::with_capacity(rest.len() + 1);
out.push('{');
out.push_str(rest);
out
}
fn push_number(out: &mut String, n: u64) {
let mut digits = [0u8; 20];
let mut at = digits.len();
let mut n = n;
loop {
at -= 1;
digits[at] = b'0' + (n % 10) as u8;
n /= 10;
if n == 0 {
break;
}
}
for digit in &digits[at..] {
out.push(char::from(*digit));
}
}
fn new_id() -> String {
let (hi, lo) = crate::client::random_pair();
format!("{hi:016x}{lo:016x}")
}
pub(crate) fn now_ms() -> u64 {
web_time::SystemTime::now()
.duration_since(web_time::UNIX_EPOCH)
.map_or(0, |d| u64::try_from(d.as_millis()).unwrap_or(u64::MAX))
}
#[cfg_attr(target_arch = "wasm32", allow(dead_code))]
const CRC_TABLES: [[u32; 256]; 8] = {
let mut tables = [[0u32; 256]; 8];
let mut i = 0;
while i < 256 {
let mut c = i as u32;
let mut k = 0;
while k < 8 {
c = if c & 1 != 0 {
0xEDB8_8320 ^ (c >> 1)
} else {
c >> 1
};
k += 1;
}
tables[0][i] = c;
i += 1;
}
let mut i = 0;
while i < 256 {
let mut k = 1;
while k < 8 {
let before = tables[k - 1][i];
tables[k][i] = (before >> 8) ^ tables[0][(before & 0xFF) as usize];
k += 1;
}
i += 1;
}
tables
};
#[cfg_attr(target_arch = "wasm32", allow(dead_code))]
fn crc32(parts: &[&[u8]]) -> u32 {
let t = &CRC_TABLES;
let mut crc = !0u32;
for part in parts {
let (chunks, rest) = part.as_chunks::<8>();
for c in chunks {
let lo = u32::from_le_bytes([c[0], c[1], c[2], c[3]]) ^ crc;
let hi = u32::from_le_bytes([c[4], c[5], c[6], c[7]]);
crc = t[7][(lo & 0xFF) as usize]
^ t[6][((lo >> 8) & 0xFF) as usize]
^ t[5][((lo >> 16) & 0xFF) as usize]
^ t[4][(lo >> 24) as usize]
^ t[3][(hi & 0xFF) as usize]
^ t[2][((hi >> 8) & 0xFF) as usize]
^ t[1][((hi >> 16) & 0xFF) as usize]
^ t[0][(hi >> 24) as usize];
}
for byte in rest {
crc = t[0][((crc ^ u32::from(*byte)) & 0xFF) as usize] ^ (crc >> 8);
}
}
!crc
}
#[cfg(not(target_arch = "wasm32"))]
mod file {
#[cfg(any(target_os = "linux", target_os = "android"))]
mod linux {
#[cfg(target_os = "android")]
use std::os::android::net::SocketAddrExt;
#[cfg(target_os = "linux")]
use std::os::linux::net::SocketAddrExt;
use std::os::unix::net::{SocketAddr, UnixListener};
use std::path::Path;
use sha2::{Digest, Sha256};
pub(super) fn name(path: &Path) -> std::io::Result<String> {
let dir = path.parent().filter(|d| !d.as_os_str().is_empty());
let dir = std::fs::canonicalize(dir.unwrap_or(Path::new(".")))?;
let file = path.file_name().unwrap_or_default();
let full = dir.join(file);
let digest = Sha256::digest(full.as_os_str().as_encoded_bytes());
let mut name = String::from("wse-outbox-");
for byte in &digest[..20] {
use std::fmt::Write;
let _ = write!(name, "{byte:02x}");
}
Ok(name)
}
#[cfg(test)]
#[test]
fn the_lock_is_named_as_the_other_clients_name_it() {
assert_eq!(
name(Path::new("/wse-outbox-test-queue")).unwrap(),
"wse-outbox-0bd17a84322f83203c373a9c2d09c16e9474d9c6"
);
}
#[cfg(test)]
#[test]
fn a_file_another_process_holds_on_linux_is_another_clients() {
let dir = std::env::temp_dir().join(format!("wse-lock-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("queue");
let held = named_lock(&path).unwrap();
assert!(matches!(named_lock(&path), Err(None)));
drop(held);
assert!(named_lock(&path).is_ok());
std::fs::remove_dir_all(&dir).unwrap();
}
pub(super) fn named_lock(path: &Path) -> Result<UnixListener, Option<std::io::Error>> {
let name = name(path).map_err(Some)?;
let addr = SocketAddr::from_abstract_name(name.as_bytes()).map_err(Some)?;
UnixListener::bind_addr(&addr)
.map_err(|e| (e.kind() != std::io::ErrorKind::AddrInUse).then_some(e))
}
}
use std::collections::VecDeque;
use std::fs::{File, OpenOptions, TryLockError};
use std::io::Write;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, mpsc};
use serde::{Deserialize, Serialize};
use serde_json::value::RawValue;
use super::{Head, LineRef, Read, Record, Report, Wake, crc32};
#[derive(Deserialize)]
struct RecordIn<'a> {
s: u64,
id: Box<str>,
at: u64,
until: u64,
#[serde(default)]
owned: bool,
#[serde(default)]
requires: Option<Vec<String>>,
#[serde(borrow)]
text: &'a RawValue,
}
#[derive(Deserialize)]
#[serde(rename_all = "snake_case")]
enum Line<'a> {
#[serde(borrow)]
Add(RecordIn<'a>),
Written(u64),
Ack(u64),
Drop(Vec<u64>),
User(Option<String>),
Node(String),
}
enum Job {
Append(Vec<u8>, u64),
Rewrite(Vec<u8>, u64),
}
pub(super) struct Log {
jobs: Option<mpsc::Sender<Job>>,
thread: Option<std::thread::JoinHandle<()>>,
salt: String,
batch: Vec<u8>,
pub(super) lines: usize,
broken: Arc<AtomicBool>,
written: Arc<AtomicU64>,
behind: Arc<AtomicBool>,
}
impl Log {
pub(super) fn open(
path: &Path,
report: Report,
wake: Wake,
) -> Result<(Self, Option<Read>), String> {
let fail = |what: &str, e: std::io::Error| format!("{}: {what}: {e}", path.display());
let mut lock_path = path.as_os_str().to_owned();
lock_path.push(".lock");
let lock = OpenOptions::new()
.create(true)
.truncate(false)
.write(true)
.open(&lock_path)
.map_err(|e| fail("cannot open its lock file", e))?;
match lock.try_lock() {
Ok(()) => {}
Err(TryLockError::WouldBlock) => {
return Err(format!("{} is another client's", path.display()));
}
Err(TryLockError::Error(e)) => return Err(fail("cannot lock it", e)),
}
#[cfg(any(target_os = "linux", target_os = "android"))]
let named = linux::named_lock(path).map_err(|e| match e {
Some(e) => fail("cannot lock it", e),
None => format!("{} is another client's", path.display()),
})?;
let read = match std::fs::read(path) {
Ok(bytes) => read(&bytes),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => None,
Err(e) => return Err(fail("cannot read it", e)),
};
let file = append(path).map_err(|e| fail("cannot open it", e))?;
let mut tmp = path.as_os_str().to_owned();
tmp.push(".tmp");
let broken = Arc::new(AtomicBool::new(false));
let written = Arc::new(AtomicU64::new(read.as_ref().map_or(0, |r| r.written)));
let behind = Arc::new(AtomicBool::new(false));
let writer = Writer {
path: path.to_owned(),
tmp: PathBuf::from(tmp),
file: Some(file),
_lock: lock,
#[cfg(any(target_os = "linux", target_os = "android"))]
_named: named,
report,
broken: Arc::clone(&broken),
written: Arc::clone(&written),
wake,
skipping: Arc::clone(&behind),
failing: false,
};
let (jobs, taken) = mpsc::channel();
let thread = std::thread::Builder::new()
.name("wse-outbox".to_owned())
.spawn(move || writer.run(&taken))
.map_err(|e| fail("cannot start its thread", e))?;
let log = Self {
jobs: Some(jobs),
thread: Some(thread),
salt: String::new(),
batch: Vec::new(),
lines: 0,
broken,
written,
behind,
};
Ok((log, read))
}
pub(super) fn push(&mut self, line: &LineRef<'_>) {
encode(&mut self.batch, self.salt.as_bytes(), line);
self.lines += 1;
}
pub(super) fn push_add(&mut self, record: &Record) {
encode_add(&mut self.batch, self.salt.as_bytes(), record);
self.lines += 1;
}
pub(super) fn flush(&mut self, mark: u64) {
if self.batch.is_empty() {
return;
}
let capacity = self.batch.capacity().min(64 * 1024);
let batch = std::mem::replace(&mut self.batch, Vec::with_capacity(capacity));
if let Some(jobs) = &self.jobs {
let _ = jobs.send(Job::Append(batch, mark));
}
}
pub(super) fn written(&self) -> u64 {
self.written.load(Ordering::Acquire)
}
pub(super) fn behind(&self) -> bool {
self.behind.load(Ordering::Acquire)
}
pub(super) fn forget(&mut self) {
self.batch.clear();
}
pub(super) fn take_broken(&self) -> bool {
self.broken.swap(false, Ordering::AcqRel)
}
pub(super) fn rewrite(&mut self, bytes: Vec<u8>, salt: String, lines: usize, mark: u64) {
self.batch.clear();
self.salt = salt;
self.lines = lines;
if let Some(jobs) = &self.jobs {
let _ = jobs.send(Job::Rewrite(bytes, mark));
}
}
}
impl Drop for Log {
fn drop(&mut self) {
self.jobs.take();
if let Some(thread) = self.thread.take() {
let _ = thread.join();
}
}
}
struct Writer {
path: PathBuf,
tmp: PathBuf,
file: Option<File>,
_lock: File,
#[cfg(any(target_os = "linux", target_os = "android"))]
_named: std::os::unix::net::UnixListener,
report: Report,
broken: Arc<AtomicBool>,
written: Arc<AtomicU64>,
wake: Wake,
skipping: Arc<AtomicBool>,
failing: bool,
}
impl Writer {
fn run(mut self, jobs: &mpsc::Receiver<Job>) {
while let Ok(job) = jobs.recv() {
let mut mark = 0;
let mut dirty = self.take(job, &mut mark);
while let Ok(job) = jobs.try_recv() {
dirty |= self.take(job, &mut mark);
}
if mark > self.written.fetch_max(mark, Ordering::AcqRel) {
(self.wake)();
}
if dirty
&& let Some(file) = &self.file
&& let Err(e) = file.sync_data()
{
self.fail(&format!("cannot sync it to the disk: {e}"));
}
}
}
fn take(&mut self, job: Job, mark: &mut u64) -> bool {
match job {
Job::Append(lines, holds) => {
if self.skipping.load(Ordering::Acquire) {
return false;
}
let written = match &mut self.file {
Some(file) => file.write_all(&lines),
None => Err(std::io::Error::other("no file")),
};
match written {
Ok(()) => {
*mark = (*mark).max(holds);
true
}
Err(e) => {
self.fail(&format!("cannot write it: {e}"));
false
}
}
}
Job::Rewrite(bytes, holds) => {
match self.rewrite(&bytes) {
Ok(()) => {
*mark = (*mark).max(holds);
self.skipping.store(false, Ordering::Release);
self.failing = false;
}
Err(e) => self.fail(&format!("cannot write it anew: {e}")),
}
false
}
}
}
fn fail(&mut self, why: &str) {
self.skipping.store(true, Ordering::Release);
self.broken.store(true, Ordering::Release);
if !std::mem::replace(&mut self.failing, true) {
(self.report)("write", format!("{}: {why}", self.path.display()));
}
}
fn rewrite(&mut self, bytes: &[u8]) -> std::io::Result<()> {
let written = File::create(&self.tmp).and_then(|mut tmp| {
tmp.write_all(bytes)?;
tmp.sync_all()
});
if let Err(e) = written {
let _ = std::fs::remove_file(&self.tmp);
return Err(e);
}
self.file = None;
if let Err(e) = std::fs::rename(&self.tmp, &self.path) {
let _ = std::fs::remove_file(&self.tmp);
self.file = append(&self.path).ok();
return Err(e);
}
sync_dir(&self.path);
self.file = Some(append(&self.path)?);
Ok(())
}
}
fn append(path: &Path) -> std::io::Result<File> {
OpenOptions::new().create(true).append(true).open(path)
}
fn sync_dir(path: &Path) {
#[cfg(unix)]
if let Some(dir) = path.parent() {
let dir = if dir.as_os_str().is_empty() {
Path::new(".")
} else {
dir
};
if let Ok(dir) = File::open(dir) {
let _ = dir.sync_all();
}
}
#[cfg(not(unix))]
let _ = path;
}
pub(super) fn encode(out: &mut Vec<u8>, salt: &[u8], line: &impl Serialize) {
let start = begin(out);
let _ = serde_json::to_writer(&mut *out, line);
end(out, start, salt);
}
pub(super) fn encode_add(out: &mut Vec<u8>, salt: &[u8], record: &Record) {
let start = begin(out);
out.extend_from_slice(b"{\"add\":{\"s\":");
number(out, record.s);
out.extend_from_slice(b",\"id\":");
let _ = serde_json::to_writer(&mut *out, &*record.id);
out.extend_from_slice(b",\"at\":");
number(out, record.at);
out.extend_from_slice(b",\"until\":");
number(out, record.until);
out.extend_from_slice(if record.owned {
b",\"owned\":true"
} else {
b",\"owned\":false"
});
if let Some(requires) = &record.requires {
out.extend_from_slice(b",\"requires\":");
let _ = serde_json::to_writer(&mut *out, requires);
}
out.extend_from_slice(b",\"text\":");
out.extend_from_slice(record.text.as_bytes());
out.extend_from_slice(b"}}");
end(out, start, salt);
}
fn begin(out: &mut Vec<u8>) -> usize {
let start = out.len();
out.extend_from_slice(b"00000000 ");
start
}
fn end(out: &mut Vec<u8>, start: usize, salt: &[u8]) {
const HEX: &[u8; 16] = b"0123456789abcdef";
let crc = crc32(&[salt, &out[start + 9..]]);
for (i, byte) in out[start..start + 8].iter_mut().enumerate() {
*byte = HEX[((crc >> (28 - 4 * i)) & 0xF) as usize];
}
out.push(b'\n');
}
fn number(out: &mut Vec<u8>, n: u64) {
let mut digits = [0u8; 20];
let mut at = digits.len();
let mut n = n;
loop {
at -= 1;
digits[at] = b'0' + (n % 10) as u8;
n /= 10;
if n == 0 {
break;
}
}
out.extend_from_slice(&digits[at..]);
}
fn checked<'a>(line: &'a [u8], salt: &[u8]) -> Option<&'a [u8]> {
if line.get(8) != Some(&b' ') {
return None;
}
let (crc, json) = (line.get(..8)?, line.get(9..)?);
let crc = u32::from_str_radix(std::str::from_utf8(crc).ok()?, 16).ok()?;
(crc32(&[salt, json]) == crc).then_some(json)
}
fn unknown_kind(json: &[u8]) -> bool {
const KNOWN: [&str; 6] = ["add", "written", "ack", "drop", "user", "node"];
serde_json::from_slice::<std::collections::BTreeMap<String, &RawValue>>(json)
.is_ok_and(|line| line.len() == 1 && line.keys().all(|k| !KNOWN.contains(&k.as_str())))
}
pub(super) fn read(bytes: &[u8]) -> Option<Read> {
let mut lines = bytes.split(|b| *b == b'\n');
let head: Head<'_> = serde_json::from_slice(checked(lines.next()?, b"")?).ok()?;
if head.wse_outbox != 1 {
return None;
}
let salt = head.salt.as_bytes();
let mut read = Read {
producer: head.producer.as_ref().into(),
node: None,
written: 0,
next: head.next.max(1),
user: None,
records: VecDeque::new(),
};
for line in lines {
if line.is_empty() {
continue;
}
let Some(json) = checked(line, salt) else {
tracing::warn!("[WSE] the outbox's file ends in a line that does not check");
break;
};
let line = match serde_json::from_slice::<Line<'_>>(json) {
Ok(line) => line,
Err(_) if unknown_kind(json) => continue,
Err(_) => {
tracing::warn!("[WSE] the outbox's file ends in a line that does not check");
break;
}
};
match line {
Line::Add(record) => {
let text = record.text.get();
let requires = match record.requires {
None => None,
Some(topics)
if !topics.is_empty() && topics.iter().all(|t| !t.is_empty()) =>
{
Some(topics.into_boxed_slice())
}
Some(_) => {
tracing::warn!(
"[WSE] the outbox's file ends in a line that does not check"
);
break;
}
};
if !text.starts_with('{') {
tracing::warn!(
"[WSE] the outbox's file ends in a line that does not check"
);
break;
}
read.next = read.next.max(record.s + 1);
read.records.push_back(Record {
s: record.s,
id: record.id,
at: record.at,
until: record.until,
owned: record.owned,
requires,
size: super::unnumbered(text).len(),
text: text.into(),
tag: 0,
});
}
Line::Written(n) => {
read.written = read.written.max(n);
read.next = read.next.max(n + 1);
}
Line::Ack(n) => {
while read.records.front().is_some_and(|r| r.s <= n) {
read.records.pop_front();
}
}
Line::Drop(mut numbers) => {
numbers.sort_unstable();
read.records
.retain(|r| numbers.binary_search(&r.s).is_err());
}
Line::User(user) => read.user = user,
Line::Node(node) => read.node = Some(node.into()),
}
}
Some(read)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn quiet() -> Report {
Arc::new(|_, _| {})
}
fn outbox(offline: &Offline) -> Outbox {
Outbox::open(
offline,
Arc::new(Room::new(offline)),
quiet(),
Arc::new(|| {}),
)
}
fn kept(json: &str) -> String {
format!("{}{json}", " ".repeat(HEADROOM))
}
fn add(outbox: &mut Outbox, id: &str) -> u64 {
let json = format!(r#"{{"t":"m","p":{{}},"id":"{id}"}}"#);
assert!(outbox.room.take(json.len()));
outbox.add(id.into(), kept(&json), json.len(), None, 0, None, 0)
}
fn due(outbox: &mut Outbox, now: u64, outcomes: &mut Outcomes) -> Vec<(u64, String)> {
let mut out = Vec::new();
outbox.due(
now,
outcomes,
|_, _| true,
|s, text| out.push((s, text.to_string())),
);
out
}
fn answering(outbox: &mut Outbox, acked: u64) -> Outcomes {
answering_unsure(outbox, acked, 0)
}
fn answering_unsure(outbox: &mut Outbox, acked: u64, unsure: u64) -> Outcomes {
outbox.connected(Some("ann"), 0);
outbox.server_answers(true);
outbox.answered(acked, &[], "n1", unsure)
}
fn numbers(out: &[(u64, String)]) -> Vec<u64> {
out.iter().map(|(s, _)| *s).collect()
}
fn ids(list: &[String]) -> Vec<&str> {
list.iter().map(String::as_str).collect()
}
#[test]
fn a_message_waits_for_the_answer_and_goes_again_after_a_reconnect() {
let mut outbox = outbox(&Offline::in_memory());
add(&mut outbox, "a");
add(&mut outbox, "b");
let mut outcomes = Outcomes::default();
assert!(
due(&mut outbox, 0, &mut outcomes).is_empty(),
"no connection"
);
outbox.connected(Some("ann"), 0);
outbox.server_answers(true);
assert!(
due(&mut outbox, 0, &mut outcomes).is_empty(),
"no answer yet"
);
assert_eq!(outbox.hello()["from"], 1);
outbox.answered(0, &[], "n1", 0);
let out = due(&mut outbox, 0, &mut outcomes);
assert_eq!(
out,
[
(1, r#"{"s":1,"t":"m","p":{},"id":"a"}"#.to_owned()),
(2, r#"{"s":2,"t":"m","p":{},"id":"b"}"#.to_owned())
]
);
outbox.wrote(2);
assert_eq!(ids(&outbox.acked(1).delivered), ["a"]);
assert_eq!(outbox.disconnected(), Outcomes::default());
assert_eq!(outbox.hello()["written"], 2);
assert_eq!(outbox.hello()["node"], "n1");
assert!(answering(&mut outbox, 1).delivered.is_empty());
assert_eq!(numbers(&due(&mut outbox, 0, &mut outcomes)), [2]);
add(&mut outbox, "c");
assert_eq!(
numbers(&due(&mut outbox, 0, &mut outcomes)),
[3],
"a new one"
);
assert_eq!(ids(&outbox.acked(3).delivered), ["b", "c"]);
assert_eq!(outbox.room.count(), 0);
assert_eq!(outcomes, Outcomes::default());
}
#[test]
fn the_answer_may_come_before_the_server_takes_the_connection() {
let mut outbox = outbox(&Offline::in_memory());
add(&mut outbox, "a");
let mut outcomes = Outcomes::default();
outbox.server_answers(true);
outbox.answered(0, &[], "n1", 0);
assert!(
due(&mut outbox, 0, &mut outcomes).is_empty(),
"not taken yet"
);
outbox.connected(Some("ann"), 0);
assert_eq!(numbers(&due(&mut outbox, 0, &mut outcomes)), [1]);
}
#[test]
fn a_server_without_answers_takes_what_was_written() {
let mut outbox = outbox(&Offline::in_memory());
add(&mut outbox, "a");
outbox.connected(None, 0);
outbox.server_answers(false);
let mut outcomes = Outcomes::default();
let out = due(&mut outbox, 0, &mut outcomes);
assert_eq!(
out,
[(1, r#"{"t":"m","p":{},"id":"a"}"#.to_owned())],
"not numbered"
);
assert_eq!(ids(&outbox.wrote(1).delivered), ["a"]);
assert_eq!(outbox.len(), 0);
assert_eq!(outbox.hello()["written"], 0);
}
#[test]
fn a_message_never_goes_again_as_another_user_than_it_was_written_as() {
let mut outbox = outbox(&Offline::in_memory());
add(&mut outbox, "before any");
answering(&mut outbox, 0);
let mut outcomes = Outcomes::default();
assert_eq!(numbers(&due(&mut outbox, 0, &mut outcomes)), [1]);
outbox.disconnected();
let outcomes = outbox.connected(Some("bob"), 0);
assert_eq!(
outcomes.not_delivered,
[("user_changed".to_owned(), vec!["before any".to_owned()])]
);
outbox.server_answers(true);
outbox.answered(0, &[], "n1", 0);
assert!(due(&mut outbox, 0, &mut Outcomes::default()).is_empty());
}
#[test]
fn another_users_messages_go_when_a_connection_is_taken_for_another() {
let mut outbox = outbox(&Offline::in_memory());
add(&mut outbox, "before any");
outbox.connected(Some("ann"), 0);
outbox.disconnected();
add(&mut outbox, "anns");
let outcomes = outbox.connected(Some("bob"), 0);
assert_eq!(
outcomes.not_delivered,
[("user_changed".to_owned(), vec!["anns".to_owned()])]
);
assert_eq!(
outbox.len(),
1,
"sent before any connection: it goes on any"
);
assert_eq!(outbox.room.count(), 1);
}
#[test]
fn a_message_not_written_in_its_time_runs_out_on_a_timer() {
let mut outbox = outbox(&Offline::in_memory());
assert!(outbox.room.take(2));
outbox.add(
"late".into(),
kept("{}"),
2,
Some(Duration::from_millis(5)),
0,
None,
0,
);
add(&mut outbox, "on time");
assert!(outbox.deadline().is_some(), "a timer for the late one");
let mut outcomes = Outcomes::default();
outbox.expire(10, &mut outcomes);
assert_eq!(
outcomes.not_delivered,
[("expired".to_owned(), vec!["late".to_owned()])]
);
assert!(outbox.deadline().is_some(), "a timer for the one on time");
let outcomes = answering(&mut outbox, 0);
assert_eq!(outcomes, Outcomes::default());
let mut outcomes = Outcomes::default();
assert_eq!(numbers(&due(&mut outbox, 10, &mut outcomes)), [2]);
}
#[test]
fn a_refusal_not_heard_comes_with_the_answer() {
let mut outbox = outbox(&Offline::in_memory());
answering(&mut outbox, 0);
for id in ["a", "b", "c"] {
add(&mut outbox, id);
}
let mut outcomes = Outcomes::default();
due(&mut outbox, 0, &mut outcomes);
outbox.disconnected();
assert_eq!(outbox.hello()["from"], 1, "waits to hear of 1 on");
outbox.connected(Some("ann"), 0);
outbox.server_answers(true);
let outcomes = outbox.answered(3, &[(2, 2, "busy")], "n1", 0);
assert_eq!(ids(&outcomes.delivered), ["a", "c"]);
assert_eq!(
outcomes.not_delivered,
[("busy".to_owned(), vec!["b".to_owned()])]
);
assert_eq!(outbox.hello()["from"], 4, "nothing waits");
assert_eq!(outbox.room.count(), 0);
}
#[test]
fn a_refused_message_is_said() {
let mut outbox = outbox(&Offline::in_memory());
answering(&mut outbox, 0);
add(&mut outbox, "x");
add(&mut outbox, "y");
let mut outcomes = Outcomes::default();
assert_eq!(numbers(&due(&mut outbox, 0, &mut outcomes)), [1, 2]);
let refused = outbox.refused(1, 1, "busy");
assert_eq!(
refused.not_delivered,
[("busy".to_owned(), vec!["x".to_owned()])]
);
assert_eq!(ids(&outbox.acked(2).delivered), ["y"]);
assert_eq!(outbox.room.count(), 0);
}
#[test]
fn a_message_written_waits_for_its_answer_whatever_its_age_where_the_server_cannot_tell() {
let mut outbox = outbox(&Offline::in_memory());
answering(&mut outbox, 0);
assert!(outbox.room.take(2));
outbox.add(
"a".into(),
kept("{}"),
2,
Some(Duration::from_millis(5)),
0,
None,
0,
);
let mut outcomes = Outcomes::default();
assert_eq!(numbers(&due(&mut outbox, 0, &mut outcomes)), [1]);
outbox.disconnected();
let outcomes = answering_unsure(&mut outbox, 0, 1);
assert_eq!(outcomes, Outcomes::default(), "not expired: it was written");
let mut outcomes = Outcomes::default();
assert_eq!(numbers(&due(&mut outbox, 100, &mut outcomes)), [1]);
assert_eq!(outcomes, Outcomes::default());
outbox.disconnected();
assert_eq!(answering(&mut outbox, 0), Outcomes::default());
let mut outcomes = Outcomes::default();
assert!(due(&mut outbox, 100, &mut outcomes).is_empty());
assert_eq!(
outcomes.not_delivered,
[("expired".to_owned(), vec!["a".to_owned()])]
);
assert_eq!(outbox.hello()["written"], 0);
}
#[test]
fn a_clear_tells_what_the_server_has_in_hand_by_its_answer() {
let mut outbox = outbox(&Offline::in_memory());
answering(&mut outbox, 0);
add(&mut outbox, "a");
add(&mut outbox, "b");
let mut outcomes = Outcomes::default();
due(&mut outbox, 0, &mut outcomes);
outbox.wrote(2);
add(&mut outbox, "c");
let cleared = outbox.clear();
assert_eq!(
cleared.not_delivered,
[("cleared".to_owned(), vec!["c".to_owned()])],
"c never left"
);
assert_eq!(outbox.room.count(), 0);
assert_eq!(ids(&outbox.acked(1).delivered), ["a"]);
assert_eq!(
outbox.disconnected().not_delivered,
[("cleared".to_owned(), vec!["b".to_owned()])]
);
answering(&mut outbox, 0);
assert!(due(&mut outbox, 0, &mut outcomes).is_empty());
}
#[test]
fn the_room_refuses_past_its_limits() {
let mut offline = Offline::in_memory();
offline.max_messages = 2;
offline.max_bytes = 100;
let room = Room::new(&offline);
assert!(room.take(10));
assert!(room.take(80));
assert!(!room.take(1), "two at most");
room.give_back(80);
assert!(!room.take(91), "100 bytes at most");
assert!(room.take(90));
}
#[test]
fn crc32_is_zlibs() {
assert_eq!(crc32(&[b"123456789"]), 0xCBF4_3926);
assert_eq!(crc32(&[b"1234", b"56789"]), 0xCBF4_3926);
let bytes: Vec<u8> = (0..200u32).map(|n| (n * 37 + 11) as u8).collect();
let one_at_a_time = |data: &[u8]| {
let mut crc = !0u32;
for byte in data {
crc = CRC_TABLES[0][((crc ^ u32::from(*byte)) & 0xFF) as usize] ^ (crc >> 8);
}
!crc
};
for len in 0..bytes.len() {
let data = &bytes[..len];
assert_eq!(crc32(&[data]), one_at_a_time(data), "{len} bytes");
let (a, b) = data.split_at(len / 3);
assert_eq!(crc32(&[a, b]), one_at_a_time(data), "{len} bytes, split");
}
}
#[test]
fn the_number_is_written_in_the_room_before_the_message() {
let text = number_in_place(7, kept(r#"{"t":"x"}"#));
assert_eq!(&*text, r#"{"s":7,"t":"x"}"#);
assert_eq!(&*number_in_place(7, kept("{}")), r#"{"s":7 }"#);
assert_eq!(&*number_in_place(12, kept("{ }")), r#"{"s":12 }"#);
let most = number_in_place(u64::MAX, kept(r#"{"a":1}"#));
assert_eq!(&*most, format!(r#"{{"s":{},"a":1}}"#, u64::MAX));
assert_eq!(
&*number_in_place(3, r#"{"t":"x"}"#.to_owned()),
r#"{"s":3,"t":"x"}"#
);
for text in [&*text, &*most] {
serde_json::from_str::<serde_json::Value>(text).unwrap();
}
}
#[test]
fn numbering_keeps_the_json_whole() {
assert_eq!(numbered(7, r#"{"t":"x"}"#), r#"{"s":7,"t":"x"}"#);
assert_eq!(numbered(7, "{}"), r#"{"s":7}"#);
assert_eq!(numbered(18, r#" { "t":1}"#), r#"{"s":18, "t":1}"#);
assert_eq!(unnumbered(r#"{"s":7,"t":"x"}"#), r#"{"t":"x"}"#);
assert_eq!(unnumbered(r#"{"s":7}"#), "{}");
assert_eq!(numbered(u64::MAX, "{}"), format!(r#"{{"s":{}}}"#, u64::MAX));
}
#[cfg(not(target_arch = "wasm32"))]
mod files {
use super::*;
fn dir() -> std::path::PathBuf {
let dir = std::env::temp_dir().join(format!(
"wse-outbox-{}-{}",
std::process::id(),
fastrand::u64(..)
));
std::fs::create_dir_all(&dir).unwrap();
dir
}
#[test]
fn a_numbered_message_goes_once_the_file_holds_it_and_the_mark_that_it_may_have() {
let dir = dir();
let path = dir.join("outbox");
let mut outbox = outbox(&Offline::in_file(&path));
answering(&mut outbox, 0);
add(&mut outbox, "a");
let mut outcomes = Outcomes::default();
let mut went = Vec::new();
for _ in 0..1000 {
outbox.due(
0,
&mut outcomes,
|_, _| true,
|s, _| {
let file = std::fs::read_to_string(&path).unwrap();
assert!(
file.contains(&format!(r#"{{"written":{s}}}"#))
&& file.contains(&format!(r#"{{"add":{{"s":{s},"#)),
"it went before the file held it: {file}"
);
went.push(s);
},
);
if !went.is_empty() {
break;
}
std::thread::sleep(Duration::from_millis(2));
}
assert_eq!(went, [1]);
drop(outbox);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn what_is_kept_outlives_the_client_until_the_server_took_it() {
let dir = dir();
let offline = Offline::in_file(dir.join("queue"));
let producer = {
let mut outbox = outbox(&offline);
answering(&mut outbox, 0);
add(&mut outbox, "a");
add(&mut outbox, "b");
add(&mut outbox, "c");
let mut outcomes = Outcomes::default();
due(&mut outbox, 0, &mut outcomes);
outbox.acked(1);
outbox.producer.clone()
};
let mut outbox = outbox(&offline);
assert_eq!(outbox.producer, producer, "the same producer");
assert_eq!(outbox.len(), 2);
assert_eq!(outbox.hello()["written"], 3);
assert_eq!(outbox.hello()["node"], "n1");
assert_eq!(outbox.room.count(), 2);
let mut outcomes = Outcomes::default();
answering(&mut outbox, 1);
let out = due(&mut outbox, 0, &mut outcomes);
assert_eq!(
out,
[
(2, r#"{"s":2,"t":"m","p":{},"id":"b"}"#.to_owned()),
(3, r#"{"s":3,"t":"m","p":{},"id":"c"}"#.to_owned())
],
"the text as it was"
);
assert_eq!(add(&mut outbox, "d"), 4, "numbers go on");
drop(outbox);
std::fs::remove_dir_all(&dir).unwrap();
}
#[test]
fn a_number_is_never_used_twice() {
let dir = dir();
let offline = Offline::in_file(dir.join("queue"));
{
let mut outbox = outbox(&offline);
add(&mut outbox, "a");
add(&mut outbox, "b");
outbox.clear();
}
let mut outbox = outbox(&offline);
assert_eq!(outbox.len(), 0);
assert_eq!(add(&mut outbox, "c"), 3);
drop(outbox);
let mut outbox = self::outbox(&offline);
assert_eq!(add(&mut outbox, "d"), 4);
drop(outbox);
std::fs::remove_dir_all(&dir).unwrap();
}
#[test]
fn a_tail_that_does_not_check_is_not_read() {
let dir = dir();
let path = dir.join("queue");
let offline = Offline::in_file(&path);
{
let mut outbox = outbox(&offline);
add(&mut outbox, "a");
add(&mut outbox, "b");
}
let mut bytes = std::fs::read(&path).unwrap();
bytes.truncate(bytes.len() - 5);
bytes.extend_from_slice(&[0xff, 0xfe, 0x00, b'\n']);
std::fs::write(&path, &bytes).unwrap();
let outbox = outbox(&offline);
assert_eq!(outbox.len(), 1, "read up to the line cut short");
drop(outbox);
std::fs::remove_dir_all(&dir).unwrap();
}
#[test]
fn a_file_that_does_not_open_is_said_and_the_outbox_kept_in_memory() {
let dir = dir();
let offline = Offline::in_file(dir.join("queue"));
let first = outbox(&offline);
let said = Arc::new(std::sync::Mutex::new(Vec::new()));
let seen = Arc::clone(&said);
let report: Report = Arc::new(move |operation, why| {
seen.lock().unwrap().push((operation, why));
});
let mut second = Outbox::open(
&offline,
Arc::new(Room::new(&offline)),
report,
Arc::new(|| {}),
);
{
let said = said.lock().unwrap();
assert_eq!(said.len(), 1, "{said:?}");
assert_eq!(said[0].0, "open");
assert!(said[0].1.contains("another client's"), "{said:?}");
}
add(&mut second, "a");
assert_eq!(second.len(), 1, "in memory");
drop((first, second));
std::fs::remove_dir_all(&dir).unwrap();
}
#[test]
fn the_file_is_written_anew_once_what_went_outweighs_what_is_kept() {
let dir = dir();
let path = dir.join("queue");
let mut outbox = outbox(&Offline::in_file(&path));
answering(&mut outbox, 0);
let mut outcomes = Outcomes::default();
for round in 0..300 {
let s = add(&mut outbox, &format!("m{round}"));
due(&mut outbox, 0, &mut outcomes);
outbox.acked(s);
}
drop(outbox);
let lines = std::fs::read_to_string(&path).unwrap().lines().count();
assert!(lines < 80, "{lines} lines");
let outbox = self::outbox(&Offline::in_file(&path));
assert_eq!(outbox.len(), 0);
assert_eq!(outbox.next, 301);
drop(outbox);
std::fs::remove_dir_all(&dir).unwrap();
}
#[test]
fn the_shared_vector_is_written_and_read_alike() {
let vector: serde_json::Value =
serde_json::from_str(include_str!("../tests/vectors/outbox.json")).unwrap();
let salt = vector["salt"].as_str().unwrap().as_bytes();
let mut whole = Vec::new();
for line in vector["lines"].as_array().unwrap() {
let json = line["json"].as_str().unwrap();
let value: serde_json::Value = serde_json::from_str(json).unwrap();
let mut out = Vec::new();
match line["kind"].as_str().unwrap() {
"head" => {
let head = Head {
wse_outbox: 1,
salt: value["salt"].as_str().unwrap().into(),
producer: value["producer"].as_str().unwrap().into(),
next: value["next"].as_u64().unwrap(),
};
file::encode(&mut out, b"", &head);
}
"add" => {
let add = &value["add"];
let text = json
.split_once(",\"text\":")
.and_then(|(_, rest)| rest.strip_suffix("}}"))
.unwrap();
let record = Record {
s: add["s"].as_u64().unwrap(),
id: add["id"].as_str().unwrap().into(),
at: add["at"].as_u64().unwrap(),
until: add["until"].as_u64().unwrap(),
owned: add["owned"].as_bool().unwrap(),
requires: add["requires"].as_array().map(|topics| {
topics
.iter()
.map(|t| t.as_str().unwrap().to_owned())
.collect()
}),
text: text.into(),
size: 0,
tag: 0,
};
file::encode_add(&mut out, salt, &record);
}
"node" => file::encode(
&mut out,
salt,
&LineRef::Node(value["node"].as_str().unwrap()),
),
"written" => {
file::encode(
&mut out,
salt,
&LineRef::Written(value["written"].as_u64().unwrap()),
);
}
"user" => file::encode(&mut out, salt, &LineRef::User(value["user"].as_str())),
"ack" => file::encode(
&mut out,
salt,
&LineRef::Ack(value["ack"].as_u64().unwrap()),
),
"drop" => {
let numbers: Vec<u64> = value["drop"]
.as_array()
.unwrap()
.iter()
.map(|n| n.as_u64().unwrap())
.collect();
file::encode(&mut out, salt, &LineRef::Drop(&numbers));
}
kind => panic!("a line of kind {kind}"),
}
let expected = format!("{}\n", line["line"].as_str().unwrap());
assert_eq!(String::from_utf8(out.clone()).unwrap(), expected);
whole.extend_from_slice(&out);
}
let read = file::read(&whole).unwrap();
let state = &vector["state"];
assert_eq!(&*read.producer, state["producer"].as_str().unwrap());
assert_eq!(read.node.as_deref(), state["node"].as_str());
assert_eq!(read.written, state["written"].as_u64().unwrap());
assert_eq!(read.next, state["next"].as_u64().unwrap());
assert_eq!(read.user.as_deref(), state["user"].as_str());
type Row<'a> = (u64, &'a str, u64, u64, bool, Option<Vec<String>>, &'a str);
let records: Vec<Row<'_>> = read
.records
.iter()
.map(|r| {
let requires = r.requires.as_ref().map(|t| t.to_vec());
(r.s, &*r.id, r.at, r.until, r.owned, requires, &*r.text)
})
.collect();
let expected: Vec<Row<'_>> = state["records"]
.as_array()
.unwrap()
.iter()
.map(|r| {
(
r["s"].as_u64().unwrap(),
r["id"].as_str().unwrap(),
r["at"].as_u64().unwrap(),
r["until"].as_u64().unwrap(),
r["owned"].as_bool().unwrap(),
r["requires"]
.as_array()
.map(|t| t.iter().map(|t| t.as_str().unwrap().to_owned()).collect()),
r["text"].as_str().unwrap(),
)
})
.collect();
assert_eq!(records, expected);
for damaged in vector["damaged"].as_array().unwrap() {
let read = file::read(damaged["file"].as_str().unwrap().as_bytes());
let why = damaged["why"].as_str().unwrap();
match damaged["records"].as_array() {
None => assert!(read.is_none(), "{why}"),
Some(ids) => {
let read = read.unwrap();
let got: Vec<&str> = read.records.iter().map(|r| &*r.id).collect();
let want: Vec<&str> = ids.iter().map(|id| id.as_str().unwrap()).collect();
assert_eq!(got, want, "{why}");
assert_eq!(read.user.as_deref(), damaged["user"].as_str(), "{why}");
}
}
}
}
#[cfg(unix)]
#[test]
fn a_rewrite_owed_within_the_second_comes_on_the_deadline() {
use std::os::unix::fs::PermissionsExt;
let dir = dir();
let path = dir.join("queue");
let said = Arc::new(std::sync::Mutex::new(Vec::new()));
let seen = Arc::clone(&said);
let report: Report = Arc::new(move |operation, why| {
seen.lock().unwrap().push((operation, why));
});
let offline = Offline::in_file(&path);
let mut outbox = Outbox::open(
&offline,
Arc::new(Room::new(&offline)),
report,
Arc::new(|| {}),
);
std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o500)).unwrap();
if std::fs::write(dir.join("probe"), b"").is_ok() {
std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o700)).unwrap();
drop(outbox);
std::fs::remove_dir_all(&dir).unwrap();
return;
}
answering(&mut outbox, 0);
let mut outcomes = Outcomes::default();
for round in 0..100 {
let s = add(&mut outbox, &format!("m{round}"));
due(&mut outbox, 0, &mut outcomes);
outbox.acked(s);
}
for _ in 0..250 {
if !said.lock().unwrap().is_empty() {
break;
}
std::thread::sleep(Duration::from_millis(10));
}
add(&mut outbox, "a");
due(&mut outbox, 0, &mut outcomes);
std::thread::sleep(Duration::from_millis(200));
std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o700)).unwrap();
add(&mut outbox, "b");
due(&mut outbox, 0, &mut outcomes);
let at = outbox.deadline().expect("a deadline for the rewrite owed");
assert!(at <= Instant::now() + Duration::from_secs(1));
std::thread::sleep(at.saturating_duration_since(Instant::now()));
outbox.heal(Instant::now());
let mut both = false;
for _ in 0..250 {
let text = std::fs::read_to_string(&path).unwrap_or_default();
if text.contains(r#""id":"a""#) && text.contains(r#""id":"b""#) {
both = true;
break;
}
std::thread::sleep(Duration::from_millis(10));
}
assert!(both, "the file written anew with a and b");
assert_eq!(outbox.heal, None, "nothing owed any more");
drop(outbox);
std::fs::remove_dir_all(&dir).unwrap();
}
#[cfg(unix)]
#[test]
fn a_file_that_cannot_be_written_says_so_and_heals() {
use std::os::unix::fs::PermissionsExt;
let dir = dir();
let path = dir.join("queue");
let said = Arc::new(std::sync::Mutex::new(Vec::new()));
let seen = Arc::clone(&said);
let report: Report = Arc::new(move |operation, why| {
seen.lock().unwrap().push((operation, why));
});
let offline = Offline::in_file(&path);
let mut outbox = Outbox::open(
&offline,
Arc::new(Room::new(&offline)),
report,
Arc::new(|| {}),
);
std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o500)).unwrap();
if std::fs::write(dir.join("probe"), b"").is_ok() {
std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o700)).unwrap();
drop(outbox);
std::fs::remove_dir_all(&dir).unwrap();
return;
}
answering(&mut outbox, 0);
let mut outcomes = Outcomes::default();
for round in 0..100 {
let s = add(&mut outbox, &format!("m{round}"));
due(&mut outbox, 0, &mut outcomes);
outbox.acked(s);
}
add(&mut outbox, "kept");
due(&mut outbox, 0, &mut outcomes);
for _ in 0..250 {
if !said.lock().unwrap().is_empty() {
break;
}
std::thread::sleep(Duration::from_millis(10));
}
assert_eq!(said.lock().unwrap().len(), 1, "{:?}", said.lock().unwrap());
assert_eq!(said.lock().unwrap()[0].0, "write");
std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o700)).unwrap();
outbox.disconnected();
add(&mut outbox, "after");
drop(outbox);
let outbox = self::outbox(&offline);
assert_eq!(outbox.len(), 2, "kept and after, written anew");
drop(outbox);
std::fs::remove_dir_all(&dir).unwrap();
}
}
}