From 1deeeba0c05bcb3b18097734bd33a4c9012ee387 Mon Sep 17 00:00:00 2001 From: nociza Date: Sun, 25 Dec 2022 03:36:31 -0800 Subject: [PATCH 01/15] new file and boilerplate --- src/extensions/storage_macro/redis.rs | 33 +++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/extensions/storage_macro/redis.rs diff --git a/src/extensions/storage_macro/redis.rs b/src/extensions/storage_macro/redis.rs new file mode 100644 index 0000000..9d69734 --- /dev/null +++ b/src/extensions/storage_macro/redis.rs @@ -0,0 +1,33 @@ +use async_recursion::async_recursion; + +type Error = Box; + +impl crate::application::CoLink { + #[async_recursion] + pub(crate) async fn _create_entry_redis( + &self, + key_name: &str, + payload: &[u8], + ) -> Result { + Ok("".to_string()) + } + + #[async_recursion] + pub(crate) async fn _read_entry_redis(&self, key_name: &str) -> Result, Error> { + Ok(vec![]) + } + + #[async_recursion] + pub(crate) async fn _update_entry_redis( + &self, + key_name: &str, + payload: &[u8], + ) -> Result { + Ok("".to_string()) + } + + #[async_recursion] + pub(crate) async fn _delete_entry_redis(&self, key_name: &str) -> Result { + Ok("".to_string()) + } +} From 0a0c4a014e6a341dac722da073ae01d352cad194 Mon Sep 17 00:00:00 2001 From: nociza Date: Sun, 25 Dec 2022 03:37:26 -0800 Subject: [PATCH 02/15] more boilerplate --- src/extensions/storage_macro.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/extensions/storage_macro.rs b/src/extensions/storage_macro.rs index 45cff94..1a3fbea 100644 --- a/src/extensions/storage_macro.rs +++ b/src/extensions/storage_macro.rs @@ -1,4 +1,5 @@ mod chunk; +mod redis; type Error = Box; @@ -36,6 +37,10 @@ impl crate::application::CoLink { self._create_entry_chunk(string_before.as_str(), payload) .await } + "redis" => { + self._create_entry_redis(string_before.as_str(), payload) + .await + } _ => Err(format!( "invalid storage macro, found {} in key name {}", macro_type, key_name @@ -48,6 +53,7 @@ impl crate::application::CoLink { let (string_before, macro_type, _) = self._parse_macro(key_name); match macro_type.as_str() { "chunk" => self._read_entry_chunk(string_before.as_str()).await, + "redis" => self._read_entry_redis(string_before.as_str()).await, _ => Err(format!( "invalid storage macro, found {} in key name {}", macro_type, key_name @@ -67,6 +73,10 @@ impl crate::application::CoLink { self._update_entry_chunk(string_before.as_str(), payload) .await } + "redis" => { + self._update_entry_redis(string_before.as_str(), payload) + .await + } _ => Err(format!( "invalid storage macro, found {} in key name {}", macro_type, key_name @@ -79,6 +89,7 @@ impl crate::application::CoLink { let (string_before, macro_type, _) = self._parse_macro(key_name); match macro_type.as_str() { "chunk" => self._delete_entry_chunk(string_before.as_str()).await, + "redis" => self._delete_entry_redis(string_before.as_str()).await, _ => Err(format!( "invalid storage macro, found {} in key name {}", macro_type, key_name From 612a1fe09f5874812fea688f0c747386e3cb50ab Mon Sep 17 00:00:00 2001 From: nociza Date: Fri, 30 Dec 2022 17:01:22 -0800 Subject: [PATCH 03/15] get redis connection --- Cargo.toml | 1 + src/extensions/storage_macro.rs | 24 ++++--- src/extensions/storage_macro/db_redis.rs | 87 ++++++++++++++++++++++++ src/extensions/storage_macro/redis.rs | 33 --------- 4 files changed, 103 insertions(+), 42 deletions(-) create mode 100644 src/extensions/storage_macro/db_redis.rs delete mode 100644 src/extensions/storage_macro/redis.rs diff --git a/Cargo.toml b/Cargo.toml index eb27d2c..a8f1717 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ futures-lite = "1.12" lapin = "2.1" prost = "0.10" rand = { version = "0.8.4", features = ["std_rng"] } +redis = "0.22.1" secp256k1 = { version = "0.21.2", features = ["rand-std"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" diff --git a/src/extensions/storage_macro.rs b/src/extensions/storage_macro.rs index 1a3fbea..2335265 100644 --- a/src/extensions/storage_macro.rs +++ b/src/extensions/storage_macro.rs @@ -1,5 +1,5 @@ mod chunk; -mod redis; +mod db_redis; type Error = Box; @@ -31,14 +31,14 @@ impl crate::application::CoLink { key_name: &str, payload: &[u8], ) -> Result { - let (string_before, macro_type, _) = self._parse_macro(key_name); + let (string_before, macro_type, string_after) = self._parse_macro(key_name); match macro_type.as_str() { "chunk" => { self._create_entry_chunk(string_before.as_str(), payload) .await } "redis" => { - self._create_entry_redis(string_before.as_str(), payload) + self._create_entry_redis(string_before.as_str(), string_after.as_str(), payload) .await } _ => Err(format!( @@ -50,10 +50,13 @@ impl crate::application::CoLink { } pub(crate) async fn _sm_read_entry(&self, key_name: &str) -> Result, Error> { - let (string_before, macro_type, _) = self._parse_macro(key_name); + let (string_before, macro_type, string_after) = self._parse_macro(key_name); match macro_type.as_str() { "chunk" => self._read_entry_chunk(string_before.as_str()).await, - "redis" => self._read_entry_redis(string_before.as_str()).await, + "redis" => { + self._read_entry_redis(string_before.as_str(), string_after.as_str()) + .await + } _ => Err(format!( "invalid storage macro, found {} in key name {}", macro_type, key_name @@ -67,14 +70,14 @@ impl crate::application::CoLink { key_name: &str, payload: &[u8], ) -> Result { - let (string_before, macro_type, _) = self._parse_macro(key_name); + let (string_before, macro_type, string_after) = self._parse_macro(key_name); match macro_type.as_str() { "chunk" => { self._update_entry_chunk(string_before.as_str(), payload) .await } "redis" => { - self._update_entry_redis(string_before.as_str(), payload) + self._update_entry_redis(string_before.as_str(), string_after.as_str(), payload) .await } _ => Err(format!( @@ -86,10 +89,13 @@ impl crate::application::CoLink { } pub(crate) async fn _sm_delete_entry(&self, key_name: &str) -> Result { - let (string_before, macro_type, _) = self._parse_macro(key_name); + let (string_before, macro_type, string_after) = self._parse_macro(key_name); match macro_type.as_str() { "chunk" => self._delete_entry_chunk(string_before.as_str()).await, - "redis" => self._delete_entry_redis(string_before.as_str()).await, + "redis" => { + self._delete_entry_redis(string_before.as_str(), string_after.as_str()) + .await + } _ => Err(format!( "invalid storage macro, found {} in key name {}", macro_type, key_name diff --git a/src/extensions/storage_macro/db_redis.rs b/src/extensions/storage_macro/db_redis.rs new file mode 100644 index 0000000..60b2077 --- /dev/null +++ b/src/extensions/storage_macro/db_redis.rs @@ -0,0 +1,87 @@ +use async_recursion::async_recursion; +use redis::Commands; + +type Error = Box; + +impl crate::application::CoLink { + fn _get_con_from_address(&self, redis_address: &str) -> Result { + let client = redis::Client::open(redis_address)?; + let con = client.get_connection()?; + Ok(con) + } + + async fn _get_con_from_stored_credentials( + &self, + address: &str, + ) -> Result { + // get credentials from storage + let redis_username = self + .read_entry(format!("{}:{}", address, "redis_username").as_str()) + .await?; + let redis_username_string = String::from_utf8(redis_username)?; + let redis_password = self + .read_entry(format!("{}:{}", address, "redis_password").as_str()) + .await?; + let redis_password_string = String::from_utf8(redis_password)?; + let redis_host_name = self + .read_entry(format!("{}:{}", address, "redis_host_name").as_str()) + .await?; + let redis_host_name_string = String::from_utf8(redis_host_name)?; + + // get the uri scheme from storage, if doesn't exist, default to non-ssl + let uri_scheme = self + .read_entry(format!("{}:{}", address, "redis_use_secure").as_str()) + .await; + let mut uri_scheme_string = "redis://".to_string(); + if uri_scheme.is_ok() { + uri_scheme_string = "rediss://".to_string(); + } + + let redis_address = format!( + "{}://{}:{}@{}", + uri_scheme_string, redis_username_string, redis_password_string, redis_host_name_string + ); + self._get_con_from_address(&redis_address) + } + + #[async_recursion] + pub(crate) async fn _create_entry_redis( + &self, + address: &str, + key_name: &str, + payload: &[u8], + ) -> Result { + let mut con = self._get_con_from_stored_credentials(address).await?; + let response = con.set(key_name, payload)?; + + Ok("".to_string()) + } + + #[async_recursion] + pub(crate) async fn _read_entry_redis( + &self, + address: &str, + key_name: &str, + ) -> Result, Error> { + Ok(vec![]) + } + + #[async_recursion] + pub(crate) async fn _update_entry_redis( + &self, + address: &str, + key_name: &str, + payload: &[u8], + ) -> Result { + Ok("".to_string()) + } + + #[async_recursion] + pub(crate) async fn _delete_entry_redis( + &self, + address: &str, + key_name: &str, + ) -> Result { + Ok("".to_string()) + } +} diff --git a/src/extensions/storage_macro/redis.rs b/src/extensions/storage_macro/redis.rs deleted file mode 100644 index 9d69734..0000000 --- a/src/extensions/storage_macro/redis.rs +++ /dev/null @@ -1,33 +0,0 @@ -use async_recursion::async_recursion; - -type Error = Box; - -impl crate::application::CoLink { - #[async_recursion] - pub(crate) async fn _create_entry_redis( - &self, - key_name: &str, - payload: &[u8], - ) -> Result { - Ok("".to_string()) - } - - #[async_recursion] - pub(crate) async fn _read_entry_redis(&self, key_name: &str) -> Result, Error> { - Ok(vec![]) - } - - #[async_recursion] - pub(crate) async fn _update_entry_redis( - &self, - key_name: &str, - payload: &[u8], - ) -> Result { - Ok("".to_string()) - } - - #[async_recursion] - pub(crate) async fn _delete_entry_redis(&self, key_name: &str) -> Result { - Ok("".to_string()) - } -} From 3f49baf23373d53420567fe79dbcdda71f662507 Mon Sep 17 00:00:00 2001 From: nociza Date: Fri, 30 Dec 2022 20:14:54 -0800 Subject: [PATCH 04/15] evoke redis client commands --- src/extensions/storage_macro/db_redis.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/extensions/storage_macro/db_redis.rs b/src/extensions/storage_macro/db_redis.rs index 60b2077..2f8b699 100644 --- a/src/extensions/storage_macro/db_redis.rs +++ b/src/extensions/storage_macro/db_redis.rs @@ -53,8 +53,7 @@ impl crate::application::CoLink { ) -> Result { let mut con = self._get_con_from_stored_credentials(address).await?; let response = con.set(key_name, payload)?; - - Ok("".to_string()) + Ok(response) } #[async_recursion] @@ -63,7 +62,9 @@ impl crate::application::CoLink { address: &str, key_name: &str, ) -> Result, Error> { - Ok(vec![]) + let mut con = self._get_con_from_stored_credentials(address).await?; + let response: Vec = con.get(key_name)?; + Ok(response) } #[async_recursion] @@ -73,7 +74,9 @@ impl crate::application::CoLink { key_name: &str, payload: &[u8], ) -> Result { - Ok("".to_string()) + let mut con = self._get_con_from_stored_credentials(address).await?; + let response = con.set(key_name, payload)?; + Ok(response) } #[async_recursion] @@ -82,6 +85,8 @@ impl crate::application::CoLink { address: &str, key_name: &str, ) -> Result { - Ok("".to_string()) + let mut con = self._get_con_from_stored_credentials(address).await?; + let response = con.del(key_name)?; + Ok(response) } } From 3c34332b374cfffae8552ae89c1325de35dff4ab Mon Sep 17 00:00:00 2001 From: nociza Date: Sun, 1 Jan 2023 07:59:07 -0800 Subject: [PATCH 05/15] assume entire url stored as redis_url --- src/extensions/storage_macro/db_redis.rs | 34 ++++-------------------- 1 file changed, 5 insertions(+), 29 deletions(-) diff --git a/src/extensions/storage_macro/db_redis.rs b/src/extensions/storage_macro/db_redis.rs index 2f8b699..da36d33 100644 --- a/src/extensions/storage_macro/db_redis.rs +++ b/src/extensions/storage_macro/db_redis.rs @@ -12,36 +12,12 @@ impl crate::application::CoLink { async fn _get_con_from_stored_credentials( &self, - address: &str, + key_path: &str, ) -> Result { - // get credentials from storage - let redis_username = self - .read_entry(format!("{}:{}", address, "redis_username").as_str()) - .await?; - let redis_username_string = String::from_utf8(redis_username)?; - let redis_password = self - .read_entry(format!("{}:{}", address, "redis_password").as_str()) - .await?; - let redis_password_string = String::from_utf8(redis_password)?; - let redis_host_name = self - .read_entry(format!("{}:{}", address, "redis_host_name").as_str()) - .await?; - let redis_host_name_string = String::from_utf8(redis_host_name)?; - - // get the uri scheme from storage, if doesn't exist, default to non-ssl - let uri_scheme = self - .read_entry(format!("{}:{}", address, "redis_use_secure").as_str()) - .await; - let mut uri_scheme_string = "redis://".to_string(); - if uri_scheme.is_ok() { - uri_scheme_string = "rediss://".to_string(); - } - - let redis_address = format!( - "{}://{}:{}@{}", - uri_scheme_string, redis_username_string, redis_password_string, redis_host_name_string - ); - self._get_con_from_address(&redis_address) + let redis_url_key = format!("{}:redis_url", key_path); + let redis_url = self.read_entry(redis_url_key.as_str()).await?; + let redis_url_string = String::from_utf8(redis_url)?; + self._get_con_from_address(redis_url_string.as_str()) } #[async_recursion] From 98574d59aa89f1f1eb2e7a1a9b5f01c2c7571c16 Mon Sep 17 00:00:00 2001 From: stneng Date: Tue, 17 Jan 2023 10:24:38 +0800 Subject: [PATCH 06/15] fix --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 3b9db46..658600a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,4 +48,4 @@ variable_transfer = ["extensions", "remote_storage", "hyper", "jsonwebtoken", "r registry = [] policy_module = [] instant_server = ["reqwest"] -storage_macro = ["async-recursion"] +storage_macro = ["async-recursion", "redis"] From 0960af2ba51d49c702781983fea9d07a3ba805db Mon Sep 17 00:00:00 2001 From: stneng Date: Tue, 17 Jan 2023 11:15:54 +0800 Subject: [PATCH 07/15] add test --- tests/test_storage_macro.rs | 49 +++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 tests/test_storage_macro.rs diff --git a/tests/test_storage_macro.rs b/tests/test_storage_macro.rs new file mode 100644 index 0000000..38ecc33 --- /dev/null +++ b/tests/test_storage_macro.rs @@ -0,0 +1,49 @@ +use colink::{ + extensions::instant_server::{InstantRegistry, InstantServer}, + CoLink, +}; +use rand::Rng; + +#[tokio::test] +async fn test_storage_macro() -> Result<(), Box> { + let _ir = InstantRegistry::new(); + let is = InstantServer::new(); + let cl = is.get_colink().switch_to_generated_user().await?; + + // chunk + let key_name = "storage_macro_test_chunk:$chunk"; + test_crud(&cl, key_name).await?; + + // redis + cl.create_entry("storage_macro_test_redis:redis_url", b"redis://localhost") + .await?; + let key_name = "storage_macro_test_redis:$redis:redis_key"; + test_crud(&cl, key_name).await?; + + Ok(()) +} + +async fn test_crud( + cl: &CoLink, + key_name: &str, +) -> Result<(), Box> { + let payload = rand::thread_rng() + .sample_iter(&rand::distributions::Standard) + .take(5e6 as usize) + .collect::>(); + cl.create_entry(key_name, &payload.clone()).await?; + assert!(cl.create_entry(key_name, b"").await.is_err()); + let data = cl.read_entry(key_name).await?; + assert_eq!(data, payload); + let new_payload = rand::thread_rng() + .sample_iter(&rand::distributions::Standard) + .take(3e6 as usize) + .collect::>(); + cl.update_entry(key_name, &new_payload.clone()).await?; + let data = cl.read_entry(key_name).await?; + assert_eq!(data, new_payload); + cl.delete_entry(key_name).await?; + assert!(cl.read_entry(key_name).await.is_err()); + assert!(cl.delete_entry(key_name).await.is_err()); + Ok(()) +} From 565c917c4fde237222e556c228306b270314be5a Mon Sep 17 00:00:00 2001 From: stneng Date: Tue, 17 Jan 2023 11:16:05 +0800 Subject: [PATCH 08/15] fix chunk --- src/extensions/storage_macro/chunk.rs | 64 ++++++++++++++++----------- 1 file changed, 37 insertions(+), 27 deletions(-) diff --git a/src/extensions/storage_macro/chunk.rs b/src/extensions/storage_macro/chunk.rs index 0eedb1a..f3df354 100644 --- a/src/extensions/storage_macro/chunk.rs +++ b/src/extensions/storage_macro/chunk.rs @@ -50,34 +50,39 @@ impl crate::application::CoLink { ) -> Result { let metadata_key = format!("{}:chunk_metadata", key_name); // lock the metadata entry to prevent simultaneous writes - let lock_token = self.lock(&metadata_key.clone()).await?; - // create the chunks and store them - let chunk_paths = self._store_chunks(payload, key_name).await?; - // make sure that the chunk paths are smaller than the maximum entry size - let chunk_paths_string = self._check_chunk_paths_size(chunk_paths)?; - // store the chunk paths in the metadata entry and update metadata - let response = self - .create_entry(&metadata_key.clone(), &chunk_paths_string.into_bytes()) - .await?; + let lock_token = self.lock(&metadata_key).await?; + // use a closure to prevent locking forever caused by errors + let res = async { + // create the chunks and store them + let chunk_paths = self._store_chunks(payload, key_name).await?; + // make sure that the chunk paths are smaller than the maximum entry size + let chunk_paths_string = self._check_chunk_paths_size(chunk_paths)?; + // store the chunk paths in the metadata entry and update metadata + let response = self + .create_entry(&metadata_key, &chunk_paths_string.into_bytes()) + .await?; + Ok::(response) + } + .await; self.unlock(lock_token).await?; - Ok(response) + res } #[async_recursion] pub(crate) async fn _read_entry_chunk(&self, key_name: &str) -> Result, Error> { let metadata_key = format!("{}:chunk_metadata", key_name); - let metadata_response = self.read_entry(&metadata_key.clone()).await?; - let payload_string = String::from_utf8(metadata_response.clone())?; + let metadata_response = self.read_entry(&metadata_key).await?; + let payload_string = String::from_utf8(metadata_response)?; let user_id = decode_jwt_without_validation(&self.jwt).unwrap().user_id; // read the chunks into a single vector let chunks_paths = payload_string.split(';').collect::>(); let mut payload = Vec::new(); for (i, timestamp) in chunks_paths.iter().enumerate() { - let response = self + let mut response = self .read_entry(&format!("{}::{}:{}@{}", user_id, key_name, i, timestamp)) .await?; - payload.append(&mut response.clone()); + payload.append(&mut response); } Ok(payload) } @@ -90,25 +95,30 @@ impl crate::application::CoLink { ) -> Result { let metadata_key = format!("{}:chunk_metadata", key_name); // lock the metadata entry to prevent simultaneous writes - let lock_token = self.lock(&metadata_key.clone()).await?; - // split payload into chunks and update the chunks - let chunk_paths = self._store_chunks(payload, key_name).await?; - // make sure that the chunk paths are smaller than the maximum entry size - let chunk_paths_string = self._check_chunk_paths_size(chunk_paths)?; - // update the metadata entry - let response = self - .update_entry(&metadata_key.clone(), &chunk_paths_string.into_bytes()) - .await?; + let lock_token = self.lock(&metadata_key).await?; + // use a closure to prevent locking forever caused by errors + let res = async { + // split payload into chunks and update the chunks + let chunk_paths = self._store_chunks(payload, key_name).await?; + // make sure that the chunk paths are smaller than the maximum entry size + let chunk_paths_string = self._check_chunk_paths_size(chunk_paths)?; + // update the metadata entry + let response = self + .update_entry(&metadata_key, &chunk_paths_string.into_bytes()) + .await?; + Ok::(response) + } + .await; self.unlock(lock_token).await?; - Ok(response) + res } #[async_recursion] pub(crate) async fn _delete_entry_chunk(&self, key_name: &str) -> Result { let metadata_key = format!("{}:chunk_metadata", key_name); - let lock_token = self.lock(&metadata_key.clone()).await?; - let response = self.delete_entry(&metadata_key.clone()).await?; + let lock_token = self.lock(&metadata_key).await?; + let res = self.delete_entry(&metadata_key).await; self.unlock(lock_token).await?; - Ok(response) + res } } From ac95f59e294d157ffb1e72f730c8bb16846d7484 Mon Sep 17 00:00:00 2001 From: stneng Date: Tue, 17 Jan 2023 11:29:52 +0800 Subject: [PATCH 09/15] fix redis --- src/extensions/storage_macro/db_redis.rs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/extensions/storage_macro/db_redis.rs b/src/extensions/storage_macro/db_redis.rs index da36d33..a7c339a 100644 --- a/src/extensions/storage_macro/db_redis.rs +++ b/src/extensions/storage_macro/db_redis.rs @@ -28,8 +28,11 @@ impl crate::application::CoLink { payload: &[u8], ) -> Result { let mut con = self._get_con_from_stored_credentials(address).await?; - let response = con.set(key_name, payload)?; - Ok(response) + let response: i32 = con.set_nx(key_name, payload)?; + if response == 0 { + Err("key already exists.")? + } + Ok(response.to_string()) } #[async_recursion] @@ -39,8 +42,11 @@ impl crate::application::CoLink { key_name: &str, ) -> Result, Error> { let mut con = self._get_con_from_stored_credentials(address).await?; - let response: Vec = con.get(key_name)?; - Ok(response) + let response: Option> = con.get(key_name)?; + match response { + Some(response) => Ok(response), + None => Err("key does not exist.")?, + } } #[async_recursion] @@ -62,7 +68,10 @@ impl crate::application::CoLink { key_name: &str, ) -> Result { let mut con = self._get_con_from_stored_credentials(address).await?; - let response = con.del(key_name)?; - Ok(response) + let response: i32 = con.del(key_name)?; + if response == 0 { + Err("key does not exist.")? + } + Ok(response.to_string()) } } From 2015b15c8b711813bb82fa3f2dae0514428ef309 Mon Sep 17 00:00:00 2001 From: stneng Date: Tue, 17 Jan 2023 11:31:51 +0800 Subject: [PATCH 10/15] fix workflow --- .github/workflows/check.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 22bd3b0..bfe23dc 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -16,8 +16,12 @@ jobs: rabbitmq: image: rabbitmq:3.8-management ports: - - 5672:5672 + - 5672:5672 - 15672:15672 + redis: + image: redis + ports: + - 6379:6379 steps: - name: Checkout uses: actions/checkout@v3 From 8a4cb68133bd77799916feb79bb91fe7f8c0ab4a Mon Sep 17 00:00:00 2001 From: stneng Date: Tue, 17 Jan 2023 11:41:18 +0800 Subject: [PATCH 11/15] bump version to 0.2.9 --- Cargo.toml | 8 ++++---- README.md | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 658600a..599c418 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "colink" -version = "0.2.8" +version = "0.2.9" edition = "2021" description = "CoLink Rust SDK" license = "MIT" @@ -10,9 +10,9 @@ documentation = "https://docs.rs/colink" repository = "https://github.com/CoLearn-Dev/colink-sdk-rust-dev" [dependencies] -async-recursion = { version = "1.0.0", optional = true } +async-recursion = { version = "1.0", optional = true } async-trait = "0.1" -base64 = "0.13.0" +base64 = "0.13" chrono = "0.4" clap = { version = "4.0", features = ["derive", "env"] } futures-lite = "1.12" @@ -21,7 +21,7 @@ hyper-rustls = { version = "0.23", optional = true } jsonwebtoken = { version = "7.2", optional = true } lapin = "2.1" prost = "0.10" -rand = { version = "0.8.4", features = ["std_rng"] } +rand = { version = "0.8", features = ["std_rng"] } rcgen = { version = "0.10", optional = true } redis = { version = "0.22", optional = true } reqwest = { version = "0.11", default-features = false, features = ["rustls-tls-native-roots"], optional = true } diff --git a/README.md b/README.md index 34e7812..45532fd 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ CoLink SDK helps both application adnd protocol developers access the functional Add this to your Cargo.toml: ```toml [dependencies] -colink = "0.2.8" +colink = "0.2.9" ``` ## Getting Started From 58e3ac9b519863f09c75298974bd279d8a877efb Mon Sep 17 00:00:00 2001 From: stneng Date: Thu, 19 Jan 2023 11:23:47 +0800 Subject: [PATCH 12/15] fix --- .cargo/config.toml | 2 ++ src/extensions/lock.rs | 2 ++ tests/test_storage_macro.rs | 30 +++++++++++++++++++++++++++--- 3 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 .cargo/config.toml diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..8af59dd --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,2 @@ +[env] +RUST_TEST_THREADS = "1" diff --git a/src/extensions/lock.rs b/src/extensions/lock.rs index 89f5cc5..032aa50 100644 --- a/src/extensions/lock.rs +++ b/src/extensions/lock.rs @@ -5,6 +5,8 @@ type Error = Box; impl crate::application::CoLink { /// The default retry time cap is 100 ms. If you want to specify a retry time cap, use lock_with_retry_time instead. pub async fn lock(&self, key: &str) -> Result { + #[cfg(feature = "storage_macro")] + let key = &key.replace("$", "_lock_dollar"); self.lock_with_retry_time(key, 100).await } diff --git a/tests/test_storage_macro.rs b/tests/test_storage_macro.rs index 38ecc33..cb5a78c 100644 --- a/tests/test_storage_macro.rs +++ b/tests/test_storage_macro.rs @@ -5,16 +5,25 @@ use colink::{ use rand::Rng; #[tokio::test] -async fn test_storage_macro() -> Result<(), Box> { +async fn test_storage_macro_chunk() -> Result<(), Box> +{ let _ir = InstantRegistry::new(); let is = InstantServer::new(); let cl = is.get_colink().switch_to_generated_user().await?; - // chunk let key_name = "storage_macro_test_chunk:$chunk"; test_crud(&cl, key_name).await?; - // redis + Ok(()) +} + +#[tokio::test] +async fn test_storage_macro_redis() -> Result<(), Box> +{ + let _ir = InstantRegistry::new(); + let is = InstantServer::new(); + let cl = is.get_colink().switch_to_generated_user().await?; + cl.create_entry("storage_macro_test_redis:redis_url", b"redis://localhost") .await?; let key_name = "storage_macro_test_redis:$redis:redis_key"; @@ -23,6 +32,21 @@ async fn test_storage_macro() -> Result<(), Box Result<(), Box> { + let _ir = InstantRegistry::new(); + let is = InstantServer::new(); + let cl = is.get_colink().switch_to_generated_user().await?; + + cl.create_entry("test_storage_macro_chunk_redis:redis_url", b"redis://localhost") + .await?; + let key_name = "test_storage_macro_chunk_redis:$redis:redis_chunk:$chunk"; + test_crud(&cl, key_name).await?; + + Ok(()) +} + async fn test_crud( cl: &CoLink, key_name: &str, From 48c57114d640cba7aafcca1589dbd695fa3de6b3 Mon Sep 17 00:00:00 2001 From: stneng Date: Thu, 19 Jan 2023 11:28:18 +0800 Subject: [PATCH 13/15] ignore --- tests/test_storage_macro.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_storage_macro.rs b/tests/test_storage_macro.rs index cb5a78c..574f5ea 100644 --- a/tests/test_storage_macro.rs +++ b/tests/test_storage_macro.rs @@ -32,6 +32,7 @@ async fn test_storage_macro_redis() -> Result<(), Box Result<(), Box> { From 3c32476b48335f8e7a545704866435e5fe2f904a Mon Sep 17 00:00:00 2001 From: stneng Date: Thu, 19 Jan 2023 11:42:19 +0800 Subject: [PATCH 14/15] fmt, clippy --- src/extensions/lock.rs | 2 +- tests/test_storage_macro.rs | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/extensions/lock.rs b/src/extensions/lock.rs index 032aa50..e74b482 100644 --- a/src/extensions/lock.rs +++ b/src/extensions/lock.rs @@ -6,7 +6,7 @@ impl crate::application::CoLink { /// The default retry time cap is 100 ms. If you want to specify a retry time cap, use lock_with_retry_time instead. pub async fn lock(&self, key: &str) -> Result { #[cfg(feature = "storage_macro")] - let key = &key.replace("$", "_lock_dollar"); + let key = &key.replace('$', "_lock_dollar"); self.lock_with_retry_time(key, 100).await } diff --git a/tests/test_storage_macro.rs b/tests/test_storage_macro.rs index 574f5ea..4e81f2d 100644 --- a/tests/test_storage_macro.rs +++ b/tests/test_storage_macro.rs @@ -40,8 +40,11 @@ async fn test_storage_macro_chunk_redis( let is = InstantServer::new(); let cl = is.get_colink().switch_to_generated_user().await?; - cl.create_entry("test_storage_macro_chunk_redis:redis_url", b"redis://localhost") - .await?; + cl.create_entry( + "test_storage_macro_chunk_redis:redis_url", + b"redis://localhost", + ) + .await?; let key_name = "test_storage_macro_chunk_redis:$redis:redis_chunk:$chunk"; test_crud(&cl, key_name).await?; From 99ec02efd739f95fc0b69210052a90db6612d239 Mon Sep 17 00:00:00 2001 From: stneng Date: Thu, 19 Jan 2023 14:41:58 +0800 Subject: [PATCH 15/15] add underline --- src/extensions/lock.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/extensions/lock.rs b/src/extensions/lock.rs index e74b482..bce7b0a 100644 --- a/src/extensions/lock.rs +++ b/src/extensions/lock.rs @@ -6,7 +6,7 @@ impl crate::application::CoLink { /// The default retry time cap is 100 ms. If you want to specify a retry time cap, use lock_with_retry_time instead. pub async fn lock(&self, key: &str) -> Result { #[cfg(feature = "storage_macro")] - let key = &key.replace('$', "_lock_dollar"); + let key = &key.replace('$', "_lock_dollar_"); self.lock_with_retry_time(key, 100).await }