use std::{
collections::{BTreeMap, BTreeSet},
io, mem,
panic::{self, AssertUnwindSafe},
sync::{Arc, Mutex, MutexGuard, PoisonError, mpsc},
thread::{self, ThreadId},
time::{Duration, Instant},
};
use crate::{
config::{Cancellation, Config},
error::{CallError, Error},
error_object::ErrorObject,
handles::CancelToken,
id::Id,
message::{write_call, write_numbered_call, write_response},
pending::Pending,
raw::{Json, Raw},
sink::Sink,
text,
};
pub(crate) type Outcome = Result<Raw<'static>, CallError>;
pub(crate) trait Link: Send + Sync {
fn notify(&self, method: &str, params: Option<&Json<'_>>) -> Result<(), Error>;
fn request(
&self,
method: &str,
params: Option<&Json<'_>>,
timeout: Option<Duration>,
) -> Result<Issued, Error>;
fn cancel(&self, id: u64) -> bool;
fn expire(&self);
fn respond(
&self,
id: &Id<'static>,
result: Result<&Json<'_>, &ErrorObject>,
) -> Result<(), Error>;
fn on_endpoint_thread(&self) -> bool;
fn is_closed(&self) -> bool;
}
pub(crate) struct Issued {
pub(crate) id: u64,
pub(crate) outcome: mpsc::Receiver<Outcome>,
pub(crate) deadline: Option<Instant>,
}
pub(crate) struct Shared<K> {
inner: Mutex<Inner<K>>,
}
impl<K: Sink> Shared<K> {
pub(crate) fn new(sink: K, config: &Config) -> Arc<Self> {
Arc::new(Self {
inner: Mutex::new(Inner {
sink,
closed: false,
broken: None,
corked: false,
scratch: String::new(),
pending: Pending::new(config.max_pending),
in_flight: BTreeMap::new(),
batches: BTreeMap::new(),
next_batch: 0,
gate: if config.gate.is_some() {
GateState::Closed
} else {
GateState::Open
},
held: Vec::new(),
held_requests: BTreeSet::new(),
settings: Settings::from(config),
epoch: Instant::now(),
endpoint_thread: None,
}),
})
}
pub(crate) fn lock(&self) -> MutexGuard<'_, Inner<K>> {
self.inner.lock().unwrap_or_else(PoisonError::into_inner)
}
}
impl<K: Sink> Link for Shared<K> {
fn notify(&self, method: &str, params: Option<&Json<'_>>) -> Result<(), Error> {
self.lock().notify(method, params)
}
fn request(
&self,
method: &str,
params: Option<&Json<'_>>,
timeout: Option<Duration>,
) -> Result<Issued, Error> {
self.lock().request(method, params, timeout)
}
fn cancel(&self, id: u64) -> bool {
self.lock().cancel_outgoing(id)
}
fn expire(&self) {
self.lock().expire();
}
fn respond(
&self,
id: &Id<'static>,
result: Result<&Json<'_>, &ErrorObject>,
) -> Result<(), Error> {
self.lock().respond_deferred(id, result)
}
fn on_endpoint_thread(&self) -> bool {
self.lock().endpoint_thread == Some(thread::current().id())
}
fn is_closed(&self) -> bool {
self.lock().closed
}
}
pub(crate) struct Settings {
pub(crate) cancellation: Option<Cancellation>,
pub(crate) request_timeout: Option<Duration>,
pub(crate) max_in_flight: usize,
}
impl From<&Config> for Settings {
fn from(config: &Config) -> Self {
Self {
cancellation: config.cancellation.clone(),
request_timeout: config.request_timeout,
max_in_flight: config.max_in_flight,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum GateState {
Open,
Closed,
Opening(Id<'static>),
}
struct InFlight {
token: CancelToken,
batch: Option<u64>,
}
struct Batch {
remaining: usize,
body: String,
}
struct Held {
request: Option<u64>,
body: String,
}
#[derive(Clone, Copy)]
enum Sending {
Response,
Notification,
Request(u64),
}
pub(crate) struct Inner<K> {
sink: K,
closed: bool,
broken: Option<io::Error>,
corked: bool,
scratch: String,
pending: Pending<mpsc::Sender<Outcome>>,
in_flight: BTreeMap<Id<'static>, InFlight>,
batches: BTreeMap<u64, Batch>,
next_batch: u64,
pub(crate) gate: GateState,
held: Vec<Held>,
held_requests: BTreeSet<u64>,
pub(crate) settings: Settings,
epoch: Instant,
pub(crate) endpoint_thread: Option<ThreadId>,
}
impl<K: Sink> Inner<K> {
pub(crate) fn sink_mut(&mut self) -> &mut K {
&mut self.sink
}
pub(crate) fn is_closed(&self) -> bool {
self.closed
}
pub(crate) fn configure(&mut self, config: &Config) {
self.settings = Settings::from(config);
if self.pending.is_empty() {
self.pending = Pending::new(config.max_pending);
}
if config.gate.is_none() {
self.gate = GateState::Open;
} else if self.gate == GateState::Open && self.held.is_empty() {
self.gate = GateState::Closed;
}
}
fn now(&self) -> Duration {
self.epoch.elapsed()
}
pub(crate) fn next_deadline(&self) -> Option<Instant> {
self.pending
.next_deadline()
.and_then(|at| self.epoch.checked_add(at))
}
pub(crate) fn cork(&mut self) {
self.corked = true;
}
fn on_endpoint_thread(&self) -> bool {
self.endpoint_thread == Some(thread::current().id())
}
pub(crate) fn uncork(&mut self) {
self.corked = false;
if !self.closed {
if let Err(error) = guard(|| self.sink.flush()) {
let _closed = self.fail(error);
}
}
}
pub(crate) fn take_broken(&mut self) -> Option<io::Error> {
self.broken.take()
}
pub(crate) fn close(&mut self) {
if self.closed {
return;
}
if let Err(error) = guard(|| self.sink.flush()) {
let _closed = self.fail(error);
}
self.shut();
}
fn shut(&mut self) {
self.closed = true;
for (_, waiter) in self.pending.drain() {
let _gone = waiter.send(Err(CallError::Closed));
}
for (_, request) in mem::take(&mut self.in_flight) {
request.token.cancel();
}
self.batches.clear();
self.held.clear();
self.held_requests.clear();
}
fn fail(&mut self, error: io::Error) -> Error {
let copy = io::Error::new(error.kind(), error.to_string());
if self.broken.is_none() {
self.broken = Some(error);
}
self.shut();
Error::Io(copy)
}
fn deliver(&mut self, sending: Sending) -> Result<(), Error> {
if self.closed {
return Err(Error::Closed);
}
if self.gate != GateState::Open && !matches!(sending, Sending::Response) {
let request = match sending {
Sending::Request(id) => Some(id),
_ => None,
};
if let Some(id) = request {
let _fresh = self.held_requests.insert(id);
}
self.held.push(Held {
request,
body: self.scratch.clone(),
});
return Ok(());
}
let mut sent = guard(|| self.sink.send(&self.scratch));
if sent.is_ok() && (!self.corked || !self.on_endpoint_thread()) {
sent = guard(|| self.sink.flush());
}
sent.map_err(|error| self.fail(error))
}
pub(crate) fn notify(&mut self, method: &str, params: Option<&Json<'_>>) -> Result<(), Error> {
if self.closed {
return Err(Error::Closed);
}
self.scratch.clear();
write_call(&mut self.scratch, None, method, params);
self.deliver(Sending::Notification)
}
pub(crate) fn request(
&mut self,
method: &str,
params: Option<&Json<'_>>,
timeout: Option<Duration>,
) -> Result<Issued, Error> {
if self.closed {
return Err(Error::Closed);
}
self.expire();
let (waiter, outcome) = mpsc::channel();
let deadline = timeout
.or(self.settings.request_timeout)
.and_then(|t| self.now().checked_add(t));
let (id, evicted) = self.pending.issue(waiter, deadline);
if let Some((oldest, waiter)) = evicted {
self.abandon(oldest, waiter, CallError::Evicted);
}
self.scratch.clear();
write_numbered_call(&mut self.scratch, id, method, params);
if let Err(error) = self.deliver(Sending::Request(id)) {
let _unsent = self.pending.settle(id);
return Err(error);
}
Ok(Issued {
id,
outcome,
deadline: deadline.and_then(|at| self.epoch.checked_add(at)),
})
}
pub(crate) fn expire(&mut self) {
let now = self.now();
while let Some((id, waiter)) = self.pending.pop_expired(now) {
self.abandon(id, waiter, CallError::TimedOut);
}
}
pub(crate) fn cancel_outgoing(&mut self, id: u64) -> bool {
match self.pending.settle(id) {
Some(waiter) => {
self.abandon(id, waiter, CallError::Cancelled);
true
}
None => false,
}
}
pub(crate) fn settle(&mut self, id: u64, outcome: Outcome) -> bool {
if self.held_requests.contains(&id) {
return false;
}
match self.pending.settle(id) {
Some(waiter) => {
let _gone = waiter.send(outcome);
true
}
None => false,
}
}
fn abandon(&mut self, id: u64, waiter: mpsc::Sender<Outcome>, reason: CallError) {
if !self.held_requests.remove(&id) && !self.closed {
self.send_cancel(id);
}
let _gone = waiter.send(Err(reason));
}
fn send_cancel(&mut self, id: u64) {
let Some(cancellation) = &self.settings.cancellation else {
return;
};
self.scratch.clear();
self.scratch.push_str("{\"jsonrpc\":\"2.0\",\"method\":");
jsonx_lib::write_string(&mut self.scratch, &cancellation.method);
self.scratch.push_str(",\"params\":{");
jsonx_lib::write_string(&mut self.scratch, &cancellation.id_member);
self.scratch.push(':');
text::push_u64(&mut self.scratch, id);
self.scratch.push_str("}}");
let _best_effort = self.deliver(Sending::Notification);
}
pub(crate) fn respond(
&mut self,
id: Option<&Id<'_>>,
batch: Option<u64>,
result: Result<&Json<'_>, &ErrorObject>,
) -> Result<(), Error> {
if self.closed {
return Err(Error::Closed);
}
self.scratch.clear();
write_response(&mut self.scratch, id, result);
let opening = match (&self.gate, id) {
(GateState::Opening(opening), Some(id)) => opening == id,
_ => false,
};
let sent = match batch {
Some(batch) => self.add_to_batch(batch, true),
None => self.deliver(Sending::Response),
};
if opening {
self.settle_gate(result.is_ok());
}
sent
}
fn add_to_batch(&mut self, batch: u64, with_response: bool) -> Result<(), Error> {
let Some(open) = self.batches.get_mut(&batch) else {
return Ok(());
};
if with_response {
if open.body.len() > 1 {
open.body.push(',');
}
open.body.push_str(&self.scratch);
}
open.remaining = open.remaining.saturating_sub(1);
if open.remaining > 0 {
return Ok(());
}
let Some(mut done) = self.batches.remove(&batch) else {
return Ok(());
};
if done.body.len() == 1 {
return Ok(());
}
done.body.push(']');
mem::swap(&mut self.scratch, &mut done.body);
self.deliver(Sending::Response)
}
fn settle_gate(&mut self, succeeded: bool) {
let held = mem::take(&mut self.held);
let live = mem::take(&mut self.held_requests);
if succeeded {
self.gate = GateState::Open;
for message in held {
if message.request.is_some_and(|id| !live.contains(&id)) {
continue;
}
self.scratch = message.body;
if self.deliver(Sending::Notification).is_err() {
break;
}
}
} else {
self.gate = GateState::Closed;
for message in held {
if let Some(id) = message.request.filter(|id| live.contains(id)) {
if let Some(waiter) = self.pending.settle(id) {
let _gone = waiter.send(Err(CallError::Cancelled));
}
}
}
}
}
pub(crate) fn open_batch(&mut self, answers: usize) -> u64 {
let id = self.next_batch;
self.next_batch = self.next_batch.wrapping_add(1);
let _fresh = self.batches.insert(
id,
Batch {
remaining: answers,
body: String::from("["),
},
);
id
}
pub(crate) fn in_flight(&self, id: &Id<'_>) -> bool {
!self.in_flight.is_empty() && self.in_flight.contains_key(&id.clone().into_owned())
}
pub(crate) fn begin_deferred(
&mut self,
id: Id<'static>,
batch: Option<u64>,
) -> Option<CancelToken> {
if self.in_flight.len() >= self.settings.max_in_flight {
return None;
}
let token = CancelToken::new();
let _fresh = self.in_flight.insert(
id,
InFlight {
token: token.clone(),
batch,
},
);
Some(token)
}
pub(crate) fn respond_deferred(
&mut self,
id: &Id<'static>,
result: Result<&Json<'_>, &ErrorObject>,
) -> Result<(), Error> {
match self.in_flight.remove(id) {
Some(request) => self.respond(Some(id), request.batch, result),
None if self.closed => Err(Error::Closed),
None => Err(Error::AlreadyAnswered),
}
}
pub(crate) fn cancel_incoming(&mut self, id: &Id<'_>) -> bool {
let Some(request) = self.in_flight.remove(&id.clone().into_owned()) else {
return false;
};
request.token.cancel();
let reply = self.settings.cancellation.as_ref().and_then(|c| c.reply);
let _sent = match reply {
Some(code) => {
let error = ErrorObject::new(code, "the request was cancelled");
self.respond(Some(id), request.batch, Err(&error))
}
None => {
if matches!(&self.gate, GateState::Opening(opening) if opening == id) {
self.settle_gate(false);
}
match request.batch {
Some(batch) => self.add_to_batch(batch, false),
None => Ok(()),
}
}
};
true
}
}
fn guard(send: impl FnOnce() -> io::Result<()>) -> io::Result<()> {
panic::catch_unwind(AssertUnwindSafe(send))
.unwrap_or_else(|_| Err(io::Error::other("the sink panicked")))
}