Skip to content

Commit 122efe9

Browse files
committed
hide time precision changes under feature flag to preserve js compat
1 parent 57139d9 commit 122efe9

File tree

5 files changed

+40
-8
lines changed

5 files changed

+40
-8
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@ default = ["tokio-comp", "sync"]
3131
sync = ["tokio"]
3232
tokio-comp = ["redis/tokio-comp"]
3333
async-std-comp = ["redis/async-std-comp"]
34+
break-js-comp = []

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,17 @@ workers for new messages with SUBSCRIBE to prevent multiple simultaneous
9292
If you enable the `sync` feature, you can import a `RsmqSync` object with sync
9393
versions of the methods.
9494

95+
## Time Precision
96+
97+
By default this library keeps compatibility with the JS counterpart. If you require
98+
sub-second precision or are sending many messages very close together and require to
99+
keep track of them with more precision than one second, you can enable the feature
100+
`break-js-comp` like this on your `Cargo.toml`
101+
102+
```toml
103+
rsmq_async = { version = "11", features = [ "break-js-comp" ] }
104+
```
105+
95106
## Guarantees
96107

97108
If you want to implement "at least one delivery" guarantee, you need to receive

src/functions.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ lazy_static! {
2121

2222
const JS_COMPAT_MAX_TIME_MILLIS: u64 = 9_999_999_000;
2323

24+
#[cfg(feature = "break-js-comp")]
25+
const TIME_MULTIPLIER: u64 = 1000;
26+
#[cfg(not(feature = "break-js-comp"))]
27+
const TIME_MULTIPLIER: u64 = 1;
28+
2429
/// The main object of this library. Creates/Handles the redis connection and contains all the methods
2530
#[derive(Clone)]
2631
pub struct RsmqFunctions<T: ConnectionLike> {
@@ -133,7 +138,7 @@ impl<T: ConnectionLike> RsmqFunctions<T> {
133138
redis::cmd("SADD")
134139
.arg(format!("{}:QUEUES", self.ns))
135140
.arg(qname)
136-
.query_async(conn)
141+
.query_async::<_, ()>(conn)
137142
.await?;
138143

139144
Ok(())
@@ -212,7 +217,7 @@ impl<T: ConnectionLike> RsmqFunctions<T> {
212217
.arg(&key)
213218
.cmd("ZCOUNT")
214219
.arg(&key)
215-
.arg(time.0 * 1000)
220+
.arg(time.0 * TIME_MULTIPLIER)
216221
.arg("+inf")
217222
.query_async(conn)
218223
.await?;
@@ -382,7 +387,7 @@ impl<T: ConnectionLike> RsmqFunctions<T> {
382387
redis::cmd("PUBLISH")
383388
.arg(format!("{}:rt:{}", self.ns, qname))
384389
.arg(result[3])
385-
.query_async(conn)
390+
.query_async::<_, ()>(conn)
386391
.await?;
387392
}
388393

@@ -453,7 +458,7 @@ impl<T: ConnectionLike> RsmqFunctions<T> {
453458
.arg(maxsize);
454459
}
455460

456-
commands.query_async(conn).await?;
461+
commands.query_async::<_, ()>(conn).await?;
457462

458463
self.get_queue_attributes(conn, qname).await
459464
}
@@ -469,8 +474,11 @@ impl<T: ConnectionLike> RsmqFunctions<T> {
469474
.cmd("TIME")
470475
.query_async(conn)
471476
.await?;
472-
473-
let time_micros = (result.1).0 * 1000000 + (result.1).1;
477+
478+
#[cfg(feature = "break-js-comp")]
479+
let time = (result.1).0 * 1000000 + (result.1).1;
480+
#[cfg(not(feature = "break-js-comp"))]
481+
let time = (result.1).0 * 1000;
474482

475483
let (hmget_first, hmget_second, hmget_third) =
476484
match (result.0.first(), result.0.get(1), result.0.get(2)) {
@@ -479,7 +487,7 @@ impl<T: ConnectionLike> RsmqFunctions<T> {
479487
};
480488

481489
let quid = if uid {
482-
Some(radix_36(time_micros).to_string() + &RsmqFunctions::<T>::make_id(22)?)
490+
Some(radix_36(time).to_string() + &RsmqFunctions::<T>::make_id(22)?)
483491
} else {
484492
None
485493
};
@@ -494,7 +502,7 @@ impl<T: ConnectionLike> RsmqFunctions<T> {
494502
maxsize: hmget_third
495503
.parse()
496504
.map_err(|_| RsmqError::CannotParseMaxsize)?,
497-
ts: time_micros / 1000,
505+
ts: time / TIME_MULTIPLIER,
498506
uid: quid,
499507
})
500508
}

src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,17 @@
7171
//! the Redis SUBSCRIBE command to be notified of new messages and issue a `receiveMessage` then. However make sure not
7272
//! to listen with multiple workers for new messages with SUBSCRIBE to prevent multiple simultaneous `receiveMessage`
7373
//! calls.
74+
//!
75+
//! ## Time Precision
76+
//!
77+
//! By default this library keeps compatibility with the JS counterpart. If you require
78+
//! sub-second precision or are sending many messages very close together and require to
79+
//! keep track of them with more precision than one second, you can enable the feature
80+
//! `break-js-comp` like this on your `Cargo.toml`
81+
//!
82+
//! ```toml
83+
//! rsmq_async = { version = "11", features = [ "break-js-comp" ] }
84+
//! ```
7485
//!
7586
//! ## Guarantees
7687
//!

tests/test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ fn change_queue_size() {
448448
})
449449
}
450450

451+
#[cfg(feature = "break-js-comp")]
451452
#[test]
452453
fn sent_messages_must_keep_order() {
453454
let rt = tokio::runtime::Runtime::new().unwrap();

0 commit comments

Comments
 (0)