Skip to content

Commit

Permalink
feature: send batch request
Browse files Browse the repository at this point in the history
  • Loading branch information
prestwich committed Jul 16, 2023
1 parent cf00429 commit ed0a5e8
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 15 deletions.
14 changes: 11 additions & 3 deletions crates/transports/src/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ use tower::Service;

use crate::{
error::{RpcResult, TransportError},
types::{Id, JsonRpcRequest, JsonRpcResponse, RpcReturn},
rpc_types::{Id, JsonRpcRequest, JsonRpcResponse, RpcReturn},
};

type Channel = oneshot::Sender<RpcResult<Box<RawValue>, TransportError>>;
type ChannelMap = HashMap<Id, Channel>;

/// A Batch JSON-RPC request, awaiting dispatch.
#[derive(Debug, Default)]
pub struct BatchRequest<T> {
transport: T,
Expand All @@ -27,6 +28,7 @@ pub struct BatchRequest<T> {
channels: ChannelMap,
}

/// Awaits a single response for a request that has been included in a batch.
pub struct Waiter<Resp> {
rx: oneshot::Receiver<RpcResult<Box<RawValue>, TransportError>>,
_resp: PhantomData<Resp>,
Expand Down Expand Up @@ -84,9 +86,15 @@ impl<T> BatchRequest<T> {
self.requests.push(request);
rx
}
}

pub fn decompose(self) -> (Vec<JsonRpcRequest>, ChannelMap) {
(self.requests, self.channels)
impl<T> BatchRequest<T>
where
T: Service<Vec<JsonRpcRequest>, Response = Vec<JsonRpcResponse>, Error = TransportError>,
{
/// Send the batch future via its connection.
pub fn send(self) -> BatchFuture<T> {
BatchFuture::Prepared(self)
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/transports/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use tower::Service;

use crate::{
error::{RpcResult, TransportError},
types::{JsonRpcRequest, JsonRpcResponse, RpcParam, RpcReturn},
rpc_types::{JsonRpcRequest, JsonRpcResponse, RpcParam, RpcReturn},
};

#[pin_project::pin_project(project = CallStateProj)]
Expand Down
4 changes: 2 additions & 2 deletions crates/transports/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
batch::BatchRequest,
call::RpcCall,
error::TransportError,
types::{Id, JsonRpcRequest, JsonRpcResponse, RpcParam, RpcReturn},
rpc_types::{Id, JsonRpcRequest, JsonRpcResponse, RpcParam, RpcReturn},
utils::to_json_raw_value,
};

Expand Down Expand Up @@ -79,6 +79,6 @@ mod test {
fn basic_instantiation() {
let h: RpcClient<Http<reqwest::Client>> = "http://localhost:8545".parse().unwrap();

assert_eq!(h.is_local(), true);
assert!(h.is_local());
}
}
2 changes: 1 addition & 1 deletion crates/transports/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::types::{ErrorPayload, JsonRpcResponse, ResponsePayload, RpcReturn};
use crate::rpc_types::{ErrorPayload, JsonRpcResponse, ResponsePayload, RpcReturn};

use serde_json::value::RawValue;
use std::{error::Error as StdError, fmt::Debug};
Expand Down
26 changes: 19 additions & 7 deletions crates/transports/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
pub mod call;
pub mod common;
mod call;
pub use call::RpcCall;

mod common;
pub use common::Authorization;

pub mod connection;
pub mod error;
pub mod utils;
pub use connection::RpcClient;

mod error;
pub use error::{RpcResult, TransportError};

pub(crate) mod utils;

mod batch;
pub use batch::BatchRequest;

mod transports;
pub use transports::Http;

pub mod batch;
pub mod transports;
pub mod types;
pub mod rpc_types;
File renamed without changes.
2 changes: 1 addition & 1 deletion crates/transports/src/transports/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tower::Service;
use crate::{
connection::RpcClient,
error::TransportError,
types::{JsonRpcRequest, JsonRpcResponse},
rpc_types::{JsonRpcRequest, JsonRpcResponse},
};

impl<T> RpcClient<Http<T>>
Expand Down
1 change: 1 addition & 0 deletions crates/transports/src/transports/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod http;
pub use http::Http;

0 comments on commit ed0a5e8

Please sign in to comment.