use std::collections::HashMap;
use std::hash::{BuildHasher, Hasher, RandomState};
use std::panic::{AssertUnwindSafe, catch_unwind};
use std::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering};
use std::sync::{Arc, Condvar, Mutex, MutexGuard, PoisonError};
use std::time::{Duration, Instant, SystemTime};
use super::Route;
#[cfg(feature = "backup")]
use super::backup;
#[cfg(feature = "blobs")]
use super::blobs;
#[cfg(feature = "channels")]
use super::channels;
use super::codec::Message;
#[cfg(feature = "config")]
use super::config;
use super::frame::{self, Frame, Kind};
#[cfg(feature = "jobs")]
use super::jobs;
use super::kv::{self, Run};
#[cfg(feature = "locks")]
use super::locks;
#[cfg(feature = "metrics")]
use super::metrics;
use super::output::Output;
use super::protocol::method::{self, Access};
use super::protocol::{Empty, Failure, GoAway, Hello, ServerClock, Welcome};
#[cfg(feature = "records")]
use super::records;
#[cfg(feature = "search")]
use super::search;
#[cfg(feature = "sql")]
use super::sql;
#[cfg(feature = "sql")]
use crate::Transaction;
use crate::clock::from_unix_millis;
use crate::engine::SharedFile;
use crate::kv::Handed;
use crate::limits::{ANSWER_BYTES, Limit};
use crate::pipe::{Capability, Connect};
use crate::pool::Pool;
use crate::sync::lock;
use crate::{CancellationToken, Clock, Result, Store, unix_millis};
const PROTOCOL: u64 = 1;
const MAX_BODY: u64 = 4 << 20;
const IN_FLIGHT: u64 = 1024;
pub(super) const CONNECTION_CREDIT: u64 = 8 << 20;
const STREAM_CREDIT: u64 = 2 << 20;
const CLIENT_STREAM_CREDIT: u64 = 2 << 20;
const HELLO_MOST: usize = 1 << 16;
const CHALLENGE: usize = 16;
pub(super) const OUTPUT_MOST: usize = 8 << 20;
const OUTPUT_LOW: usize = 1 << 20;
const CROWD: usize = 6;
pub type Wake = Arc<dyn Fn() + Send + Sync>;
pub(crate) struct Session {
shared: Arc<Shared>,
}
struct Shared {
store: Store,
workers: Arc<Pool>,
kv: kv::Handles,
#[cfg(feature = "config")]
config: config::Configs,
#[cfg(feature = "locks")]
locks: Arc<locks::Holds>,
#[cfg(feature = "channels")]
channels: channels::Subscriptions,
#[cfg(feature = "jobs")]
jobs: jobs::Handles,
#[cfg(feature = "sql")]
sql: sql::Handles,
#[cfg(feature = "search")]
search: search::Handles,
#[cfg(feature = "blobs")]
blobs: blobs::Handles,
#[cfg(feature = "records")]
records: records::Handles,
#[cfg(feature = "metrics")]
metrics: metrics::Handles,
#[cfg(feature = "backup")]
backups: backup::Backups,
input: Mutex<Input>,
output: Mutex<Output>,
runs: Mutex<HashMap<u32, ClientRun>>,
reads: Mutex<HashMap<u32, CancellationToken>>,
asked: AtomicU64,
open: Mutex<HashMap<u32, OpenStream>>,
outstanding: AtomicU64,
to_grant: AtomicU64,
agreed: AtomicUsize,
reads_away: AtomicUsize,
read_only: AtomicBool,
owing: AtomicUsize,
held: AtomicBool,
waiting_output: Mutex<Vec<Box<dyn FnOnce() + Send>>>,
ready: Condvar,
wake: Mutex<Option<Wake>>,
connect: Connect,
}
struct OpenStream {
left: u64,
held: u64,
}
enum ClientRun {
Asking(u64),
Handed(Handed),
}
#[derive(Default)]
struct Input {
reader: frame::Reader,
max_body: Option<usize>,
stream_credit: u64,
going_away: bool,
capability: Option<Capability>,
ended: bool,
crowd: bool,
}
impl Session {
pub(crate) fn new(store: Store, mut connect: Connect) -> Result<Session> {
let workers = Pool::of(&store)?;
let wake = Mutex::new(connect.wake.take());
let shared = Shared {
store,
workers,
kv: kv::Handles::default(),
#[cfg(feature = "config")]
config: config::Configs::default(),
#[cfg(feature = "locks")]
locks: Arc::default(),
#[cfg(feature = "channels")]
channels: channels::Subscriptions::default(),
#[cfg(feature = "jobs")]
jobs: jobs::Handles::default(),
#[cfg(feature = "sql")]
sql: sql::Handles::default(),
#[cfg(feature = "search")]
search: search::Handles::default(),
#[cfg(feature = "blobs")]
blobs: blobs::Handles::default(),
#[cfg(feature = "records")]
records: records::Handles::default(),
#[cfg(feature = "metrics")]
metrics: metrics::Handles::default(),
#[cfg(feature = "backup")]
backups: backup::Backups::default(),
input: Mutex::new(Input::default()),
output: Mutex::new(Output::default()),
runs: Mutex::new(HashMap::new()),
reads: Mutex::new(HashMap::new()),
asked: AtomicU64::new(0),
open: Mutex::new(HashMap::new()),
outstanding: AtomicU64::new(0),
to_grant: AtomicU64::new(0),
agreed: AtomicUsize::new(HELLO_MOST),
reads_away: AtomicUsize::new(0),
read_only: AtomicBool::new(false),
owing: AtomicUsize::new(0),
held: AtomicBool::new(false),
waiting_output: Mutex::new(Vec::new()),
ready: Condvar::new(),
wake,
connect,
};
shared.store.probe().shift("tinystore_wire_connections", &[], 1.0);
Ok(Session { shared: Arc::new(shared) })
}
pub(crate) fn receive(&self, bytes: &[u8]) {
let mut input = self.shared.lock_input();
if input.ended {
return;
}
input.reader.push(bytes);
input.crowd = input.reader.holds(CROWD);
self.shared.read_frames(&mut input);
}
pub(crate) fn full(&self) -> bool {
self.shared.held.load(Ordering::SeqCst)
}
pub(crate) fn take_ready(&self) -> Vec<u8> {
let mut output = self.shared.lock_output();
let taken = output.take_all();
self.took(output);
taken
}
pub(crate) fn take_ready_into(&self, into: &mut [u8]) -> usize {
let mut output = self.shared.lock_output();
let taken = output.take_into(into);
self.took(output);
taken
}
pub(crate) fn take(&self, wait: Duration) -> Vec<u8> {
let mut output = self.awaited(wait);
let taken = output.take_all();
self.took(output);
taken
}
pub(crate) fn take_into(&self, wait: Duration, into: &mut [u8]) -> usize {
let mut output = self.awaited(wait);
let taken = output.take_into(into);
self.took(output);
taken
}
fn took(&self, output: MutexGuard<'_, Output>) {
self.shared.owing.store(output.owed(), Ordering::SeqCst);
drop(output);
self.shared.drained();
self.shared.read_held();
}
fn awaited(&self, wait: Duration) -> MutexGuard<'_, Output> {
let deadline = Instant::now() + wait;
let mut output = self.shared.lock_output();
while output.owes_nothing() && !output.ended {
let left = deadline.saturating_duration_since(Instant::now());
if left.is_zero() {
break;
}
output.awaited = true;
output = self.shared.ready.wait_timeout(output, left).unwrap_or_else(PoisonError::into_inner).0;
}
output.awaited = false;
output.woken = false;
output
}
pub(crate) fn go_away(&self) {
let mut input = self.shared.lock_input();
if input.ended || input.going_away {
return;
}
input.going_away = true;
let body = GoAway { code: "unavailable".to_owned(), message: "the server is closing".to_owned() }.encode();
self.shared.send(&[Frame::new(Kind::GoAway, 0, body)]);
self.end_open("the server is closing");
}
pub(crate) fn client_closed(&self) {
self.end_open("the client closed its side");
}
fn end_open(&self, why: &str) {
#[cfg(feature = "jobs")]
{
self.shared.jobs.halt_works();
for watch in self.shared.jobs.take_watches() {
watch.close(Err(Failure::unavailable(format!("{why}; watch again on another connection"))));
}
}
#[cfg(feature = "records")]
self.shared.records.go_away(why);
#[cfg(feature = "config")]
self.shared.config.go_away(why);
#[cfg(feature = "channels")]
self.shared.channels.go_away(why);
let _ = why;
}
pub(crate) fn streams(&self) -> usize {
self.shared.lock_open().len()
}
pub(crate) fn welcomed(&self) -> bool {
self.shared.lock_input().max_body.is_some()
}
pub(crate) fn finished(&self) -> bool {
let output = self.shared.lock_output();
output.ended && output.owes_nothing()
}
pub(crate) fn end(&self) {
self.shared.lock_input().ended = true;
let mut output = self.shared.lock_output();
output.ended = true;
output.clear();
self.shared.owing.store(0, Ordering::SeqCst);
drop(output);
self.shared.ready.notify_all();
lock(&self.shared.wake).take();
let waiting = std::mem::take(&mut *lock(&self.shared.waiting_output));
drop(waiting);
let runs = std::mem::take(&mut *self.shared.lock_runs());
drop(runs);
let reads: Vec<CancellationToken> = lock(&self.shared.reads).drain().map(|(_, read)| read).collect();
reads.iter().for_each(CancellationToken::cancel);
#[cfg(feature = "jobs")]
{
for watch in self.shared.jobs.take_watches() {
watch.end();
}
for work in self.shared.jobs.take_works() {
work.end();
}
}
#[cfg(feature = "sql")]
self.shared.sql.end();
#[cfg(feature = "blobs")]
self.shared.blobs.end();
#[cfg(feature = "records")]
self.shared.records.end();
#[cfg(feature = "metrics")]
self.shared.metrics.end();
#[cfg(feature = "config")]
self.shared.config.end();
#[cfg(feature = "locks")]
self.shared.locks.end();
#[cfg(feature = "channels")]
self.shared.channels.end();
#[cfg(feature = "backup")]
self.shared.backups.end_all("the connection is gone");
}
}
impl Shared {
fn read_frames(self: &Arc<Self>, input: &mut Input) {
while !input.ended {
if input.max_body.is_some() && self.owes_past_bound() {
return;
}
let max_body = input.max_body.unwrap_or(HELLO_MOST);
match input.reader.next(max_body) {
Ok(Some(frame)) => self.handle(input, frame),
Ok(None) => return,
Err(refused) => self.go_away(input, "protocol", &refused.0),
}
}
}
fn owes_past_bound(&self) -> bool {
if self.owing.load(Ordering::SeqCst) > OUTPUT_MOST {
self.held.store(true, Ordering::SeqCst);
if self.owing.load(Ordering::SeqCst) > OUTPUT_MOST {
return true;
}
}
if self.held.load(Ordering::Relaxed) {
self.held.store(false, Ordering::SeqCst);
}
false
}
fn read_held(self: &Arc<Self>) {
if !self.held.load(Ordering::SeqCst) || self.owing.load(Ordering::SeqCst) > OUTPUT_LOW {
return;
}
let mut input = self.lock_input();
self.read_frames(&mut input);
}
fn handle(self: &Arc<Self>, input: &mut Input, frame: Frame) {
match (input.max_body, frame.kind) {
(None, Kind::Hello) => self.welcome(input, &frame.body),
(None, kind) => self.go_away(input, "protocol", &format!("a {kind:?} before HELLO")),
(Some(_), Kind::Request) => self.request(input, frame),
(Some(_), Kind::Data) => self.data(input, frame),
(Some(_), Kind::Cancel) => self.cancel(frame.stream),
(Some(_), Kind::Ping) => self.send(&[Frame::new(Kind::Pong, 0, frame.body)]),
(Some(_), Kind::Credit) => self.credit(&frame),
(Some(_), Kind::GoAway) => self.finish(input),
(Some(_), kind) => self.go_away(input, "protocol", &format!("a {kind:?} from a client")),
}
}
fn welcome(&self, input: &mut Input, body: &[u8]) {
let hello = match Hello::decode(body) {
Ok(hello) => hello,
Err(failure) => return self.go_away(input, "protocol", &failure.message),
};
if hello.protocol != PROTOCOL {
let refused = format!("protocol {}, where this server speaks {PROTOCOL}", hello.protocol);
return self.go_away(input, "protocol", &refused);
}
if let Some(length) = hello.challenge.as_ref().map(Vec::len).filter(|length| *length != CHALLENGE) {
let refused = format!("a challenge of {length} bytes, not {CHALLENGE}");
return self.go_away(input, "protocol", &refused);
}
let capability = match &self.connect.admit {
None => Capability::Admin,
Some(admit) => match admit(hello.token.as_deref()) {
Some(capability) => capability,
None => return self.go_away(input, "unauthenticated", "no token this server knows"),
},
};
input.capability = Some(capability);
self.read_only.store(capability == Capability::Read, Ordering::Relaxed);
let client_most = hello.max_body.unwrap_or(MAX_BODY);
let stream_credit = hello.stream_credit.unwrap_or(CLIENT_STREAM_CREDIT);
let max_body = MAX_BODY.min(client_most).min(stream_credit);
input.max_body = Some(max_body as usize);
self.agreed.store(max_body as usize, Ordering::Release);
input.stream_credit = stream_credit;
let proof = hello.challenge.zip(self.connect.prove.as_ref()).map(|(challenge, prove)| prove(&challenge));
self.send(&[Frame::new(Kind::Welcome, 0, self.welcome_body(max_body, capability, proof))]);
if let Some(greeted) = &self.connect.greeted {
greeted(&hello.client);
}
}
fn welcome_body(&self, max_body: u64, capability: Capability, proof: Option<Vec<u8>>) -> Vec<u8> {
let capability = match capability {
Capability::Admin => "admin",
Capability::Data => "data",
Capability::Read => "read",
};
let welcome = Welcome {
protocol: PROTOCOL,
server: env!("CARGO_PKG_VERSION").to_owned(),
instance: instance(),
capability: capability.to_owned(),
max_body,
in_flight: IN_FLIGHT,
connection_credit: CONNECTION_CREDIT,
stream_credit: STREAM_CREDIT,
engines: engines(),
now: unix_millis(self.store.now()),
proof,
durability: self.store.durability().map(|durability| durability.to_string()),
};
welcome.encode()
}
fn request(self: &Arc<Self>, input: &mut Input, frame: Frame) {
let opened = self.receive(frame.body.len() as u64).and_then(|()| self.open_stream(frame.stream, &frame.body));
if let Err(refused) = opened {
return self.go_away(input, "protocol", &refused);
}
if input.going_away {
let closing = Failure::unavailable("the server is closing; ask another connection");
return self.answer(frame.stream, Err(closing));
}
if let Some(refused) = refused_to(input.capability, frame.method) {
return self.answer(frame.stream, Err(Failure::permission(refused)));
}
if frame.method >> 8 == 0x00 {
return self.server_call(frame.stream, frame.method, &frame.body);
}
self.route_call(input, frame);
}
fn route_call(self: &Arc<Self>, input: &mut Input, frame: Frame) {
let max_body = input.max_body.unwrap_or(HELLO_MOST);
let (method, stream, body) = (frame.method, frame.stream, frame.body);
if self.store.probe().listening() {
self.store.probe().count("tinystore_wire_requests_total", &[("method", method_name(method))], 1.0);
}
let route = route(method);
if route == Route::Inline && (input.crowd || self.reads_away.load(Ordering::Acquire) > 0) {
return self.read_away(stream, method, body, max_body);
}
match route {
Route::Inline => self.answer(stream, guarded(|| self.call(method, &body, max_body))),
Route::Submit => self.submit(stream, method, body),
Route::Handover => self.hand_over(stream, body),
#[cfg(feature = "jobs")]
Route::Exchange => self.exchange(input, stream, &body),
#[cfg(feature = "jobs")]
Route::Watch => self.watch(input, stream, &body),
#[cfg(feature = "sql")]
Route::Download => self.download(input, stream, method, body),
#[cfg(feature = "sql")]
Route::Transaction => self.transaction(input, stream, &body),
#[cfg(feature = "blobs")]
Route::Read => self.read(input, stream, body),
#[cfg(feature = "blobs")]
Route::Upload => self.upload(input, stream, &body),
#[cfg(feature = "records")]
Route::Tail => self.tail(input, stream, &body),
#[cfg(feature = "records")]
Route::Lines => self.lines(input, stream, &body),
#[cfg(feature = "metrics")]
Route::Parts => self.parts(input, stream, method, body),
#[cfg(feature = "metrics")]
Route::Instruments => self.instruments(input, stream, &body),
#[cfg(feature = "metrics")]
Route::Gather => self.gather(input, stream, &body),
#[cfg(feature = "config")]
Route::Follow => self.follow(input, stream, &body),
#[cfg(feature = "backup")]
Route::Backup => self.backup(input, stream, &body),
#[cfg(feature = "locks")]
Route::Hold => self.hold(stream, &body),
#[cfg(feature = "channels")]
Route::Subscribe => self.subscribe(input, stream, &body),
Route::Worker => {
let (shared, reading) = (Arc::clone(self), self.reading(stream, method));
let writes = method::access(method) == Some(Access::Write);
self.run_call(Box::new(move || {
let answered =
shared.read_under(stream, reading, || guarded(|| shared.call(method, &body, max_body)));
shared.answer(stream, if writes { shared.written_within_agreed(answered) } else { answered });
}));
}
}
}
fn submit(self: &Arc<Self>, stream: u32, method: u16, body: Vec<u8>) {
let shared = Arc::clone(self);
self.run_call(Box::new(move || {
let answering = Arc::clone(&shared);
let done = move |answered| answering.answer(stream, answering.written_within_agreed(answered));
let queued = guarded(|| {
match method >> 8 {
#[cfg(feature = "jobs")]
0x02 => jobs::submit(&shared.jobs, method, &body, done),
#[cfg(feature = "sql")]
0x03 => sql::submit(&shared.sql, (method, &body), &|called, body| shared.lend(called, body), done),
_ => kv::submit(&shared.kv, method, &body, done),
}
Ok(())
});
if let Err(failure) = queued {
shared.answer(stream, Err(failure));
}
}));
}
fn hand_over(self: &Arc<Self>, stream: u32, body: Vec<u8>) {
let asked = self.asked.fetch_add(1, Ordering::Relaxed);
self.lock_runs().insert(stream, ClientRun::Asking(asked));
let shared = Arc::clone(self);
self.run_call(Box::new(move || shared.run_once(stream, asked, body, None)));
}
fn read_away(self: &Arc<Self>, stream: u32, method: u16, body: Vec<u8>, max_body: usize) {
self.reads_away.fetch_add(1, Ordering::AcqRel);
let shared = Arc::clone(self);
self.run_call(Box::new(move || {
let answered = guarded(|| shared.call(method, &body, max_body));
shared.reads_away.fetch_sub(1, Ordering::AcqRel);
shared.answer(stream, answered);
}));
}
fn run_call(self: &Arc<Self>, call: Box<dyn FnOnce() + Send>) {
let shared = Arc::clone(self);
self.workers.run(Box::new(move || shared.start_call(call)));
}
fn start_call(self: &Arc<Self>, call: Box<dyn FnOnce() + Send>) {
let owing = self.owing.load(Ordering::SeqCst);
if owing <= OUTPUT_LOW || (owing <= OUTPUT_MOST && lock(&self.waiting_output).is_empty()) {
return call();
}
let shared = Arc::clone(self);
self.when_drained(Box::new(move || shared.start_call(call)));
}
#[cfg(feature = "channels")]
fn subscribe(self: &Arc<Self>, input: &Input, stream: u32, body: &[u8]) {
let link = self.stream_link(input, stream);
match guarded(|| channels::subscribe(&self.store, &self.channels, stream, body, link)) {
Ok(subscribed) => {
self.send(&[Frame::new(Kind::Response, stream, Empty {}.encode())]);
subscribed.begin();
}
Err(failure) => self.answer(stream, Err(failure)),
}
}
#[cfg(feature = "locks")]
fn hold(self: &Arc<Self>, stream: u32, body: &[u8]) {
let answering = Arc::downgrade(self);
let answer: locks::Answering = Box::new(move |answered| {
if let Some(shared) = answering.upgrade() {
shared.answer(stream, answered);
}
});
if let Err(failure) = guarded(|| locks::hold(&self.store, &self.locks, stream, body, answer)) {
self.locks.cancel(stream);
self.answer(stream, Err(failure));
}
}
#[cfg(feature = "jobs")]
fn exchange(self: &Arc<Self>, input: &Input, stream: u32, body: &[u8]) {
match guarded(|| jobs::work(&self.jobs, stream, body, self.link(input, stream))) {
Ok(work) => {
self.send(&[Frame::new(Kind::Response, stream, Empty {}.encode())]);
work.begin();
}
Err(failure) => self.answer(stream, Err(failure)),
}
}
#[cfg(feature = "jobs")]
fn watch(self: &Arc<Self>, input: &Input, stream: u32, body: &[u8]) {
match guarded(|| jobs::watch(&self.jobs, stream, body, self.link(input, stream))) {
Ok(watch) => {
self.send(&[Frame::new(Kind::Response, stream, Empty {}.encode())]);
watch.begin();
}
Err(failure) => self.answer(stream, Err(failure)),
}
}
#[cfg(feature = "jobs")]
fn link(self: &Arc<Self>, input: &Input, stream: u32) -> jobs::Link {
let (sending, finishing, spawning) = (Arc::downgrade(self), Arc::downgrade(self), Arc::downgrade(self));
let send: jobs::Sender = Arc::new(move |body| {
if let Some(shared) = sending.upgrade() {
shared.send(&[Frame::new(Kind::Data, stream, body)]);
}
});
let finish: jobs::Finisher = Arc::new(move |last| {
if let Some(shared) = finishing.upgrade() {
shared.end_stream(stream, last);
}
});
let spawn: jobs::Spawn = Arc::new(move |task| {
if let Some(shared) = spawning.upgrade() {
shared.workers.run(task);
}
});
let max_body = input.max_body.unwrap_or(HELLO_MOST);
jobs::Link { send, finish, spawn, credit: input.stream_credit, max_body }
}
#[cfg(feature = "sql")]
fn download(self: &Arc<Self>, input: &Input, stream: u32, called: u16, body: Vec<u8>) {
let link = self.sql_link(input, stream);
let (shared, reading) = (Arc::clone(self), self.reading(stream, called));
self.run_call(Box::new(move || {
let (answering, sending) = (Arc::clone(&shared), link.clone());
let done = move |queried: std::result::Result<sql::Queried, Failure>| match queried {
Ok(sql::Queried::Whole(rows)) => answering.answer(stream, Ok(rows)),
Ok(sql::Queried::Parts(parts, held)) => {
answering.send(&[Frame::new(Kind::Response, stream, Empty {}.encode())]);
sql::download(&answering.sql, stream, (parts, held), &sending);
}
Err(failure) => answering.answer(stream, Err(failure)),
};
let queued = shared.read_under(stream, reading, || {
guarded(|| {
let read_only = shared.read_only.load(Ordering::Relaxed);
sql::query(&shared.sql, (called, &body), &link, read_only, done);
Ok(())
})
});
if let Err(failure) = queued {
shared.answer(stream, Err(failure));
}
}));
}
#[cfg(feature = "sql")]
fn transaction(self: &Arc<Self>, input: &Input, stream: u32, body: &[u8]) {
let link = self.sql_link(input, stream);
match guarded(|| sql::transaction(&self.sql, stream, body, &link)) {
Ok((txing, running)) => {
self.send(&[Frame::new(Kind::Response, stream, Empty {}.encode())]);
let (shared, line) = (Arc::clone(self), running.line());
let turn = Box::new(move || {
let nested = |tx: &Transaction<'_>, method, body: &[u8], max_body| {
shared.call_in(tx, method, body, max_body)
};
txing.run(running, &shared.sql, stream, &nested);
});
self.workers.run_in_line(&line, turn);
}
Err(failure) => self.answer(stream, Err(failure)),
}
}
#[cfg(feature = "blobs")]
fn read(self: &Arc<Self>, input: &Input, stream: u32, body: Vec<u8>) {
let link = self.stream_link(input, stream);
let shared = Arc::clone(self);
self.run_call(Box::new(move || match guarded(|| blobs::read(&shared.blobs, stream, &body, &link)) {
Ok(blobs::Reading::Whole(response)) => shared.answer(stream, Ok(response)),
Ok(blobs::Reading::Begun(response, getting)) => {
shared.queue_moved(Frame::new(Kind::Response, stream, response), None);
getting.pump();
}
Err(failure) => shared.answer(stream, Err(failure)),
}));
}
#[cfg(feature = "blobs")]
fn upload(self: &Arc<Self>, input: &Input, stream: u32, body: &[u8]) {
let link = self.stream_link(input, stream);
match guarded(|| blobs::upload(&self.blobs, stream, body, &link)) {
Ok(()) => self.send(&[Frame::new(Kind::Response, stream, Empty {}.encode())]),
Err(failure) => self.answer(stream, Err(failure)),
}
}
#[cfg(feature = "records")]
fn tail(self: &Arc<Self>, input: &Input, stream: u32, body: &[u8]) {
let link = self.stream_link(input, stream);
match guarded(|| records::tail(&self.store, &self.records, stream, body, link)) {
Ok(tailing) => {
self.send(&[Frame::new(Kind::Response, stream, Empty {}.encode())]);
tailing.begin();
}
Err(failure) => self.answer(stream, Err(failure)),
}
}
#[cfg(feature = "records")]
fn lines(self: &Arc<Self>, input: &Input, stream: u32, body: &[u8]) {
let link = self.stream_link(input, stream);
match guarded(|| records::lines(&self.records, stream, body, link)) {
Ok(()) => self.send(&[Frame::new(Kind::Response, stream, Empty {}.encode())]),
Err(failure) => self.answer(stream, Err(failure)),
}
}
#[cfg(feature = "metrics")]
fn parts(self: &Arc<Self>, input: &Input, stream: u32, called: u16, body: Vec<u8>) {
let link = self.stream_link(input, stream);
let (shared, reading) = (Arc::clone(self), self.reading(stream, called));
self.run_call(Box::new(move || {
let bound = super::part_bound(link.max_body, link.credit, 0);
let parted =
shared.read_under(stream, reading, || guarded(|| metrics::parts(&shared.store, called, &body, bound)));
match parted {
Ok(mut parts) if parts.len() == 1 => shared.answer(stream, Ok(parts.pop_front().unwrap_or_default())),
Ok(parts) => {
shared.send(&[Frame::new(Kind::Response, stream, Empty {}.encode())]);
metrics::download(&shared.metrics, stream, parts, link);
}
Err(failure) => shared.answer(stream, Err(failure)),
}
}));
}
#[cfg(feature = "metrics")]
fn instruments(self: &Arc<Self>, input: &Input, stream: u32, body: &[u8]) {
let link = self.stream_link(input, stream);
match guarded(|| metrics::instruments(&self.store, &self.metrics, stream, body, link)) {
Ok(response) => self.send(&[Frame::new(Kind::Response, stream, response)]),
Err(failure) => self.answer(stream, Err(failure)),
}
}
#[cfg(feature = "metrics")]
fn gather(self: &Arc<Self>, input: &Input, stream: u32, body: &[u8]) {
let link = self.stream_link(input, stream);
match guarded(|| metrics::gather(&self.store, &self.metrics, stream, body, link)) {
Ok(gathering) => {
self.send(&[Frame::new(Kind::Response, stream, Empty {}.encode())]);
gathering.begin();
}
Err(failure) => self.answer(stream, Err(failure)),
}
}
#[cfg(feature = "config")]
fn follow(self: &Arc<Self>, input: &Input, stream: u32, body: &[u8]) {
let link = self.stream_link(input, stream);
match guarded(|| config::follow(&self.config, stream, body, link)) {
Ok(following) => {
self.send(&[Frame::new(Kind::Response, stream, Empty {}.encode())]);
following.begin();
}
Err(failure) => self.answer(stream, Err(failure)),
}
}
#[cfg(feature = "backup")]
fn backup(self: &Arc<Self>, input: &Input, stream: u32, body: &[u8]) {
let link = self.stream_link(input, stream);
match guarded(|| backup::asked(&self.backups, stream, body, link)) {
Ok(sending) => {
self.send(&[Frame::new(Kind::Response, stream, Empty {}.encode())]);
sending.begin(&self.store, &self.backups);
}
Err(failure) => self.answer(stream, Err(failure)),
}
}
#[cfg(any(
feature = "blobs",
feature = "records",
feature = "metrics",
feature = "config",
feature = "backup",
feature = "channels"
))]
fn stream_link(self: &Arc<Self>, input: &Input, stream: u32) -> super::Link {
let (sending, finishing, granting, spawning) =
(Arc::downgrade(self), Arc::downgrade(self), Arc::downgrade(self), Arc::downgrade(self));
super::Link {
send: Arc::new(move |body| {
if let Some(shared) = sending.upgrade() {
shared.queue_moved(Frame::new(Kind::Data, stream, body), None);
}
}),
finish: Arc::new(move |last| {
if let Some(shared) = finishing.upgrade() {
shared.end_stream(stream, last);
}
}),
grant: Arc::new(move |bytes: u32| {
if let Some(shared) = granting.upgrade() {
shared.send(&[Frame::new(Kind::Credit, stream, bytes.to_le_bytes().to_vec())]);
}
}),
spawn: Arc::new(move |task| {
if let Some(shared) = spawning.upgrade() {
shared.workers.run(task);
}
}),
credit: input.stream_credit,
max_body: input.max_body.unwrap_or(HELLO_MOST),
#[cfg(feature = "channels")]
owed: {
let owing = Arc::downgrade(self);
Arc::new(move || owing.upgrade().map_or(0, |shared| shared.owed()))
},
#[cfg(feature = "channels")]
when_drained: {
let draining = Arc::downgrade(self);
Arc::new(move |task| {
if let Some(shared) = draining.upgrade() {
shared.when_drained(task);
}
})
},
}
}
#[cfg(feature = "sql")]
fn sql_link(self: &Arc<Self>, input: &Input, stream: u32) -> sql::Link {
let (sending, finishing) = (Arc::downgrade(self), Arc::downgrade(self));
sql::Link {
send: Arc::new(move |body| {
if let Some(shared) = sending.upgrade() {
shared.send(&[Frame::new(Kind::Data, stream, body)]);
}
}),
finish: Arc::new(move |last| {
if let Some(shared) = finishing.upgrade() {
shared.end_stream(stream, last);
}
}),
credit: input.stream_credit,
max_body: input.max_body.unwrap_or(HELLO_MOST),
}
}
fn run_once(self: &Arc<Self>, stream: u32, asked: u64, body: Vec<u8>, unkept: Option<Arc<crate::kv::Raw>>) {
if !still_asking(&self.lock_runs(), stream, asked) {
return;
}
let started = match unkept {
Some(raw) => Ok(kv::unkept(&raw)),
None => {
let again = {
let (shared, body) = (Arc::clone(self), body.clone());
Box::new(move |unkept| {
let asking = Arc::clone(&shared);
shared.workers.run(Box::new(move || asking.run_once(stream, asked, body, unkept)));
})
};
catch_unwind(AssertUnwindSafe(|| kv::run(&self.kv, &body, again)))
.unwrap_or_else(|_| Err(Failure::internal("a call panicked")))
}
};
let mut runs = self.lock_runs();
if !still_asking(&runs, stream, asked) {
drop(runs);
drop(started);
return;
}
match started {
Ok(Run::Answered(answer)) => {
runs.remove(&stream);
self.answer(stream, Ok(answer));
}
Ok(Run::Handed(handed, answer)) => {
runs.insert(stream, ClientRun::Handed(*handed));
self.send(&[Frame::new(Kind::Response, stream, answer)]);
}
Ok(Run::Waiting) => {}
Err(failure) => {
runs.remove(&stream);
self.answer(stream, Err(failure));
}
}
}
fn data(self: &Arc<Self>, input: &mut Input, frame: Frame) {
let bytes = frame.body.len() as u64;
if let Err(refused) = self.receive(bytes).and_then(|()| self.spend_stream_credit(frame.stream, bytes)) {
return self.go_away(input, "protocol", &refused);
}
#[cfg(feature = "blobs")]
if let Some(uploading) = self.blobs.uploading(frame.stream) {
let last = frame.flags & frame::END != 0;
return uploading.piece(frame.body, last);
}
#[cfg(feature = "records")]
if self.records.piece(frame.stream, &frame.body, frame.flags & frame::END != 0) {
return;
}
#[cfg(feature = "metrics")]
if let Some(gathering) = self.metrics.gathering(frame.stream) {
return gathering.piece(frame.body, frame.flags & frame::END != 0);
}
#[cfg(feature = "metrics")]
if self.metrics.batch(frame.stream, &frame.body, frame.flags & frame::END != 0) {
return;
}
#[cfg(feature = "sql")]
if let Some(tx) = self.sql.tx(frame.stream) {
if let Ok(read) = u32::try_from(frame.body.len()) {
self.send(&[Frame::new(Kind::Credit, frame.stream, read.to_le_bytes().to_vec())]);
}
return tx.item(&frame.body, frame.flags & frame::END != 0);
}
#[cfg(feature = "jobs")]
if let Some(work) = self.jobs.work(frame.stream) {
return self.work_data(&work, &frame);
}
if frame.flags & frame::END == 0 {
return;
}
let handed = {
let mut runs = self.lock_runs();
match runs.remove(&frame.stream) {
Some(ClientRun::Handed(handed)) => handed,
Some(asking) => {
runs.insert(frame.stream, asking);
return;
}
None => return,
}
};
let (shared, stream, body) = (Arc::clone(self), frame.stream, frame.body);
self.workers.run(Box::new(move || {
let kept = guarded(|| kv::kept(handed, &body));
shared.end_stream(stream, kept);
}));
}
#[cfg(feature = "jobs")]
fn work_data(self: &Arc<Self>, work: &jobs::Work, frame: &Frame) {
self.let_go_of(frame.stream, frame.body.len() as u64);
if let Some(granted) = work.release(frame.body.len() as u64, STREAM_CREDIT) {
self.send(&[Frame::new(Kind::Credit, frame.stream, granted.to_le_bytes().to_vec())]);
}
let ended = frame.flags & frame::END != 0;
let item = if ended && frame.body.is_empty() { Ok(()) } else { guarded(|| work.item(&frame.body)) };
match (item, ended) {
(Ok(()), false) => {}
(Ok(()), true) => self.end_work(frame.stream, Ok(Empty {}.encode())),
(Err(failure), _) => self.end_work(frame.stream, Err(failure)),
}
}
#[cfg(feature = "jobs")]
fn end_work(self: &Arc<Self>, stream: u32, last: std::result::Result<Vec<u8>, Failure>) {
let Some(work) = self.jobs.take_work(stream) else {
return;
};
let shared = Arc::clone(self);
self.workers.run(Box::new(move || {
work.end();
shared.end_stream(stream, last);
}));
}
#[cfg_attr(
not(any(
feature = "jobs",
feature = "sql",
feature = "blobs",
feature = "records",
feature = "metrics",
feature = "config",
feature = "backup",
feature = "channels"
)),
expect(
unused_variables,
reason = "only the streams of jobs, sql, blobs, records, metrics, config, backup and channels take credit"
)
)]
fn credit(&self, frame: &Frame) {
let Ok(granted) = <[u8; 4]>::try_from(frame.body.as_slice()) else {
return;
};
let granted = u32::from_le_bytes(granted);
#[cfg(feature = "jobs")]
self.jobs.grant(frame.stream, granted);
#[cfg(feature = "sql")]
self.sql.grant(frame.stream, granted);
#[cfg(feature = "blobs")]
self.blobs.grant(frame.stream, granted);
#[cfg(feature = "records")]
self.records.grant(frame.stream, granted);
#[cfg(feature = "metrics")]
self.metrics.grant(frame.stream, granted);
#[cfg(feature = "config")]
self.config.grant(frame.stream, granted);
#[cfg(feature = "backup")]
self.backups.grant(frame.stream, granted);
#[cfg(feature = "channels")]
self.channels.grant(frame.stream, granted);
}
fn cancel(self: &Arc<Self>, stream: u32) {
let read = lock(&self.reads).get(&stream).cloned();
if let Some(read) = read {
read.cancel();
}
#[cfg(feature = "sql")]
if self.sql.cancel(stream) {
return;
}
#[cfg(feature = "blobs")]
if self.blobs.cancel(stream) {
return;
}
#[cfg(feature = "records")]
if self.records.cancel(stream) {
return;
}
#[cfg(feature = "metrics")]
if self.metrics.cancel(stream) {
return;
}
#[cfg(feature = "config")]
if self.config.cancel(stream) {
return;
}
#[cfg(feature = "backup")]
if self.backups.cancel(stream) {
return;
}
#[cfg(feature = "channels")]
if self.channels.cancel(stream) {
return;
}
#[cfg(feature = "locks")]
if self.locks.cancel(stream) {
return self.answer(stream, Err(Failure::cancelled("the client cancelled its hold")));
}
#[cfg(feature = "jobs")]
if self.jobs.work(stream).is_some() {
return self.end_work(stream, Err(Failure::cancelled("the client cancelled its worker")));
}
#[cfg(feature = "jobs")]
if let Some(watch) = self.jobs.take_watch(stream) {
return watch.close(Err(Failure::cancelled("the client cancelled its watch")));
}
let mut runs = self.lock_runs();
let cancelled = Failure::cancelled("the client cancelled the run");
match runs.remove(&stream) {
Some(ClientRun::Asking(_)) => self.answer(stream, Err(cancelled)),
Some(ClientRun::Handed(handed)) => {
self.end_stream(stream, Err(cancelled));
drop(runs);
drop(handed);
}
None => {}
}
}
fn reading(&self, stream: u32, method: u16) -> Option<CancellationToken> {
if method::access(method) != Some(Access::Read) {
return None;
}
let token = CancellationToken::new();
lock(&self.reads).insert(stream, token.clone());
Some(token)
}
fn read_under<T>(&self, stream: u32, token: Option<CancellationToken>, read: impl FnOnce() -> T) -> T {
let Some(token) = token else { return read() };
let answered = token.run(read);
let mut reads = lock(&self.reads);
if reads.get(&stream).is_some_and(|kept| kept.same(&token)) {
reads.remove(&stream);
}
answered
}
fn server_call(&self, stream: u32, called: u16, body: &[u8]) {
match called {
method::SERVER_STOP => match self.stopping(body) {
Ok(stop) => {
self.answer(stream, Ok(Empty {}.encode()));
stop();
}
Err(failure) => self.answer(stream, Err(failure)),
},
method::SERVER_CLOCK => self.answer(stream, guarded(|| self.clock(body))),
other => self.answer(stream, Err(Failure::unimplemented(format!("method {other:#06x}")))),
}
}
fn stopping(&self, body: &[u8]) -> std::result::Result<crate::pipe::Stop, Failure> {
Empty::decode(body)?;
let refused = || Failure::permission("this store's program decides when its server stops");
self.connect.stop.clone().ok_or_else(refused)
}
fn clock(&self, body: &[u8]) -> std::result::Result<Vec<u8>, Failure> {
let asked = ServerClock::decode(body)?;
let Some(clock) = &self.connect.clock else {
return Err(Failure::invalid("a clock call to a server that runs on the system's clock"));
};
match (asked.at, asked.advance) {
(Some(_), Some(_)) => return Err(Failure::invalid("a clock set and moved in one call")),
(Some(at), None) => clock.set(from_unix_millis(at))?,
(None, Some(advance)) => clock.advance(Duration::from_millis(advance)),
(None, None) => return Ok(ServerClock { at: Some(unix_millis(clock.now())), advance: None }.encode()),
}
self.store.clock_moved();
Ok(ServerClock { at: Some(unix_millis(clock.now())), advance: None }.encode())
}
fn answer(&self, stream: u32, answered: std::result::Result<Vec<u8>, Failure>) {
let response = match self.within_agreed(answered) {
Ok(body) => Frame::new(Kind::Response, stream, body).ending(),
Err(failure) => Frame::new(Kind::Response, stream, failure.encode()).failing(),
};
self.send_last(response);
}
fn end_stream(&self, stream: u32, answered: std::result::Result<Vec<u8>, Failure>) {
let data = match self.within_agreed(answered) {
Ok(body) => Frame::new(Kind::Data, stream, body).ending(),
Err(failure) => Frame::new(Kind::Data, stream, failure.encode()).failing(),
};
self.send_last(data);
}
fn call(&self, method: u16, body: &[u8], max_body: usize) -> std::result::Result<Vec<u8>, Failure> {
let started = self.store.probe().start();
let answered = self.call_engine(method, body, max_body);
if let Some(started) = started {
self.store.probe().time("tinystore_wire_call_ms", &[("method", method_name(method))], started.elapsed());
}
answered
}
fn call_engine(&self, method: u16, body: &[u8], max_body: usize) -> std::result::Result<Vec<u8>, Failure> {
let lent = |handle| self.lent(handle);
match method >> 8 {
0x01 => {
let read_only = self.read_only.load(Ordering::Relaxed);
kv::call(&self.store, &self.kv, &lent, method, body, (max_body, read_only))
}
#[cfg(feature = "jobs")]
0x02 => jobs::call(&self.store, &self.jobs, &lent, method, body, max_body),
#[cfg(feature = "sql")]
0x03 => sql::call(&self.store, &self.sql, method, body, self.read_only.load(Ordering::Relaxed)),
#[cfg(feature = "blobs")]
0x04 => blobs::call(&self.store, &self.blobs, &lent, method, body, max_body),
#[cfg(feature = "records")]
0x05 => records::call(&self.store, &self.records, method, body, self.read_only.load(Ordering::Relaxed)),
#[cfg(feature = "metrics")]
0x06 => metrics::call(&self.store, method, body, max_body),
#[cfg(feature = "config")]
0x07 => config::call(&self.store, &self.config, method, body, self.read_only.load(Ordering::Relaxed)),
#[cfg(feature = "locks")]
0x09 => locks::call(&self.store, method, body),
#[cfg(feature = "channels")]
0x0a => channels::call(&self.store, method, body),
#[cfg(feature = "search")]
0x0b => {
let read_only = self.read_only.load(Ordering::Relaxed);
search::call(&self.store, &self.search, &lent, method, body, read_only)
}
_ => Err(Failure::unimplemented(format!("method {method:#06x}"))),
}
}
fn lent(&self, handle: u64) -> std::result::Result<Arc<SharedFile>, Failure> {
#[cfg(feature = "sql")]
return self.sql.shared(handle);
#[cfg(not(feature = "sql"))]
Err(Failure::invalid(format!("database {handle}: this server keeps no databases")))
}
#[cfg(feature = "sql")]
fn lend(&self, called: u16, body: &[u8]) -> std::result::Result<crate::sql::Write, Failure> {
match called {
#[cfg(feature = "jobs")]
method::JOBS_ADD => jobs::lend(&self.jobs, body),
_ => Err(Failure::invalid(format!(
"method {called:#06x} does not run in a batch, which takes a queue's add"
))),
}
}
#[cfg(feature = "sql")]
fn call_in(
&self,
tx: &Transaction<'_>,
method: u16,
body: &[u8],
max_body: usize,
) -> std::result::Result<Vec<u8>, Failure> {
match method >> 8 {
0x01 => kv::call_in(&self.kv, tx, method, body, max_body),
#[cfg(feature = "jobs")]
0x02 => jobs::call_in(&self.jobs, tx, method, body, max_body),
#[cfg(feature = "blobs")]
0x04 => blobs::call_in(&self.blobs, tx, method, body),
#[cfg(feature = "channels")]
0x0a => channels::call_in(&self.store, tx, method, body),
_ => Err(Failure::invalid(format!("method {method:#06x} does not run inside a transaction"))),
}
}
fn go_away(&self, input: &mut Input, code: &str, message: &str) {
let body = GoAway { code: code.to_owned(), message: message.to_owned() }.encode();
self.send(&[Frame::new(Kind::GoAway, 0, body)]);
self.finish(input);
}
fn finish(&self, input: &mut Input) {
input.ended = true;
self.lock_output().ended = true;
self.ready.notify_all();
}
fn send(&self, frames: &[Frame]) {
self.queue(frames, None);
}
fn drained(&self) {
let mut waiting = self.waiting_output.lock().unwrap_or_else(PoisonError::into_inner);
if waiting.is_empty() || self.owed() > OUTPUT_LOW {
return;
}
for task in std::mem::take(&mut *waiting) {
self.workers.run(task);
}
}
fn owed(&self) -> usize {
self.lock_output().owed()
}
fn when_drained(&self, task: Box<dyn FnOnce() + Send>) {
self.waiting_output.lock().unwrap_or_else(PoisonError::into_inner).push(task);
self.drained();
}
fn send_last(&self, frame: Frame) {
let stream = frame.stream;
self.queue_moved(frame, Some(stream));
}
fn open_stream(&self, stream: u32, body: &[u8]) -> std::result::Result<(), String> {
let mut open = self.lock_open();
if open.contains_key(&stream) {
return Err(format!("a REQUEST on stream {stream}, which is in use"));
}
if open.len() >= IN_FLIGHT as usize {
return Err(format!("a REQUEST past the {IN_FLIGHT} streams in flight WELCOME allowed"));
}
open.insert(stream, OpenStream { left: STREAM_CREDIT, held: body.len() as u64 });
Ok(())
}
fn receive(&self, bytes: u64) -> std::result::Result<(), String> {
let outstanding = self.outstanding.fetch_add(bytes, Ordering::AcqRel) + bytes;
if outstanding > CONNECTION_CREDIT {
return Err(format!("{outstanding} bytes sent past the connection's credit of {CONNECTION_CREDIT}"));
}
Ok(())
}
fn spend_stream_credit(&self, stream: u32, bytes: u64) -> std::result::Result<(), String> {
let mut open = self.lock_open();
let Some(opened) = open.get_mut(&stream) else {
drop(open);
self.let_go(bytes);
self.grant_when_due();
return Ok(());
};
opened.left =
opened.left.checked_sub(bytes).ok_or_else(|| format!("a DATA on stream {stream} past its credit"))?;
opened.held += bytes;
Ok(())
}
#[cfg(feature = "jobs")]
fn let_go_of(&self, stream: u32, bytes: u64) {
let released = match self.lock_open().get_mut(&stream) {
Some(opened) => {
let released = bytes.min(opened.held);
opened.held -= released;
released
}
None => return,
};
self.let_go(released);
self.grant_when_due();
}
fn let_go(&self, bytes: u64) {
self.to_grant.fetch_add(bytes, Ordering::AcqRel);
}
fn grant_when_due(&self) {
let gathered = self.to_grant.load(Ordering::Acquire);
if gathered < CONNECTION_CREDIT / 2 {
return;
}
let granted = self.to_grant.swap(0, Ordering::AcqRel);
if granted == 0 {
return;
}
self.outstanding.fetch_sub(granted, Ordering::AcqRel);
let granted = u32::try_from(granted).unwrap_or(u32::MAX);
self.send(&[Frame::new(Kind::Credit, 0, granted.to_le_bytes().to_vec())]);
}
fn within_agreed(&self, answered: std::result::Result<Vec<u8>, Failure>) -> std::result::Result<Vec<u8>, Failure> {
let agreed = self.agreed.load(Ordering::Acquire);
match answered {
Ok(body) if body.len() > agreed => {
let refused = format!("an answer of {} bytes, past the {agreed} a body holds", body.len());
Err(Failure::from(Limit { bound: agreed as u64, ..ANSWER_BYTES }.refuse(body.len() as u64, refused)))
}
answered => answered,
}
}
fn written_within_agreed(
&self,
answered: std::result::Result<Vec<u8>, Failure>,
) -> std::result::Result<Vec<u8>, Failure> {
let agreed = self.agreed.load(Ordering::Acquire);
match answered {
Ok(body) if body.len() > agreed => Err(Failure {
code: "outcome_unknown".to_owned(),
message: format!(
"the write ran, and its answer of {} bytes is past the {agreed} a body holds: read what it wrote",
body.len()
),
what: None,
}),
answered => answered,
}
}
fn lock_open(&self) -> MutexGuard<'_, HashMap<u32, OpenStream>> {
self.open.lock().unwrap_or_else(PoisonError::into_inner)
}
fn queue(&self, frames: &[Frame], ended: Option<u32>) {
let mut output = self.lock_output();
let dropped = output.ended && frames.iter().all(|frame| frame.kind != Kind::GoAway);
if !dropped {
for frame in frames {
output.copy(frame);
}
}
self.queued(output, dropped, frames, ended);
}
fn queue_moved(&self, frame: Frame, ended: Option<u32>) {
debug_assert!(frame.kind != Kind::Credit, "a grant is counted where it is queued");
let mut output = self.lock_output();
let dropped = output.ended && frame.kind != Kind::GoAway;
if !dropped {
output.push(frame);
}
self.queued(output, dropped, &[], ended);
}
fn queued(&self, mut output: MutexGuard<'_, Output>, dropped: bool, frames: &[Frame], ended: Option<u32>) {
self.owing.store(output.owed(), Ordering::SeqCst);
let mut let_go = 0;
{
let mut open = self.lock_open();
for frame in frames.iter().filter(|frame| frame.kind == Kind::Credit && frame.stream != 0) {
if let (Some(opened), Ok(granted)) = (open.get_mut(&frame.stream), <[u8; 4]>::try_from(&frame.body[..]))
{
let granted = u64::from(u32::from_le_bytes(granted));
opened.left = opened.left.saturating_add(granted);
let released = granted.min(opened.held);
opened.held -= released;
let_go += released;
}
}
if let Some(opened) = ended.and_then(|stream| open.remove(&stream)) {
let_go += opened.held;
}
}
if let_go > 0 {
self.let_go(let_go);
}
if dropped {
return;
}
let wake = !std::mem::replace(&mut output.woken, true);
let awaited = output.awaited;
drop(output);
if awaited {
self.ready.notify_all();
}
if wake && let Some(wake) = &*lock(&self.wake) {
wake();
}
if let_go > 0 {
self.grant_when_due();
}
}
fn lock_input(&self) -> MutexGuard<'_, Input> {
self.input.lock().unwrap_or_else(PoisonError::into_inner)
}
fn lock_output(&self) -> MutexGuard<'_, Output> {
self.output.lock().unwrap_or_else(PoisonError::into_inner)
}
fn lock_runs(&self) -> MutexGuard<'_, HashMap<u32, ClientRun>> {
self.runs.lock().unwrap_or_else(PoisonError::into_inner)
}
}
fn still_asking(runs: &HashMap<u32, ClientRun>, stream: u32, asked: u64) -> bool {
matches!(runs.get(&stream), Some(ClientRun::Asking(asking)) if *asking == asked)
}
fn refused_to(capability: Option<Capability>, method: u16) -> Option<&'static str> {
match method::access(method) {
Some(Access::Admin) if capability != Some(Capability::Admin) => Some("the server's own calls are an admin's"),
Some(Access::Write) if capability == Some(Capability::Read) => Some("a read connection writes nothing"),
_ => None,
}
}
fn route(method: u16) -> Route {
match method >> 8 {
0x01 => kv::route(method),
#[cfg(feature = "jobs")]
0x02 => jobs::route(method),
#[cfg(feature = "sql")]
0x03 => sql::route(method),
#[cfg(feature = "blobs")]
0x04 => blobs::route(method),
#[cfg(feature = "records")]
0x05 => records::route(method),
#[cfg(feature = "metrics")]
0x06 => metrics::route(method),
#[cfg(feature = "config")]
0x07 => config::route(method),
#[cfg(feature = "backup")]
0x08 => Route::Backup,
#[cfg(feature = "locks")]
0x09 => locks::route(method),
#[cfg(feature = "channels")]
0x0a => channels::route(method),
_ => Route::Worker,
}
}
fn engines() -> Vec<String> {
let jobs = cfg!(feature = "jobs").then_some("jobs");
let sql = cfg!(feature = "sql").then_some("sql");
let blobs = cfg!(feature = "blobs").then_some("blobs");
let records = cfg!(feature = "records").then_some("records");
let metrics = cfg!(feature = "metrics").then_some("metrics");
let config = cfg!(feature = "config").then_some("config");
let backup = cfg!(feature = "backup").then_some("backup");
let locks = cfg!(feature = "locks").then_some("locks");
let channels = cfg!(feature = "channels").then_some("channels");
let search = cfg!(feature = "search").then_some("search");
let engines = [Some("kv"), config, locks, channels, jobs, sql, blobs, records, metrics, search, backup];
engines.into_iter().flatten().map(str::to_owned).collect()
}
fn guarded<T>(call: impl FnOnce() -> std::result::Result<T, Failure>) -> std::result::Result<T, Failure> {
catch_unwind(AssertUnwindSafe(call)).unwrap_or_else(|_| Err(Failure::internal("a call panicked")))
}
fn instance() -> Vec<u8> {
let state = RandomState::new();
let now = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).map_or(0, |since| since.as_nanos());
let mut bytes = Vec::with_capacity(16);
for half in 0..2u8 {
let mut hasher = state.build_hasher();
hasher.write_u8(half);
hasher.write_u128(now);
hasher.write_usize(&bytes as *const _ as usize);
bytes.extend_from_slice(&hasher.finish().to_le_bytes());
}
bytes
}
#[cfg(test)]
#[path = "session_tests.rs"]
mod tests;
impl Drop for Shared {
fn drop(&mut self) {
self.store.probe().shift("tinystore_wire_connections", &[], -1.0);
}
}
pub(super) fn method_name(method: u16) -> &'static str {
super::protocol::METHODS.iter().find(|(_, id, _)| *id == method).map_or("unknown", |(name, ..)| name)
}