From a2a091c22922ee8b596b8d7bcc6a0f990485fab6 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Thu, 3 Jun 2021 17:30:38 +0530 Subject: [PATCH] hacks documented --- README.md | 19 +++++++++++++++++-- src/lib.rs | 4 ++++ src/pocket.rs | 2 +- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index dacf98b..f6bc394 100644 --- a/README.md +++ b/README.md @@ -68,14 +68,16 @@ decrement key `myCounter` at `t=y`(where y is an instant in future), all registered decrements. When its done, it cleans itself up. This way, we are not spinning timers for every decrement operation but -instead, one for every "time pocket". +instead, one for every "pocket". ### Gotchas: -This module creates and manages data of two types: +This module creates and manages data of three types: 1. `mcaptcha_cache:captcha:y` where `y`(last character) is variable 2. `mcaptcha_cache:pocket:x` where `x`(last character) is variable +3. `mcaptcha:timer:z` where `z`(last character) is pocket name from + step 2(See [Hacks](#hacks)). **WARNING: Please don't modify these manually. If you do so, then Redis will panic** @@ -174,3 +176,16 @@ GET: 900090.06 requests per second, p50=0.775 msec mCaptcha cache with piplining MCAPTCHA_CACHE.COUNT mycounter 45: 274876.31 requests per second, p50=2.767 msec ``` + +## Hacks + +I couldn't find any ways to persist timers to disk(`RDB`/`AOF`). So I'm +using a dummy record(`mcaptcha:timer:*` see [Gotchas](#gotchas)) which +will expire after an arbitrary time(see `POCKET_EXPIRY_OFFSET` in +[`lib.rs`](./src/lib.rs)). When that expiry occurs, I derive the key of +the pocket from the values that are passed to expiration event handlers +and perform clean up of both the pocket and counters registered with the +pocket. + +Ideally, I should be able to persist timers but I couldn't find ways to +do that. diff --git a/src/lib.rs b/src/lib.rs index 83216f6..bfec233 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -35,6 +35,10 @@ pub const PREFIX_POCKET: &str = "mcaptcha_cache:pocket:"; /// pocket timer key prefix pub const PREFIX_POCKET_TIMER: &str = "mcaptcha:timer:"; +/// If pockets perform clean up at x instant, then pockets themselves will get cleaned +/// up at x + POCKET_EXPIRY_OFFSET(if they haven't already been cleaned up) +pub const POCKET_EXPIRY_OFFSET: u64 = 30; + fn timer_create(ctx: &Context, args: Vec) -> RedisResult { let mut args = args.into_iter().skip(1); // mcaptcha captcha key name diff --git a/src/pocket.rs b/src/pocket.rs index 4aadf95..cae805b 100644 --- a/src/pocket.rs +++ b/src/pocket.rs @@ -139,7 +139,7 @@ impl Pocket { pocket.set_value(&MCAPTCHA_POCKET_TYPE, counter)?; let timer = ctx.open_key_writable(&get_timer_name_from_pocket_name(&pocket_name)); timer.write("1")?; - timer.set_expire(Duration::from_secs(duration + 30))?; + timer.set_expire(Duration::from_secs(duration + POCKET_EXPIRY_OFFSET))?; } };