Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
docs: add comments to struct fields
Browse files Browse the repository at this point in the history
  • Loading branch information
prestwich committed Feb 28, 2023
1 parent eb6976e commit 77c8ff8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
40 changes: 24 additions & 16 deletions ethers-providers/src/rpc/transports/ws/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,17 @@ use serde_json::value::RawValue;
use super::{types::*, WsClientError};
use tracing::{error, trace};

/// `WsBackend` dispatches requests and routes responses and notifications. It
/// also has a simple ping-based keepalive (when not compiled to wasm), to
/// prevent inactivity from triggering server-side closes
///
/// The `WsBackend` shuts down when instructed to by the `RequestManager` or
/// when the `RequestManager` drops (because the inbound channel will close)
pub struct WsBackend {
server: InternalStream,

handler: mpsc::UnboundedSender<PubSubItem>,
error: oneshot::Sender<()>,

to_dispatch: mpsc::UnboundedReceiver<Box<RawValue>>,
shutdown: oneshot::Receiver<()>,
}

/// `BackendDriver` drives a specific `WsBackend`. It can be used to issue
/// requests, receive responses, see errors, and shut down the backend.
pub struct BackendDriver {
// Pubsub items from the backend, received via WS
pub to_handle: mpsc::UnboundedReceiver<PubSubItem>,
// Notification from the backend of a terminal error
pub error: oneshot::Receiver<()>,

// Requests that the backend should dispatch
pub dispatcher: mpsc::UnboundedSender<Box<RawValue>>,
// Notify the backend of intentional shutdown
shutdown: oneshot::Sender<()>,
}

Expand All @@ -39,6 +27,26 @@ impl BackendDriver {
}
}

/// `WsBackend` dispatches requests and routes responses and notifications. It
/// also has a simple ping-based keepalive (when not compiled to wasm), to
/// prevent inactivity from triggering server-side closes
///
/// The `WsBackend` shuts down when instructed to by the `RequestManager` or
/// when the `RequestManager` drops (because the inbound channel will close)
pub struct WsBackend {
server: InternalStream,

// channel to the manager, through which to send items received via WS
handler: mpsc::UnboundedSender<PubSubItem>,
// notify manager of an error causing this task to halt
error: oneshot::Sender<()>,

// channel of inbound requests to dispatch
to_dispatch: mpsc::UnboundedReceiver<Box<RawValue>>,
// notification from manager of intentional shutdown
shutdown: oneshot::Receiver<()>,
}

impl WsBackend {
#[cfg(target_arch = "wasm32")]
pub async fn connect(
Expand Down
19 changes: 14 additions & 5 deletions ethers-providers/src/rpc/transports/ws/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ pub const DEFAULT_RECONNECTS: usize = 5;
/// subscription id allows the subscription to behave consistently across
/// reconnections
pub struct SubscriptionManager {
// Active subs indexed by request id
subs: BTreeMap<u64, ActiveSub>,
// Maps active server-side IDs to local subscription IDs
aliases: HashMap<U256, u64>,
// used to communicate to the WsClient
// Used to share notification channels with the WsClient(s)
channel_map: SharedChannelMap,
}

Expand Down Expand Up @@ -185,14 +187,20 @@ impl SubscriptionManager {
/// been dropped (because all instruction channel `UnboundedSender` instances
/// will have dropped).
pub struct RequestManager {
// Next JSON-RPC Request ID
id: AtomicU64,
// How many times we should reconnect the backend before erroring
reconnects: usize,
// Subscription manager
subs: SubscriptionManager,
// Requests for which a response has not been receivedc
reqs: BTreeMap<u64, InFlight>,
// Control of the active WS backend
backend: BackendDriver,
// The URL and optional auth info for the connection
conn: ConnectionDetails,
// Instructions from the user-facing providers
instructions: mpsc::UnboundedReceiver<Instruction>,

reconnects: usize,
}

impl RequestManager {
Expand All @@ -218,12 +226,12 @@ impl RequestManager {
Ok((
Self {
id: Default::default(),
reconnects,
subs: SubscriptionManager::new(channel_map.clone()),
reqs: Default::default(),
backend,
conn,
instructions: instructions_rx,
reconnects,
},
WsClient { instructions: instructions_tx, channel_map },
))
Expand Down Expand Up @@ -359,7 +367,8 @@ impl RequestManager {
let fut = async move {
let result = loop {
// We bias the loop so that we always handle messages before
// reconnecting, and always reconnect before
// reconnecting, and always reconnect before dispatching new
// requests
select_biased! {
item_opt = self.backend.to_handle.next() => {
match item_opt {
Expand Down

0 comments on commit 77c8ff8

Please sign in to comment.