Add `:cost` of acquire
From: https://github.com/sensortower/sidekiq-throttled/issues/45
API
throttle.acquire):cost => 42) # default cost: 1
throttle.call(:cost => 42) { ... } # default cost: 1
Under the hood
We MUST either validate or normalize token name, so that we could scan by pattern later:
- in ruby:
raise ArgumentError if token.include? "!")
- and lua:
if token:match("!") then return redis.error_reply("invalid token") end
Acquire
To check if acquire is allowed we should check that current usage is <= limit + score
, after that:
- rate limit
-- pseudo-code for i = 1, cost, 1 do LPUSH(key, sunset) end
- concurrency
-- pseudo-code for i = 1, cost, 1 do ZADD(key, token .. "!" .. i, score) end
Release
Release happens for concurrency only
-- pseudo-code
for val in ZSCAN(key, token .. "!" .. "*") do
ZREM(key, val)
end
Edited by Alexey Zapparov