From 122efe98059d63b241d20630dc990ab637d6a9a3 Mon Sep 17 00:00:00 2001 From: David Bonet Date: Fri, 12 Jul 2024 22:04:57 +0200 Subject: [PATCH] hide time precision changes under feature flag to preserve js compat --- Cargo.toml | 1 + README.md | 11 +++++++++++ src/functions.rs | 24 ++++++++++++++++-------- src/lib.rs | 11 +++++++++++ tests/test.rs | 1 + 5 files changed, 40 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d92ffb5..cfd4045 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,3 +31,4 @@ default = ["tokio-comp", "sync"] sync = ["tokio"] tokio-comp = ["redis/tokio-comp"] async-std-comp = ["redis/async-std-comp"] +break-js-comp = [] diff --git a/README.md b/README.md index 1864efe..6d54425 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,17 @@ workers for new messages with SUBSCRIBE to prevent multiple simultaneous If you enable the `sync` feature, you can import a `RsmqSync` object with sync versions of the methods. +## Time Precision + +By default this library keeps compatibility with the JS counterpart. If you require +sub-second precision or are sending many messages very close together and require to +keep track of them with more precision than one second, you can enable the feature +`break-js-comp` like this on your `Cargo.toml` + +```toml +rsmq_async = { version = "11", features = [ "break-js-comp" ] } +``` + ## Guarantees If you want to implement "at least one delivery" guarantee, you need to receive diff --git a/src/functions.rs b/src/functions.rs index 2607752..36d4c76 100644 --- a/src/functions.rs +++ b/src/functions.rs @@ -21,6 +21,11 @@ lazy_static! { const JS_COMPAT_MAX_TIME_MILLIS: u64 = 9_999_999_000; +#[cfg(feature = "break-js-comp")] +const TIME_MULTIPLIER: u64 = 1000; +#[cfg(not(feature = "break-js-comp"))] +const TIME_MULTIPLIER: u64 = 1; + /// The main object of this library. Creates/Handles the redis connection and contains all the methods #[derive(Clone)] pub struct RsmqFunctions { @@ -133,7 +138,7 @@ impl RsmqFunctions { redis::cmd("SADD") .arg(format!("{}:QUEUES", self.ns)) .arg(qname) - .query_async(conn) + .query_async::<_, ()>(conn) .await?; Ok(()) @@ -212,7 +217,7 @@ impl RsmqFunctions { .arg(&key) .cmd("ZCOUNT") .arg(&key) - .arg(time.0 * 1000) + .arg(time.0 * TIME_MULTIPLIER) .arg("+inf") .query_async(conn) .await?; @@ -382,7 +387,7 @@ impl RsmqFunctions { redis::cmd("PUBLISH") .arg(format!("{}:rt:{}", self.ns, qname)) .arg(result[3]) - .query_async(conn) + .query_async::<_, ()>(conn) .await?; } @@ -453,7 +458,7 @@ impl RsmqFunctions { .arg(maxsize); } - commands.query_async(conn).await?; + commands.query_async::<_, ()>(conn).await?; self.get_queue_attributes(conn, qname).await } @@ -469,8 +474,11 @@ impl RsmqFunctions { .cmd("TIME") .query_async(conn) .await?; - - let time_micros = (result.1).0 * 1000000 + (result.1).1; + + #[cfg(feature = "break-js-comp")] + let time = (result.1).0 * 1000000 + (result.1).1; + #[cfg(not(feature = "break-js-comp"))] + let time = (result.1).0 * 1000; let (hmget_first, hmget_second, hmget_third) = match (result.0.first(), result.0.get(1), result.0.get(2)) { @@ -479,7 +487,7 @@ impl RsmqFunctions { }; let quid = if uid { - Some(radix_36(time_micros).to_string() + &RsmqFunctions::::make_id(22)?) + Some(radix_36(time).to_string() + &RsmqFunctions::::make_id(22)?) } else { None }; @@ -494,7 +502,7 @@ impl RsmqFunctions { maxsize: hmget_third .parse() .map_err(|_| RsmqError::CannotParseMaxsize)?, - ts: time_micros / 1000, + ts: time / TIME_MULTIPLIER, uid: quid, }) } diff --git a/src/lib.rs b/src/lib.rs index 480bd8b..7fd750a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -71,6 +71,17 @@ //! the Redis SUBSCRIBE command to be notified of new messages and issue a `receiveMessage` then. However make sure not //! to listen with multiple workers for new messages with SUBSCRIBE to prevent multiple simultaneous `receiveMessage` //! calls. +//! +//! ## Time Precision +//! +//! By default this library keeps compatibility with the JS counterpart. If you require +//! sub-second precision or are sending many messages very close together and require to +//! keep track of them with more precision than one second, you can enable the feature +//! `break-js-comp` like this on your `Cargo.toml` +//! +//! ```toml +//! rsmq_async = { version = "11", features = [ "break-js-comp" ] } +//! ``` //! //! ## Guarantees //! diff --git a/tests/test.rs b/tests/test.rs index d1892d1..a26fb38 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -448,6 +448,7 @@ fn change_queue_size() { }) } +#[cfg(feature = "break-js-comp")] #[test] fn sent_messages_must_keep_order() { let rt = tokio::runtime::Runtime::new().unwrap();