use crate::constants::{BodyFormat, ErrorCode, QueryFormat, REPE_VERSION};
use crate::error::RepeError;
use crate::io::{read_message, write_message};
use crate::message::{Message, MessageBuilder};
use repe_core::structs::ServableOwned;
use std::collections::{HashMap, VecDeque};
use std::io::{BufReader, BufWriter, ErrorKind, Write};
use std::net::{Shutdown, TcpStream, ToSocketAddrs};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::mpsc;
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;
type PendingSender = mpsc::Sender<Result<Message, RepeError>>;
type PendingRequests = HashMap<u64, PendingSender>;
type BatchResults<R> = Vec<Option<Result<R, RepeError>>>;
const MAX_BATCH_WORKERS: usize = 64;
#[derive(Clone)]
pub struct Client {
inner: Arc<ClientInner>,
}
struct ClientInner {
writer: Mutex<BufWriter<TcpStream>>,
pending: Mutex<PendingRequests>,
next_id: AtomicU64,
}
impl Drop for ClientInner {
fn drop(&mut self) {
if let Ok(writer) = self.writer.lock() {
let _ = writer.get_ref().shutdown(Shutdown::Both);
}
if let Ok(mut pending) = self.pending.lock() {
for (request_id, sender) in pending.drain() {
let io_err = std::io::Error::new(
ErrorKind::ConnectionAborted,
format!("client closed while waiting for request {request_id}"),
);
let _ = sender.send(Err(RepeError::Io(io_err)));
}
}
}
}
enum PendingDispatch {
Matched {
sender: PendingSender,
response: Message,
},
Unrecognized {
got_id: u64,
},
}
impl Client {
pub fn connect<A: ToSocketAddrs>(addr: A) -> std::io::Result<Self> {
let stream = TcpStream::connect(addr)?;
stream.set_nodelay(true).ok();
let reader_stream = stream.try_clone()?;
let inner = Arc::new(ClientInner {
writer: Mutex::new(BufWriter::new(stream)),
pending: Mutex::new(HashMap::new()),
next_id: AtomicU64::new(1),
});
spawn_response_loop(BufReader::new(reader_stream), Arc::downgrade(&inner));
Ok(Self { inner })
}
pub fn set_write_timeout(&self, d: Option<Duration>) -> std::io::Result<()> {
let writer = self
.inner
.writer
.lock()
.map_err(|_| std::io::Error::other("client writer lock poisoned"))?;
writer.get_ref().set_write_timeout(d)
}
fn next_request_id(&self) -> u64 {
self.inner.next_id.fetch_add(1, Ordering::Relaxed)
}
pub fn call_typed_json<P: AsRef<str>, T, R>(&self, path: P, body: &T) -> Result<R, RepeError>
where
T: structio::json::Write + ?Sized,
R: ServableOwned,
{
self.call_typed_json_with_optional_timeout(path, body, None)
}
pub fn call_typed_json_with_timeout<P: AsRef<str>, T, R>(
&self,
path: P,
body: &T,
timeout: Duration,
) -> Result<R, RepeError>
where
T: structio::json::Write + ?Sized,
R: ServableOwned,
{
self.call_typed_json_with_optional_timeout(path, body, Some(timeout))
}
pub fn call_typed_beve<P: AsRef<str>, T, R>(&self, path: P, body: &T) -> Result<R, RepeError>
where
T: structio::beve::Write + ?Sized,
R: ServableOwned,
{
self.call_typed_beve_with_optional_timeout(path, body, None)
}
pub fn call_typed_beve_with_timeout<P: AsRef<str>, T, R>(
&self,
path: P,
body: &T,
timeout: Duration,
) -> Result<R, RepeError>
where
T: structio::beve::Write + ?Sized,
R: ServableOwned,
{
self.call_typed_beve_with_optional_timeout(path, body, Some(timeout))
}
pub fn call_typed_slice<P, T, R>(&self, path: P, body: &[T]) -> Result<Vec<R>, RepeError>
where
P: AsRef<str>,
T: structio::beve::NumericBytes + structio::beve::Write,
R: structio::beve::NumericBytes + for<'de> structio::beve::Read<'de> + Default,
{
self.call_typed_slice_with_optional_timeout(path, body, None)
}
pub fn call_typed_slice_with_timeout<P, T, R>(
&self,
path: P,
body: &[T],
timeout: Duration,
) -> Result<Vec<R>, RepeError>
where
P: AsRef<str>,
T: structio::beve::NumericBytes + structio::beve::Write,
R: structio::beve::NumericBytes + for<'de> structio::beve::Read<'de> + Default,
{
self.call_typed_slice_with_optional_timeout(path, body, Some(timeout))
}
pub fn call_typed_slice_aligned<P, T, R>(
&self,
path: P,
body: &[T],
) -> Result<Vec<R>, RepeError>
where
P: AsRef<str>,
T: structio::beve::NumericBytes + structio::beve::Write,
R: structio::beve::NumericBytes + for<'de> structio::beve::Read<'de> + Default,
{
self.call_typed_slice_aligned_with_optional_timeout(path, body, None)
}
pub fn call_typed_slice_aligned_with_timeout<P, T, R>(
&self,
path: P,
body: &[T],
timeout: Duration,
) -> Result<Vec<R>, RepeError>
where
P: AsRef<str>,
T: structio::beve::NumericBytes + structio::beve::Write,
R: structio::beve::NumericBytes + for<'de> structio::beve::Read<'de> + Default,
{
self.call_typed_slice_aligned_with_optional_timeout(path, body, Some(timeout))
}
pub fn call_message<P: AsRef<str>>(&self, path: P) -> Result<Message, RepeError> {
self.call_message_with_formats_and_timeout(
path,
QueryFormat::JsonPointer as u16,
None,
BodyFormat::RawBinary as u16,
None,
)
}
pub fn call_message_with_timeout<P: AsRef<str>>(
&self,
path: P,
timeout: Duration,
) -> Result<Message, RepeError> {
self.call_message_with_formats_and_timeout(
path,
QueryFormat::JsonPointer as u16,
None,
BodyFormat::RawBinary as u16,
Some(timeout),
)
}
pub fn call_with_formats<P: AsRef<str>>(
&self,
path: P,
query_format: u16,
body: Option<&[u8]>,
body_format: u16,
) -> Result<Message, RepeError> {
self.call_message_with_formats_and_timeout(path, query_format, body, body_format, None)
}
pub fn call_with_formats_and_timeout<P: AsRef<str>>(
&self,
path: P,
query_format: u16,
body: Option<&[u8]>,
body_format: u16,
timeout: Duration,
) -> Result<Message, RepeError> {
self.call_message_with_formats_and_timeout(
path,
query_format,
body,
body_format,
Some(timeout),
)
}
pub fn registry_read_typed<P: AsRef<str>, R: ServableOwned>(
&self,
path: P,
) -> Result<R, RepeError> {
let resp = self.call_message(path)?;
resp.json_body::<R>()
}
pub fn registry_read_typed_with_timeout<P: AsRef<str>, R: ServableOwned>(
&self,
path: P,
timeout: Duration,
) -> Result<R, RepeError> {
let resp = self.call_message_with_timeout(path, timeout)?;
resp.json_body::<R>()
}
pub fn notify_json<P: AsRef<str>, T>(&self, path: P, body: &T) -> Result<(), RepeError>
where
T: structio::json::Write + ?Sized,
{
self.notify_with_body(path, |builder| builder.body_json(body))
}
pub fn notify_beve<P: AsRef<str>, T>(&self, path: P, body: &T) -> Result<(), RepeError>
where
T: structio::beve::Write + ?Sized,
{
self.notify_with_body(path, |builder| builder.body_beve(body))
}
pub fn notify_with_formats<P: AsRef<str>>(
&self,
path: P,
query_format: u16,
body: Option<&[u8]>,
body_format: u16,
) -> Result<(), RepeError> {
self.notify_with_message_formats(path, query_format, body, body_format)
}
pub fn batch_json<T, R>(&self, requests: Vec<(String, T)>) -> Vec<Result<R, RepeError>>
where
T: structio::json::Write + Send + 'static,
R: ServableOwned + Send + 'static,
{
self.batch_json_inner(requests, None)
}
pub fn batch_json_with_timeout<T, R>(
&self,
requests: Vec<(String, T)>,
timeout: Duration,
) -> Vec<Result<R, RepeError>>
where
T: structio::json::Write + Send + 'static,
R: ServableOwned + Send + 'static,
{
self.batch_json_inner(requests, Some(timeout))
}
fn batch_json_inner<T, R>(
&self,
requests: Vec<(String, T)>,
timeout: Option<Duration>,
) -> Vec<Result<R, RepeError>>
where
T: structio::json::Write + Send + 'static,
R: ServableOwned + Send + 'static,
{
let request_count = requests.len();
if request_count == 0 {
return Vec::new();
}
let worker_count = batch_worker_count(request_count);
let queue: VecDeque<(usize, String, T)> = requests
.into_iter()
.enumerate()
.map(|(index, (path, body))| (index, path, body))
.collect();
let work_queue = Arc::new(Mutex::new(queue));
let results: Arc<Mutex<BatchResults<R>>> = Arc::new(Mutex::new(
std::iter::repeat_with(|| None)
.take(request_count)
.collect(),
));
let mut workers = Vec::with_capacity(worker_count);
for _ in 0..worker_count {
let client = self.clone();
let work_queue = Arc::clone(&work_queue);
let results = Arc::clone(&results);
workers.push(thread::spawn(move || {
loop {
let next = {
let mut queue = match work_queue.lock() {
Ok(guard) => guard,
Err(poisoned) => poisoned.into_inner(),
};
queue.pop_front()
};
let Some((index, path, body)) = next else {
break;
};
let result = client.call_typed_json_with_optional_timeout(path, &body, timeout);
let mut out = match results.lock() {
Ok(guard) => guard,
Err(poisoned) => poisoned.into_inner(),
};
out[index] = Some(result);
}
}));
}
let mut panicked = false;
for worker in workers {
if worker.join().is_err() {
panicked = true;
}
}
let entries = match Arc::try_unwrap(results) {
Ok(mutex) => match mutex.into_inner() {
Ok(values) => values,
Err(poisoned) => poisoned.into_inner(),
},
Err(arc) => {
let mut guard = match arc.lock() {
Ok(guard) => guard,
Err(poisoned) => poisoned.into_inner(),
};
std::mem::take(&mut *guard)
}
};
entries
.into_iter()
.map(|entry| match entry {
Some(result) => result,
None if panicked => Err(batch_worker_panic_error()),
None => Err(batch_worker_missing_result_error()),
})
.collect()
}
fn call_typed_json_with_optional_timeout<P: AsRef<str>, T, R>(
&self,
path: P,
body: &T,
timeout: Option<Duration>,
) -> Result<R, RepeError>
where
T: structio::json::Write + ?Sized,
R: ServableOwned,
{
let resp = self.call_with_body_and_timeout(
path,
QueryFormat::JsonPointer as u16,
timeout,
|builder| builder.body_json(body),
)?;
resp.decode_body()
}
fn call_typed_beve_with_optional_timeout<P: AsRef<str>, T, R>(
&self,
path: P,
body: &T,
timeout: Option<Duration>,
) -> Result<R, RepeError>
where
T: structio::beve::Write + ?Sized,
R: ServableOwned,
{
let resp = self.call_with_body_and_timeout(
path,
QueryFormat::JsonPointer as u16,
timeout,
|builder| builder.body_beve(body),
)?;
resp.decode_body()
}
fn call_typed_slice_with_optional_timeout<P, T, R>(
&self,
path: P,
body: &[T],
timeout: Option<Duration>,
) -> Result<Vec<R>, RepeError>
where
P: AsRef<str>,
T: structio::beve::NumericBytes + structio::beve::Write,
R: structio::beve::NumericBytes + for<'de> structio::beve::Read<'de> + Default,
{
let resp = self.call_with_body_and_timeout(
path,
QueryFormat::JsonPointer as u16,
timeout,
|builder| builder.body_typed_slice(body),
)?;
resp.decode_typed_slice()
}
fn call_typed_slice_aligned_with_optional_timeout<P, T, R>(
&self,
path: P,
body: &[T],
timeout: Option<Duration>,
) -> Result<Vec<R>, RepeError>
where
P: AsRef<str>,
T: structio::beve::NumericBytes + structio::beve::Write,
R: structio::beve::NumericBytes + for<'de> structio::beve::Read<'de> + Default,
{
let resp = self.call_with_body_and_timeout(
path,
QueryFormat::JsonPointer as u16,
timeout,
|builder| builder.body_aligned_typed_slice(body),
)?;
resp.decode_typed_slice()
}
fn call_with_body_and_timeout<P, F>(
&self,
path: P,
query_format: u16,
timeout: Option<Duration>,
body_fn: F,
) -> Result<Message, RepeError>
where
P: AsRef<str>,
F: FnOnce(MessageBuilder) -> MessageBuilder,
{
let id = self.next_request_id();
let builder = Message::builder()
.id(id)
.query_str(path.as_ref())
.query_format_code(query_format);
let msg = body_fn(builder).build();
let (sender, receiver) = mpsc::channel();
{
let mut pending = self
.inner
.pending
.lock()
.map_err(|_| poisoned_lock_error("client pending map"))?;
pending.insert(id, sender);
}
if let Err(err) = self.write_request(&msg) {
self.remove_pending(id);
return Err(err);
}
let resp = self.wait_for_response(id, receiver, timeout)?;
Self::validate_response(id, resp)
}
fn wait_for_response(
&self,
id: u64,
receiver: mpsc::Receiver<Result<Message, RepeError>>,
timeout: Option<Duration>,
) -> Result<Message, RepeError> {
match timeout {
Some(duration) => match receiver.recv_timeout(duration) {
Ok(value) => value,
Err(mpsc::RecvTimeoutError::Timeout) => {
self.remove_pending(id);
Err(request_timeout_error(id, duration))
}
Err(mpsc::RecvTimeoutError::Disconnected) => {
self.remove_pending(id);
Err(response_channel_closed_error(id))
}
},
None => match receiver.recv() {
Ok(value) => value,
Err(_) => {
self.remove_pending(id);
Err(response_channel_closed_error(id))
}
},
}
}
fn write_request(&self, msg: &Message) -> Result<(), RepeError> {
let mut writer = self
.inner
.writer
.lock()
.map_err(|_| poisoned_lock_error("client writer"))?;
write_message(&mut *writer, msg)?;
writer.flush()?;
Ok(())
}
fn remove_pending(&self, id: u64) {
if let Ok(mut pending) = self.inner.pending.lock() {
pending.remove(&id);
}
}
fn validate_response(expected_id: u64, resp: Message) -> Result<Message, RepeError> {
if resp.header.version != REPE_VERSION {
return Err(RepeError::VersionMismatch(resp.header.version));
}
if resp.header.id != expected_id {
return Err(RepeError::ResponseIdMismatch {
expected: expected_id,
got: resp.header.id,
});
}
if resp.header.ec != ErrorCode::Ok as u32 {
let code = ErrorCode::try_from(resp.header.ec).unwrap_or(ErrorCode::ParseError);
let msg = resp
.error_message_utf8()
.unwrap_or_else(|| code.to_string());
return Err(RepeError::ServerError { code, message: msg });
}
Ok(resp)
}
fn notify_with_body<P, F>(&self, path: P, body_fn: F) -> Result<(), RepeError>
where
P: AsRef<str>,
F: FnOnce(MessageBuilder) -> MessageBuilder,
{
self.notify_with_builder(path, QueryFormat::JsonPointer as u16, body_fn)
}
fn notify_with_builder<P, F>(
&self,
path: P,
query_format: u16,
body_fn: F,
) -> Result<(), RepeError>
where
P: AsRef<str>,
F: FnOnce(MessageBuilder) -> MessageBuilder,
{
let id = self.next_request_id();
let builder = Message::builder()
.id(id)
.notify(true)
.query_str(path.as_ref())
.query_format_code(query_format);
let msg = body_fn(builder).build();
self.write_request(&msg)
}
fn call_message_with_formats_and_timeout<P: AsRef<str>>(
&self,
path: P,
query_format: u16,
body: Option<&[u8]>,
body_format: u16,
timeout: Option<Duration>,
) -> Result<Message, RepeError> {
self.call_with_body_and_timeout(path, query_format, timeout, |builder| {
if let Some(bytes) = body {
builder
.body_bytes(bytes.to_vec())
.body_format_code(body_format)
} else {
builder.body_format_code(body_format)
}
})
}
fn notify_with_message_formats<P: AsRef<str>>(
&self,
path: P,
query_format: u16,
body: Option<&[u8]>,
body_format: u16,
) -> Result<(), RepeError> {
self.notify_with_builder(path, query_format, |builder| {
if let Some(bytes) = body {
builder
.body_bytes(bytes.to_vec())
.body_format_code(body_format)
} else {
builder.body_format_code(body_format)
}
})
}
}
fn spawn_response_loop(mut reader: BufReader<TcpStream>, inner: std::sync::Weak<ClientInner>) {
thread::spawn(move || {
loop {
let response = match read_message(&mut reader) {
Ok(message) => message,
Err(RepeError::Io(ref io_err)) if io_err.kind() == ErrorKind::Interrupted => {
continue;
}
Err(err) => {
fail_all_pending(&inner, err);
break;
}
};
let dispatch = {
let Some(inner_ref) = inner.upgrade() else {
break;
};
let mut pending_map = match inner_ref.pending.lock() {
Ok(guard) => guard,
Err(poisoned) => poisoned.into_inner(),
};
if let Some(sender) = pending_map.remove(&response.header.id) {
PendingDispatch::Matched { sender, response }
} else {
PendingDispatch::Unrecognized {
got_id: response.header.id,
}
}
};
match dispatch {
PendingDispatch::Matched { sender, response } => {
let _ = sender.send(Ok(response));
}
PendingDispatch::Unrecognized { got_id } => {
eprintln!("[repe] dropping response for unrecognized request id {got_id}");
}
}
}
});
}
fn fail_all_pending(inner: &std::sync::Weak<ClientInner>, err: RepeError) {
let Some(inner_ref) = inner.upgrade() else {
return;
};
{
let writer = match inner_ref.writer.lock() {
Ok(guard) => guard,
Err(poisoned) => poisoned.into_inner(),
};
let _ = writer.get_ref().shutdown(Shutdown::Both);
}
let waiters = {
let mut map = match inner_ref.pending.lock() {
Ok(guard) => guard,
Err(poisoned) => poisoned.into_inner(),
};
map.drain().collect::<Vec<_>>()
};
for (request_id, sender) in waiters {
let _ = sender.send(Err(clone_fatal_error_for_waiter(&err, request_id)));
}
}
fn clone_fatal_error_for_waiter(err: &RepeError, request_id: u64) -> RepeError {
match err {
RepeError::VersionMismatch(version) => RepeError::VersionMismatch(*version),
RepeError::InvalidSpec(spec) => RepeError::InvalidSpec(*spec),
RepeError::InvalidHeaderLength(length) => RepeError::InvalidHeaderLength(*length),
RepeError::LengthMismatch { expected, got } => RepeError::LengthMismatch {
expected: *expected,
got: *got,
},
RepeError::BufferTooSmall { need, have } => RepeError::BufferTooSmall {
need: *need,
have: *have,
},
RepeError::FrameLengthOverflow {
query_length,
body_length,
} => RepeError::FrameLengthOverflow {
query_length: *query_length,
body_length: *body_length,
},
RepeError::ResponseIdMismatch { expected, got } => RepeError::ResponseIdMismatch {
expected: *expected,
got: *got,
},
RepeError::Io(io_err) => RepeError::Io(std::io::Error::new(
io_err.kind(),
format!("request {request_id}: {io_err}"),
)),
RepeError::Json(err) => RepeError::Json(*err),
RepeError::Beve(err) => RepeError::Beve(*err),
RepeError::UnknownEnumValue(value) => RepeError::UnknownEnumValue(*value),
RepeError::UnexpectedBodyFormat { expected, got } => RepeError::UnexpectedBodyFormat {
expected: *expected,
got: *got,
},
RepeError::ServerError { code, message } => RepeError::ServerError {
code: *code,
message: message.clone(),
},
RepeError::MessageTooLarge { size, limit } => RepeError::MessageTooLarge {
size: *size,
limit: *limit,
},
}
}
fn poisoned_lock_error(name: &str) -> RepeError {
RepeError::Io(std::io::Error::other(format!("{name} lock poisoned")))
}
fn request_timeout_error(request_id: u64, timeout: Duration) -> RepeError {
RepeError::Io(std::io::Error::new(
ErrorKind::TimedOut,
format!(
"request {request_id} timed out after {}ms",
timeout.as_millis()
),
))
}
fn response_channel_closed_error(request_id: u64) -> RepeError {
RepeError::Io(std::io::Error::new(
ErrorKind::ConnectionAborted,
format!("response channel closed for request {request_id}"),
))
}
fn batch_worker_panic_error() -> RepeError {
RepeError::Io(std::io::Error::other("batch worker panicked"))
}
fn batch_worker_missing_result_error() -> RepeError {
RepeError::Io(std::io::Error::other("batch worker missing result"))
}
fn batch_worker_count(request_count: usize) -> usize {
if request_count == 0 {
return 0;
}
let parallelism = thread::available_parallelism()
.map(|count| count.get())
.unwrap_or(1);
let cap = parallelism.saturating_mul(4).clamp(1, MAX_BATCH_WORKERS);
request_count.min(cap)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn batch_worker_count_is_bounded() {
assert_eq!(batch_worker_count(0), 0);
assert_eq!(batch_worker_count(1), 1);
let count = batch_worker_count(usize::MAX);
assert!(count >= 1);
assert!(count <= MAX_BATCH_WORKERS);
}
}