use std::borrow::Cow;
use std::cell::{Cell, RefCell};
use std::sync::{Arc, Mutex, OnceLock};
use bytes::{BufMut, Bytes, BytesMut};
use serde::{Deserialize, Serialize, Serializer};
use serde_json::value::RawValue;
use serde_json::Value;
use super::types::{PlexusStreamItem, StreamMetadata};
#[derive(Clone, Default)]
pub struct Content(Repr);
#[derive(Clone)]
enum Repr {
Value(Value),
Json { text: Bytes, value: OnceLock<Value> },
}
impl Default for Repr {
fn default() -> Self {
Repr::Value(Value::Null)
}
}
impl Content {
pub fn from_value(value: Value) -> Self {
Content(Repr::Value(value))
}
pub fn from_json(text: Bytes) -> Result<Self, serde_json::Error> {
serde_json::from_slice::<serde::de::IgnoredAny>(&text)?;
Ok(Self::json(text))
}
pub(crate) fn json(text: Bytes) -> Self {
Content(Repr::Json { text, value: OnceLock::new() })
}
pub fn value(&self) -> &Value {
match &self.0 {
Repr::Value(v) => v,
Repr::Json { text, value } => value.get_or_init(|| serde_json::from_slice(text).unwrap_or(Value::Null)),
}
}
pub fn into_value(self) -> Value {
match self.0 {
Repr::Value(v) => v,
Repr::Json { text, value } => value.into_inner().unwrap_or_else(|| serde_json::from_slice(&text).unwrap_or(Value::Null)),
}
}
pub fn to_value(&self) -> Value {
match &self.0 {
Repr::Value(v) => v.clone(),
Repr::Json { text, value } => value.get().cloned().unwrap_or_else(|| serde_json::from_slice(text).unwrap_or(Value::Null)),
}
}
pub fn decode<T: serde::de::DeserializeOwned>(&self) -> Result<T, serde_json::Error> {
match &self.0 {
Repr::Value(v) => T::deserialize(v),
Repr::Json { text, value } => match value.get() {
Some(v) => T::deserialize(v),
None => serde_json::from_slice(text),
},
}
}
pub fn json_bytes(&self) -> Option<&Bytes> {
match &self.0 {
Repr::Json { text, .. } => Some(text),
Repr::Value(_) => None,
}
}
pub fn is_encoded(&self) -> bool {
matches!(self.0, Repr::Json { .. })
}
fn write_json(&self, out: &mut BytesMut) {
match &self.0 {
Repr::Json { text, .. } => out.put_slice(text),
Repr::Value(v) => {
let _ = serde_json::to_writer((&mut *out).writer(), v);
}
}
}
}
impl std::ops::Deref for Content {
type Target = Value;
fn deref(&self) -> &Value {
self.value()
}
}
impl From<Value> for Content {
fn from(value: Value) -> Self {
Content::from_value(value)
}
}
impl From<Content> for Value {
fn from(content: Content) -> Self {
content.into_value()
}
}
impl<T: ?Sized> PartialEq<T> for Content
where
Value: PartialEq<T>,
{
fn eq(&self, other: &T) -> bool {
self.value() == other
}
}
impl PartialEq<Content> for Value {
fn eq(&self, other: &Content) -> bool {
self == other.value()
}
}
impl std::fmt::Debug for Content {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.0 {
Repr::Value(v) => v.fmt(f),
Repr::Json { text, .. } => write!(f, "{}", String::from_utf8_lossy(text)),
}
}
}
impl std::fmt::Display for Content {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.0 {
Repr::Value(v) => v.fmt(f),
Repr::Json { text, .. } => f.write_str(&String::from_utf8_lossy(text)),
}
}
}
impl Serialize for Content {
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
if let Repr::Json { text, value } = &self.0 {
if writing_json() {
if let Ok(raw) = serde_json::from_slice::<&RawValue>(text) {
return raw.serialize(s);
}
}
if let Some(v) = value.get() {
return v.serialize(s);
}
}
self.value().serialize(s)
}
}
impl<'de> Deserialize<'de> for Content {
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
Value::deserialize(d).map(Content::from_value)
}
}
impl schemars::JsonSchema for Content {
fn inline_schema() -> bool {
<Value as schemars::JsonSchema>::inline_schema()
}
fn schema_name() -> Cow<'static, str> {
<Value as schemars::JsonSchema>::schema_name()
}
fn schema_id() -> Cow<'static, str> {
<Value as schemars::JsonSchema>::schema_id()
}
fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
<Value as schemars::JsonSchema>::json_schema(generator)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum WireEncoding {
#[default]
Json,
}
#[derive(Debug, Clone)]
pub struct WireFrame {
bytes: Bytes,
fence: bool,
}
impl WireFrame {
pub fn bytes(&self) -> &Bytes {
&self.bytes
}
pub fn into_bytes(self) -> Bytes {
self.bytes
}
pub fn fenced(&self) -> bool {
self.fence
}
}
#[derive(Clone)]
pub struct FrameSink {
tx: tokio::sync::mpsc::Sender<WireFrame>,
encoding: WireEncoding,
spare: Arc<Mutex<Option<BytesMut>>>,
}
pub type FrameReceiver = tokio::sync::mpsc::Receiver<WireFrame>;
impl FrameSink {
pub fn channel(capacity: usize, encoding: WireEncoding) -> (FrameSink, FrameReceiver) {
let (tx, rx) = tokio::sync::mpsc::channel(capacity.max(1));
(FrameSink { tx, encoding, spare: Arc::new(Mutex::new(None)) }, rx)
}
pub fn encoding(&self) -> WireEncoding {
self.encoding
}
pub fn is_closed(&self) -> bool {
self.tx.is_closed()
}
fn take_block(&self) -> Option<BytesMut> {
self.spare.lock().ok().and_then(|mut s| s.take())
}
fn give_block(&self, block: BytesMut) {
if block.capacity() <= Buffer::POOLED {
if let Ok(mut s) = self.spare.lock() {
if s.is_none() {
*s = Some(block);
}
}
}
}
}
impl std::fmt::Debug for FrameSink {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("FrameSink").field("encoding", &self.encoding).field("closed", &self.tx.is_closed()).finish()
}
}
struct Buffer {
cur: BytesMut,
spare: Vec<BytesMut>,
block: usize,
started: bool,
}
impl Default for Buffer {
fn default() -> Self {
Buffer { cur: BytesMut::new(), spare: Vec::new(), block: Self::FIRST, started: false }
}
}
impl Buffer {
const FIRST: usize = 8 * 1024;
const MOST: usize = 512 * 1024;
const POOLED: usize = 64 * 1024;
const SPARE: usize = 3;
fn from_block(block: Option<BytesMut>) -> Self {
match block {
Some(cur) => Buffer { block: cur.capacity().max(Self::FIRST), cur, spare: Vec::new(), started: true },
None => Buffer::default(),
}
}
fn room(&mut self, n: usize) {
if self.cur.capacity() - self.cur.len() >= n || self.cur.try_reclaim(n) {
return;
}
let free = self.spare.iter_mut().position(|b| b.try_reclaim(n));
let next = match free {
Some(i) => self.spare.swap_remove(i),
None => {
if self.started {
self.block = (self.block * 2).clamp(Self::FIRST, Self::MOST);
}
self.started = true;
BytesMut::with_capacity(self.block.max(n))
}
};
let old = std::mem::replace(&mut self.cur, next);
if old.capacity() > 0 {
if self.spare.len() >= Self::SPARE {
self.spare.remove(0);
}
self.spare.push(old);
}
}
fn split(&mut self) -> Bytes {
self.cur.split().freeze()
}
fn into_block(self) -> Option<BytesMut> {
(self.cur.capacity() >= 1024).then_some(self.cur)
}
}
struct Encoder {
#[allow(dead_code)]
encoding: WireEncoding,
buffer: RefCell<Buffer>,
depth: Cell<u32>,
last: Cell<usize>,
}
tokio::task_local! {
static WIRE: Encoder;
}
thread_local! {
static WRITING_JSON: Cell<u32> = const { Cell::new(0) };
}
fn writing_json() -> bool {
WRITING_JSON.with(|w| w.get() > 0)
}
struct WritingJson;
impl WritingJson {
fn on() -> Self {
WRITING_JSON.with(|w| w.set(w.get() + 1));
WritingJson
}
}
impl Drop for WritingJson {
fn drop(&mut self) {
WRITING_JSON.with(|w| w.set(w.get().saturating_sub(1)));
}
}
pub(crate) fn nested<R>(poll: impl FnOnce() -> R) -> R {
if WIRE.try_with(|e| e.depth.set(e.depth.get() + 1)).is_err() {
return poll();
}
struct Back;
impl Drop for Back {
fn drop(&mut self) {
let _ = WIRE.try_with(|e| e.depth.set(e.depth.get().saturating_sub(1)));
}
}
let _back = Back;
poll()
}
pub(crate) fn encode<T: Serialize + ?Sized>(event: &T) -> Option<Content> {
WIRE.try_with(|e| {
if e.depth.get() > 0 {
return None;
}
let mut buffer = e.buffer.borrow_mut();
buffer.room(e.last.get().saturating_mul(2).max(256));
debug_assert!(buffer.cur.is_empty());
let (written, captured) = plexus_auth_core::credential::run_with_credential_capture(|| {
let _json = WritingJson::on();
serde_json::to_writer((&mut buffer.cur).writer(), event).is_ok()
});
if !written || !captured.is_empty() {
buffer.cur.clear();
return None;
}
e.last.set(buffer.cur.len());
Some(Content::json(buffer.split()))
})
.ok()
.flatten()
}
struct Frames {
prefix: Vec<u8>,
head: Option<(Arc<StreamMetadata>, Cow<'static, str>, Vec<u8>)>,
}
impl Frames {
fn new(method: &str, subscription: &jsonrpsee::types::SubscriptionId<'_>) -> Self {
let mut prefix = br#"{"jsonrpc":"2.0","method":"#.to_vec();
let _ = serde_json::to_writer(&mut prefix, method);
prefix.extend_from_slice(br#","params":{"subscription":"#);
let _ = serde_json::to_writer(&mut prefix, subscription);
prefix.extend_from_slice(br#","result":"#);
Frames { prefix, head: None }
}
fn item(&mut self, out: &mut BytesMut, item: &PlexusStreamItem) {
match item {
PlexusStreamItem::Data { metadata, content_type, content } => {
let fresh = match &self.head {
Some((m, ct, _)) => !Arc::ptr_eq(m, metadata) || ct != content_type,
None => true,
};
if fresh {
let mut head = br#"{"type":"data","metadata":"#.to_vec();
let _ = serde_json::to_writer(&mut head, &**metadata);
head.extend_from_slice(br#","content_type":"#);
let _ = serde_json::to_writer(&mut head, content_type.as_ref());
head.extend_from_slice(br#","content":"#);
self.head = Some((metadata.clone(), content_type.clone(), head));
}
let head = &self.head.as_ref().expect("made above").2;
out.reserve(head.len() + content.json_bytes().map_or(64, Bytes::len) + 1);
out.put_slice(head);
content.write_json(out);
out.put_u8(b'}');
}
other => {
let _ = serde_json::to_writer((&mut *out).writer(), other);
}
}
}
fn frame(&mut self, buffer: &mut Buffer, item: &PlexusStreamItem) -> Bytes {
let content = match item {
PlexusStreamItem::Data { content, .. } => content.json_bytes().map_or(256, Bytes::len),
_ => 256,
};
buffer.room(self.prefix.len() + content + 256);
buffer.cur.put_slice(&self.prefix);
self.item(&mut buffer.cur, item);
buffer.cur.put_slice(b"}}");
buffer.split()
}
fn raw_item(&mut self, buffer: &mut Buffer, item: &PlexusStreamItem) -> Bytes {
buffer.room(512);
self.item(&mut buffer.cur, item);
buffer.split()
}
}
pub async fn pipe(sink: jsonrpsee::SubscriptionSink, stream: super::streaming::PlexusStream, ext: &http::Extensions) -> jsonrpsee::core::SubscriptionResult {
let frames = ext.get::<FrameSink>().cloned();
let encoding = frames.as_ref().map(FrameSink::encoding).unwrap_or_default();
let buffer = Buffer::from_block(frames.as_ref().and_then(FrameSink::take_block));
let encoder = Encoder { encoding, buffer: RefCell::new(buffer), depth: Cell::new(0), last: Cell::new(0) };
WIRE.scope(encoder, run(sink, stream, frames)).await
}
async fn run(sink: jsonrpsee::SubscriptionSink, mut stream: super::streaming::PlexusStream, frames: Option<FrameSink>) -> jsonrpsee::core::SubscriptionResult {
use futures::StreamExt;
let connection = sink.connection_id().0;
let mut notification = Frames::new(sink.method_name(), &sink.subscription_id());
let mut first = true;
let out: jsonrpsee::core::SubscriptionResult = async {
loop {
let item = tokio::select! {
biased;
_ = sink.closed() => return Ok(()),
item = stream.next() => item,
};
let Some(item) = item else { return Ok(()) };
if let PlexusStreamItem::Request { request_id, .. } = &item {
super::bidirectional::bind_asker(request_id, connection);
}
match &frames {
Some(to) => {
let bytes = WIRE.with(|e| notification.frame(&mut e.buffer.borrow_mut(), &item));
drop(item);
if to.tx.send(WireFrame { bytes, fence: first }).await.is_err() {
return Ok(());
}
}
None => {
let bytes = WIRE.with(|e| notification.raw_item(&mut e.buffer.borrow_mut(), &item));
drop(item);
let raw = RawValue::from_string(String::from_utf8(bytes.to_vec()).unwrap_or_default())?;
sink.send(raw).await?;
}
}
first = false;
}
}
.await;
drop(stream);
if let Some(to) = &frames {
if let Some(block) = WIRE.with(|e| std::mem::take(&mut *e.buffer.borrow_mut())).into_block() {
to.give_block(block);
}
}
out
}
pub struct Shared<T>(Arc<SharedInner<T>>);
struct SharedInner<T> {
value: T,
json: OnceLock<Option<Box<RawValue>>>,
}
impl<T> Shared<T> {
pub fn new(value: T) -> Self {
Shared(Arc::new(SharedInner { value, json: OnceLock::new() }))
}
pub fn is_encoded(&self) -> bool {
matches!(self.0.json.get(), Some(Some(_)))
}
}
impl<T: Serialize> Shared<T> {
fn encoded(&self) -> Option<&RawValue> {
self.0
.json
.get_or_init(|| {
let (text, captured) = plexus_auth_core::credential::run_with_credential_capture(|| serde_json::to_string(&self.0.value));
match text {
Ok(text) if captured.is_empty() => RawValue::from_string(text).ok(),
_ => None,
}
})
.as_deref()
}
}
impl<T> Clone for Shared<T> {
fn clone(&self) -> Self {
Shared(self.0.clone())
}
}
impl<T> std::ops::Deref for Shared<T> {
type Target = T;
fn deref(&self) -> &T {
&self.0.value
}
}
impl<T: std::fmt::Debug> std::fmt::Debug for Shared<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.value.fmt(f)
}
}
impl<T: Default> Default for Shared<T> {
fn default() -> Self {
Shared::new(T::default())
}
}
impl<T: Serialize> Serialize for Shared<T> {
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
if writing_json() {
if let Some(raw) = self.encoded() {
return raw.serialize(s);
}
}
self.0.value.serialize(s)
}
}
impl<'de, T: Deserialize<'de>> Deserialize<'de> for Shared<T> {
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
T::deserialize(d).map(Shared::new)
}
}
impl<T: schemars::JsonSchema> schemars::JsonSchema for Shared<T> {
fn inline_schema() -> bool {
T::inline_schema()
}
fn schema_name() -> Cow<'static, str> {
T::schema_name()
}
fn schema_id() -> Cow<'static, str> {
T::schema_id()
}
fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
T::json_schema(generator)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn content_reads_as_its_value_either_way() {
let v = serde_json::json!({"b": [1, 2], "a": "x"});
let encoded = Content::from_json(Bytes::from_static(br#"{"b":[1,2],"a":"x"}"#)).unwrap();
let valued = Content::from(v.clone());
assert!(encoded.is_encoded() && !valued.is_encoded());
assert_eq!(encoded, v);
assert_eq!(valued, v);
assert_eq!(encoded, valued);
assert_eq!(encoded["a"], "x");
assert_eq!(encoded.decode::<serde_json::Map<String, Value>>().unwrap().len(), 2);
assert_eq!(serde_json::to_value(&encoded).unwrap(), v);
assert_eq!(serde_json::to_string(&encoded).unwrap(), serde_json::to_string(&v).unwrap(), "outside the pipe it writes its Value");
assert_eq!(encoded.into_value(), v);
assert!(Content::from_json(Bytes::from_static(b"{nope")).is_err());
}
#[test]
fn a_buffer_reuses_a_block_once_its_frames_are_gone() {
let mut b = Buffer::default();
b.room(100);
b.cur.put_slice(&[1u8; 100]);
let first = b.split();
let block = b.cur.capacity() + 100;
for _ in 0..1000 {
b.room(100);
b.cur.put_slice(&[2u8; 100]);
drop(b.split());
}
assert_eq!(first.len(), 100, "a frame in flight stays whole");
assert!(b.block <= Buffer::FIRST * 2, "blocks are reused, not grown, when frames are written: {} (first {block})", b.block);
}
}