mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-12 21:02:14 +00:00
upgrade redis limitation crate
This commit is contained in:
1 parent
336b887d63
commit
e75a135633
4 files changed
+224
-550
No files matched your search
Generated
+183
-519
File diff suppressed because it is too large.
Load diff
+1
-1
@@ -250,7 +250,7 @@ log = "0.4.27"
|
||||
once_cell = "1.21.3"
|
||||
r2d2 = "0.8.10"
|
||||
rand = "0.9.1"
|
||||
redis = { version = "1.0.2", features = ["r2d2", "tokio-rustls-comp"] }
|
||||
redis = { version = "1.0.2", features = ["r2d2", "tokio-rustls-comp", "tokio-comp", "cluster-async", "aio"] }
|
||||
regex = "1.12.2"
|
||||
reqwest = { version = "0.12.23", default-features = false, features = [ "charset", "http2", "system-proxy", "json", "cookies", "rustls-tls" ] }
|
||||
reqwest-websocket = { version = "0.5.0" }
|
||||
|
||||
Vendored
+1
-1
@@ -23,7 +23,7 @@ normal = ["workspace-build-acceleration"]
|
||||
[dependencies]
|
||||
chrono = "0.4.9"
|
||||
futures = "0.1.29"
|
||||
redis = "0.13.0"
|
||||
redis.workspace = true
|
||||
time = "0.3.30"
|
||||
|
||||
# Workspace hack (faster compile times)
|
||||
|
||||
Vendored
+39
-29
@@ -194,10 +194,10 @@ impl Limiter {
|
||||
/// - A time computation failed
|
||||
///
|
||||
/// [`Status`]: struct.Status.html
|
||||
pub fn count<K: Into<String>>(&self, key: K) -> impl Future<Item = Status, Error = Error> {
|
||||
pub async fn count<K: Into<String>>(&self, key: K) -> Result<Status, Error> {
|
||||
let limit = self.limit;
|
||||
|
||||
self.track(key).and_then(move |(count, reset_epoch_utc)| {
|
||||
self.track(key).await.and_then(move |(count, reset_epoch_utc)| {
|
||||
let status = build_status(count, limit, reset_epoch_utc);
|
||||
|
||||
if count > limit {
|
||||
@@ -209,37 +209,47 @@ impl Limiter {
|
||||
}
|
||||
|
||||
/// Tracks the given key in a period and returns the count and TTL for the key in seconds.
|
||||
fn track<K: Into<String>>(&self, key: K) -> impl Future<Item = (usize, usize), Error = Error> {
|
||||
async fn track<K: Into<String>>(&self, key: K) -> Result<(usize, usize), Error> {
|
||||
let key = key.into();
|
||||
let exipres = self.period.as_secs();
|
||||
|
||||
self.client
|
||||
.get_async_connection()
|
||||
.from_err()
|
||||
.and_then(move |con| {
|
||||
// The seed of this approach is outlined Atul R in a blog post about rate limiting
|
||||
// using NodeJS and Redis. For more details, see
|
||||
// https://blog.atulr.com/rate-limiter/
|
||||
let mut pipe = redis::pipe();
|
||||
pipe.atomic()
|
||||
.cmd("SET")
|
||||
.arg(&key)
|
||||
.arg(0)
|
||||
.arg("EX")
|
||||
.arg(exipres)
|
||||
.arg("NX")
|
||||
.ignore()
|
||||
.cmd("INCR")
|
||||
.arg(&key)
|
||||
.cmd("TTL")
|
||||
.arg(&key);
|
||||
let mut conn = self.client
|
||||
.get_multiplexed_async_connection()
|
||||
.await
|
||||
.map_err(|err| Error::from(err))?;
|
||||
|
||||
// The seed of this approach is outlined Atul R in a blog post about rate limiting
|
||||
// using NodeJS and Redis. For more details, see
|
||||
// https://blog.atulr.com/rate-limiter/
|
||||
let mut pipe = redis::pipe();
|
||||
pipe.atomic()
|
||||
.cmd("SET")
|
||||
.arg(&key)
|
||||
.arg(0)
|
||||
.arg("EX")
|
||||
.arg(exipres)
|
||||
.arg("NX")
|
||||
.ignore()
|
||||
.cmd("INCR")
|
||||
.arg(&key)
|
||||
.cmd("TTL")
|
||||
.arg(&key);
|
||||
|
||||
pipe.query_async(con)
|
||||
.from_err()
|
||||
.and_then(|(_, (count, ttl)): (_, (usize, u64))| {
|
||||
Ok((count, epoch_utc_plus(Duration::from_secs(ttl))?))
|
||||
})
|
||||
})
|
||||
//let result = pipe.query_async(&mut conn)
|
||||
// .await
|
||||
// .map_err(|err| Error::from(err))
|
||||
// .and_then(|(_, (count, ttl)): (_, (usize, u64))| {
|
||||
// Ok((count, epoch_utc_plus(Duration::from_secs(ttl))?))
|
||||
// })?;
|
||||
|
||||
let result = pipe.query_async(&mut conn)
|
||||
.await
|
||||
.map_err(|err| Error::from(err))
|
||||
.and_then(|(ignore, count, ttl): ((), usize, u64)| {
|
||||
Ok((count, epoch_utc_plus(Duration::from_secs(ttl))?))
|
||||
})?;
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user