Redis module that implements mCaptcha cache and counter
Find a file
realaravinth 158f518449 packet
2021-06-02 15:54:22 +05:30
src packet 2021-06-02 15:54:22 +05:30
.gitignore readme 2021-06-01 18:15:24 +05:30
Cargo.lock readme 2021-06-01 18:15:24 +05:30
Cargo.toml readme 2021-06-01 18:15:24 +05:30
LICENSE.md license 2021-06-01 18:17:05 +05:30
README.md packet 2021-06-02 15:54:22 +05:30

mCaptcha Cache

Redis module that implements leaky bucket algorithm

dependency status AGPL License Chat

Motivation

mCaptcha uses a leaky- bucket-enabled counter to keep track of traffic/challenge requests.

  • At t=0(where t is time), if someone is visiting an mCaptcha-protected website, the counter for that website will be initialized and set to 1.

  • It should also automatically decrement(by 1) after a certain period, say t=cooldown. We call this cool down period and is constant for a website.

  • If at t=x(where x<cooldown), another user visits the same website, the counter becomes 2 and will auto decrement at t = cooldown + x for second user.

    Note that, for the decrement to work, we require two different timers that goes off at two different instants. The current(v0.1.3) of libmcaptcha implements this with internal data structures and timers --- something that can't be shared across several machines in a distributed setting.

    So we figured we'd use Redis to solve this problem and get synchronisation and persistence for free.

    This Redis module implements auto decrement on a special data type(which is also defined in this module).

How does it work?

If a timer is supposed to go off to decrement key myCounter at t=y(where y is an instant in future),

  1. A hashmap called mcaptcha_cache:decrement:y(prefix might vary) is created with key-value pairs keyName: DecrementCount(myCounter: 1 in our case)

  2. A timer will be created to go off at t=y

  3. Any further decrement operations that are scheduled for t=y are registered with the same hashmap(mcaptcha_cache:decrement:y).

  4. At t=y, a procedure will be executed to read all values of the hashmap(mcaptcha_cache:decrement:y) and performs 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".