tokenlimit

    This section will introduce its basic usage through token limit (tokenlimit).

    On the whole, the token bucket production logic is as follows:

    • The average sending rate configured by the user is r, then a token is added to the bucket every 1/r second;
    • Assume that at most b tokens can be stored in the bucket. If the token bucket is full when the token arrives, then the token will be discarded;
    • When the traffic enters at the rate v, the token is taken from the bucket at the rate v, the traffic that gets the token passes, and the traffic that does not get the token does not pass, and the fuse logic is executed;

    Let’s take a look at several key attributes controlled by lua script:

    1. -- Return whether the expected token can be obtained alive
    2. local rate = tonumber(ARGV[1])
    3. local capacity = tonumber(ARGV[2])
    4. local now = tonumber(ARGV[3])
    5. local requested = tonumber(ARGV[4])
    6. -- fill_timeHow long does it take to fill the token_bucket
    7. local fill_time = capacity/rate
    8. local ttl = math.floor(fill_time*2)
    9. -- Get the number of remaining tokens in the current token_bucket
    10. local last_tokens = tonumber(redis.call("get", KEYS[1]))
    11. if last_tokens == nil then
    12. last_tokens = capacity
    13. end
    14. -- The time when the token_bucket was last updated
    15. local last_refreshed = tonumber(redis.call("get", KEYS[2]))
    16. if last_refreshed == nil then
    17. last_refreshed = 0
    18. end
    19. local delta = math.max(0, now-last_refreshed)
    20. -- Calculate the number of new tokens based on the span between the current time and the last update time, and the rate of token production
    21. local filled_tokens = math.min(capacity, last_tokens+(delta*rate))
    22. local allowed = filled_tokens >= requested
    23. if allowed then
    24. new_tokens = filled_tokens - requested
    25. end
    26. -- Update the new token number and update time
    27. redis.call("setex", KEYS[1], ttl, new_tokens)
    28. redis.call("setex", KEYS[2], ttl, now)
    29. return allowed

    It can be seen from the above that the lua script: only involves the operation of the token, ensuring that the token is produced and read reasonably.

    Seen from the above flow:

    1. There are multiple guarantee mechanisms to ensure that the current limit will be completed.
    2. If the redis limiter fails, at least in the process rate limiter will cover it.
    3. Retry the redis limiter mechanism to ensure that it runs as normally as possible.

    The tokenlimit current limiting scheme in go-zero is suitable for instantaneous traffic shocks, and the actual request scenario is not at a constant rate. The token bucket is quite pre-request, and when the real request arrives, it won’t be destroyed instantly. When the traffic hits a certain level, consumption will be carried out at a predetermined rate.