A Mana-based RateLimiter

What is a Rate Limiter?

Trần Thiện Khiêm
2 min readMar 12, 2022

Rate Limiting is used to control the rate of action done in a system. It can be used to prevent DoS attack or web scraping.

What is Mana?

Magic or mana is an attribute assigned to characters within a role-playing or video game that indicates their power to use special magical abilities or “spells”. (wiki)

How They are related?

Look at this following example. Phantom Assassin has 255 mana, Stifling Dagger costs 30 mana, ignoring cool down, she can make 8.5 Stifling Dagger spells.The regeneration at level 1 is 0.7 mana per second. So she can make maximum 8 skills for every 6 minutes (at level 1). That’s it. That’s Rate Limiting.

Blue Bar is Mana, Green Bar is Health.

Here is the overall algorithms of this Mana-based RateLimiter

The state machine of the RateLimiter
  • At first, The rate limiter has full mana.
  • Each Action consumes X mana.
  • When Mana is not full — then it starts to regenerate with a rate of Y mana per duration.
  • When the client continues to take action, then Mana will be empty or not enough mana, then client can’t take action…

--

--

Trần Thiện Khiêm

Software Engineer at Facebook — a coder, a dreamer and a Dota 2 Herald.