From 1c15baaa71bd8c1963139c2e5937bdb559c063c4 Mon Sep 17 00:00:00 2001 From: Hamish Peebles Date: Thu, 8 Feb 2024 09:19:32 +0000 Subject: [PATCH] Remove `canister_id` from state of `Runtime` implementations (#10) --- Cargo.lock | 4 +++- Cargo.toml | 1 + client/Cargo.toml | 1 + client/agent_runtime/Cargo.toml | 2 +- client/agent_runtime/src/lib.rs | 8 +++----- client/cdk_runtime/Cargo.toml | 2 +- client/cdk_runtime/src/lib.rs | 29 +++++++++++++++-------------- client/src/lib.rs | 21 ++++++++++++++++++--- 8 files changed, 43 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f829cde..db744e8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -504,6 +504,7 @@ name = "event_sink_client" version = "0.1.0" dependencies = [ "event_sink_canister", + "ic_principal", ] [[package]] @@ -514,6 +515,7 @@ dependencies = [ "event_sink_canister", "event_sink_client", "ic-agent", + "ic_principal", "rand", "tokio", "tokio-util", @@ -523,11 +525,11 @@ dependencies = [ name = "event_sink_client_cdk_runtime" version = "0.1.0" dependencies = [ - "candid", "event_sink_canister", "event_sink_client", "ic-cdk", "ic-cdk-timers", + "ic_principal", "rand", ] diff --git a/Cargo.toml b/Cargo.toml index 58f1191..2a1360c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ candid = "0.10.3" ic-agent = "0.32.0" ic-cdk = "=0.12.0" ic-cdk-timers = "0.6.0" +ic_principal = "0.1.1" ic-stable-structures = "0.6.2" pocket-ic = "2.0.1" rand = "0.8.5" diff --git a/client/Cargo.toml b/client/Cargo.toml index 4d4a942..22bf6ae 100644 --- a/client/Cargo.toml +++ b/client/Cargo.toml @@ -6,4 +6,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +ic_principal.workspace = true event_sink_canister.path = "../canister/api" diff --git a/client/agent_runtime/Cargo.toml b/client/agent_runtime/Cargo.toml index 31aa714..655e5a0 100644 --- a/client/agent_runtime/Cargo.toml +++ b/client/agent_runtime/Cargo.toml @@ -6,11 +6,11 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] - candid.workspace = true event_sink_canister.path = "../../canister/api" event_sink_client.path = ".." ic-agent.workspace = true +ic_principal.workspace = true rand.workspace = true tokio.workspace = true tokio.features = ["full"] diff --git a/client/agent_runtime/src/lib.rs b/client/agent_runtime/src/lib.rs index 737bd79..9c06a65 100644 --- a/client/agent_runtime/src/lib.rs +++ b/client/agent_runtime/src/lib.rs @@ -1,21 +1,19 @@ -use candid::Principal; use event_sink_canister::{IdempotentEvent, PushEventsArgs, TimestampMillis}; use event_sink_client::Runtime; use ic_agent::Agent; +use ic_principal::Principal; use rand::random; use std::time::{Duration, SystemTime, UNIX_EPOCH}; use tokio_util::sync::CancellationToken; pub struct AgentRuntime { - canister_id: Principal, agent: Agent, scheduler_task_cancellation_token: Option, } impl AgentRuntime { - pub fn new(canister_id: Principal, agent: Agent) -> AgentRuntime { + pub fn new(agent: Agent) -> AgentRuntime { AgentRuntime { - canister_id, agent, scheduler_task_cancellation_token: None, } @@ -44,11 +42,11 @@ impl Runtime for AgentRuntime { fn flush( &mut self, + canister_id: Principal, events: Vec, trigger_retry: F, ) { self.cancel_scheduler_task(); - let canister_id = self.canister_id; let agent = self.agent.clone(); tokio::spawn(async move { flush_async(canister_id, agent, events, trigger_retry).await }); diff --git a/client/cdk_runtime/Cargo.toml b/client/cdk_runtime/Cargo.toml index 76e7c19..8e44308 100644 --- a/client/cdk_runtime/Cargo.toml +++ b/client/cdk_runtime/Cargo.toml @@ -6,9 +6,9 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -candid.workspace = true event_sink_canister.path = "../../canister/api" event_sink_client.path = ".." ic-cdk.workspace = true ic-cdk-timers.workspace = true +ic_principal.workspace = true rand.workspace = true diff --git a/client/cdk_runtime/src/lib.rs b/client/cdk_runtime/src/lib.rs index 54b44b7..0b146d4 100644 --- a/client/cdk_runtime/src/lib.rs +++ b/client/cdk_runtime/src/lib.rs @@ -1,29 +1,17 @@ -use candid::Principal; use event_sink_canister::{IdempotentEvent, PushEventsArgs, TimestampMillis}; use event_sink_client::Runtime; use ic_cdk_timers::TimerId; +use ic_principal::Principal; use rand::rngs::StdRng; use rand::{Rng, SeedableRng}; use std::time::Duration; pub struct CdkRuntime { - canister_id: Principal, rng: StdRng, scheduled_flush_timer: Option, } impl CdkRuntime { - pub fn new(canister_id: Principal) -> CdkRuntime { - let mut seed = [0; 32]; - seed[..8].copy_from_slice(&ic_cdk::api::time().to_ne_bytes()); - - CdkRuntime { - canister_id, - rng: StdRng::from_seed(seed), - scheduled_flush_timer: None, - } - } - fn clear_timer(&mut self) { if let Some(timer_id) = self.scheduled_flush_timer.take() { ic_cdk_timers::clear_timer(timer_id); @@ -39,11 +27,12 @@ impl Runtime for CdkRuntime { fn flush( &mut self, + canister_id: Principal, events: Vec, trigger_retry: F, ) { self.clear_timer(); - ic_cdk::spawn(flush_async(self.canister_id, events, trigger_retry)) + ic_cdk::spawn(flush_async(canister_id, events, trigger_retry)) } fn rng(&mut self) -> u128 { @@ -67,3 +56,15 @@ async fn flush_async( trigger_retry(); } } + +impl Default for CdkRuntime { + fn default() -> Self { + let mut seed = [0; 32]; + seed[..8].copy_from_slice(&ic_cdk::api::time().to_ne_bytes()); + + CdkRuntime { + rng: StdRng::from_seed(seed), + scheduled_flush_timer: None, + } + } +} diff --git a/client/src/lib.rs b/client/src/lib.rs index 40afc12..f84d717 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -1,4 +1,5 @@ use event_sink_canister::{IdempotentEvent, TimestampMillis}; +use ic_principal::Principal; use std::mem; use std::sync::{Arc, Mutex, MutexGuard}; use std::time::Duration; @@ -11,6 +12,7 @@ pub struct Client { } struct ClientInner { + event_sink_canister_id: Principal, runtime: R, flush_delay: Duration, max_batch_size: usize, @@ -24,6 +26,7 @@ pub trait Runtime { fn schedule_flush(&mut self, delay: Duration, callback: F); fn flush( &mut self, + event_sync_canister_id: Principal, events: Vec, trigger_retry: F, ); @@ -38,6 +41,7 @@ impl Client { } pub struct ClientBuilder { + event_sink_canister_id: Principal, runtime: R, flush_delay: Option, max_batch_size: Option, @@ -45,8 +49,9 @@ pub struct ClientBuilder { } impl ClientBuilder { - pub fn new(runtime: R) -> ClientBuilder { + pub fn new(event_sink_canister_id: Principal, runtime: R) -> ClientBuilder { ClientBuilder { + event_sink_canister_id, runtime, flush_delay: None, max_batch_size: None, @@ -74,6 +79,7 @@ impl ClientBuilder { let max_batch_size = self.max_batch_size.unwrap_or(DEFAULT_MAX_BATCH_SIZE) as usize; let client = Client { inner: Arc::new(Mutex::new(ClientInner::new( + self.event_sink_canister_id, self.runtime, flush_delay, max_batch_size, @@ -115,9 +121,12 @@ impl Client { }; let clone = self.clone(); + let event_sink_canister_id = guard.event_sink_canister_id; guard .runtime - .flush(events.clone(), move || clone.requeue_events(events)); + .flush(event_sink_canister_id, events.clone(), move || { + clone.requeue_events(events) + }); } } @@ -164,8 +173,14 @@ impl Clone for Client { } impl ClientInner { - pub fn new(runtime: R, flush_delay: Duration, max_batch_size: usize) -> ClientInner { + pub fn new( + event_sink_canister_id: Principal, + runtime: R, + flush_delay: Duration, + max_batch_size: usize, + ) -> ClientInner { ClientInner { + event_sink_canister_id, runtime, flush_delay, max_batch_size,