razor_stream/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![cfg_attr(docsrs, allow(unused_attributes))]
3
4//! # razor-stream
5//!
6//! This crate provides a low-level streaming interface for `razor-rpc`.
7//! It is used for stream processing and is part of the modular design of `razor-rpc`.
8//!
9//! If you are looking for a high-level remote API call interface, use [`razor-rpc`](https://docs.rs/razor-rpc) instead.
10//!
11//! ## Components
12//!
13//! `razor-rpc` is designed to be modular and pluggable. It is a collection of crates that provide different functionalities:
14//!
15//! - [`razor-rpc-codec`](https://docs.rs/razor-rpc-codec): Provides codecs for serialization, such as `msgpack`.
16//! - Async runtime support by [`orb`](https://docs.rs/orb):
17//!   - [`orb-tokio`](https://docs.rs/orb-tokio): A runtime adapter for the `tokio` runtime.
18//!   - [`orb-smol`](https://docs.rs/orb-smol): A runtime adapter for the `smol` runtime.
19//! - Transports can be implemented with a raw socket, without the overhead of the HTTP protocol:
20//!   - [`razor-rpc-tcp`](https://docs.rs/razor-rpc-tcp): A TCP transport implementation.
21//!
22//! ## The Design
23//!
24//! Our implementation is designed to optimize throughput and lower
25//! CPU consumption for high-performance services.
26//!
27//! Each connection is a full-duplex, multiplexed stream.
28//! There's a `seq` ID assigned to a packet to track
29//! a request and response. The timeout of a packet is checked in batches every second.
30//! We utilize the [crossfire](https://docs.rs/crossfire) channel for parallelizing the work with
31//! coroutines.
32//!
33//! With an [ClientStream](crate::client::stream::ClientStream), the request packets sent in sequence,
34//! and wait with a sliding window throttler controlling the number of in-flight packets.
35//! An internal timer then registers the request through a channel, and when the response
36//! is received, it can optionally notify the user through a user-defined channel or another mechanism.
37//!
38//! [ClientPool](crate::client::ClientPool) and [FailoverPool](crate::client::FailoverPool) are provided on top of `ClientStream` for user.
39//!
40//! In an [RpcServer](crate::server::RpcServer), for each connection, there is one coroutine to read requests and one
41//! coroutine to write responses. Requests can be dispatched with a user-defined
42//! [Dispatch](crate::server::dispatch::Dispatch) trait implementation.
43//!
44//! Responses are received through a channel wrapped in [RespNoti](crate::server::task::RespNoti).
45//!
46//! ## Protocol
47//!
48//! The details are described in [crate::proto].
49//!
50//! The packet starts with a fixed-length header and is followed by a variable-length body.
51//! An [RpcAction](crate::proto::RpcAction) represents the type of packet.
52//! The action type is either numeric or a string.
53//!
54//! The request body contains a mandatory structured message and optional blob data.
55//!
56//! The response for each request either returns successfully with an optional structured message and
57//! optional blob data (the response can be empty), or it returns with an RpcError. The error type can
58//! be numeric (like a Unix errno), text (for user-customized errors), or a statically predefined error
59//! string (for errors that occur during socket communication or encoding/decoding)
60//!
61//! ## Usage
62//!
63//! You can refer to the [test case](https://github.com/NaturalIO/razor-rpc/blob/master/test-suite/src/stream/) for example.
64//!
65
66#[macro_use]
67extern crate captains_log;
68
69pub mod buffer;
70pub mod client;
71pub mod error;
72pub mod proto;
73pub mod server;
74// re-export for macros, so that user don't need to use multiple crates
75pub use razor_rpc_codec::Codec;