Skip to content

Commit

Permalink
Change sync call toggle to depend on transport
Browse files Browse the repository at this point in the history
  • Loading branch information
DSharifi committed Jul 22, 2024
1 parent 7150190 commit 1c7195c
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 12 deletions.
4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ rust-version = "1.75.0"
license = "Apache-2.0"

[workspace.dependencies]
ic-agent = { path = "ic-agent", version = "0.36.0", default-features = false, features = [
"sync_call",
] }
ic-agent = { path = "ic-agent", version = "0.36.0", default-features = false }
ic-utils = { path = "ic-utils", version = "0.36.0" }
ic-transport-types = { path = "ic-transport-types", version = "0.36.0" }

Expand Down
2 changes: 1 addition & 1 deletion ic-agent/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ web-sys = { version = "0.3", features = [

[features]
default = ["pem", "reqwest"]
sync_call = []
experimental_sync_call = []
reqwest = ["dep:reqwest"]
hyper = [
"dep:hyper",
Expand Down
20 changes: 16 additions & 4 deletions ic-agent/src/agent/agent_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,21 @@ use crate::agent::http_transport::route_provider::{RoundRobinRouteProvider, Rout
#[cfg(all(target_family = "wasm", feature = "wasm-bindgen"))]
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);

fn make_transport(url: &str) -> ReqwestTransport {
let transport = ReqwestTransport::create(url).unwrap();
#[cfg(feature = "experimental_sync_call")]
{
transport.with_use_call_v3_endpoint()
}
#[cfg(not(feature = "experimental_sync_call"))]
{
transport
}
}

fn make_agent(url: &str) -> Agent {
Agent::builder()
.with_transport(ReqwestTransport::create(url).unwrap())
.with_transport(make_transport(url))
.with_verify_query_signatures(false)
.build()
.unwrap()
Expand Down Expand Up @@ -215,7 +227,7 @@ async fn query_rejected() -> Result<(), AgentError> {
#[cfg_attr(not(target_family = "wasm"), tokio::test)]
#[cfg_attr(target_family = "wasm", wasm_bindgen_test)]
async fn call_error() -> Result<(), AgentError> {
let version = if cfg!(feature = "sync_call") {
let version = if cfg!(feature = "experimental_sync_call") {
"3"
} else {
"2"
Expand Down Expand Up @@ -258,7 +270,7 @@ async fn call_rejected() -> Result<(), AgentError> {

let body = serde_cbor::to_vec(&reject_body).unwrap();

let version = if cfg!(feature = "sync_call") {
let version = if cfg!(feature = "experimental_sync_call") {
"3"
} else {
"2"
Expand Down Expand Up @@ -304,7 +316,7 @@ async fn call_rejected_without_error_code() -> Result<(), AgentError> {

let body = serde_cbor::to_vec(&reject_body).unwrap();

let version = if cfg!(feature = "sync_call") {
let version = if cfg!(feature = "experimental_sync_call") {
"3"
} else {
"2"
Expand Down
15 changes: 13 additions & 2 deletions ic-agent/src/agent/http_transport/hyper_transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub struct HyperTransport<B1, S = Client<HttpsConnector<HttpConnector>, B1>> {
#[allow(dead_code)]
max_tcp_error_retries: usize,
service: S,
use_call_v3_endpoint: bool,
}

/// Trait representing the contraints on [`HttpBody`] that [`HyperTransport`] requires
Expand Down Expand Up @@ -125,6 +126,7 @@ where
service,
max_response_body_size: None,
max_tcp_error_retries: 0,
use_call_v3_endpoint: false,
})
}

Expand All @@ -144,6 +146,15 @@ where
}
}

/// Use call v3 endpoint for update calls.
#[cfg(feature = "experimental_sync_call")]
pub fn with_use_call_v3_endpoint(self) -> Self {
Self {
use_call_v3_endpoint: true,
..self
}
}

async fn request(
&self,
method: Method,
Expand Down Expand Up @@ -270,7 +281,7 @@ where
envelope: Vec<u8>,
) -> AgentFuture<TransportCallResponse> {
Box::pin(async move {
let api_version = if cfg!(feature = "sync_call") {
let api_version = if self.use_call_v3_endpoint {
"v3"
} else {
"v2"
Expand All @@ -290,7 +301,7 @@ where
}

// status_code == OK (200)
if cfg!(feature = "sync_call") {
if self.use_call_v3_endpoint {
serde_cbor::from_slice(&response_body).map_err(AgentError::InvalidCborData)
} else {
let reject_response = serde_cbor::from_slice::<RejectResponse>(&response_body)
Expand Down
15 changes: 13 additions & 2 deletions ic-agent/src/agent/http_transport/reqwest_transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub struct ReqwestTransport {
max_response_body_size: Option<usize>,
#[allow(dead_code)]
max_tcp_error_retries: usize,
use_call_v3_endpoint: bool,
}

impl ReqwestTransport {
Expand Down Expand Up @@ -68,6 +69,7 @@ impl ReqwestTransport {
client,
max_response_body_size: None,
max_tcp_error_retries: 0,
use_call_v3_endpoint: false,
})
}

Expand All @@ -87,6 +89,15 @@ impl ReqwestTransport {
}
}

/// Use call v3 endpoint for update calls.
#[cfg(feature = "experimental_sync_call")]
pub fn with_use_call_v3_endpoint(self) -> Self {
ReqwestTransport {
use_call_v3_endpoint: true,
..self
}
}

async fn request(
&self,
method: Method,
Expand Down Expand Up @@ -221,7 +232,7 @@ impl Transport for ReqwestTransport {
envelope: Vec<u8>,
) -> AgentFuture<TransportCallResponse> {
Box::pin(async move {
let api_version = if cfg!(feature = "sync_call") {
let api_version = if self.use_call_v3_endpoint {
"v3"
} else {
"v2"
Expand All @@ -241,7 +252,7 @@ impl Transport for ReqwestTransport {
}

// status_code == OK (200)
if cfg!(feature = "sync_call") {
if self.use_call_v3_endpoint {
serde_cbor::from_slice(&response_body).map_err(AgentError::InvalidCborData)
} else {
let reject_response = serde_cbor::from_slice::<RejectResponse>(&response_body)
Expand Down

0 comments on commit 1c7195c

Please sign in to comment.