Giter Site home page Giter Site logo

Incorrect retryIn? about redis-gcra HOT 3 CLOSED

pselden avatar pselden commented on August 25, 2024
Incorrect retryIn?

from redis-gcra.

Comments (3)

pselden avatar pselden commented on August 25, 2024

I believe it's probably due to the rounding mechanism, which makes it so it actually refills a token when 50% of the retryIn is remaining.

from redis-gcra.

pselden avatar pselden commented on August 25, 2024

Here's a version that seems to be slightly more accurate:

local rate_limit_key = KEYS[1]
local now = ARGV[1]
local burst = ARGV[2]
local rate = ARGV[3]
local period = ARGV[4]
local cost = ARGV[5]

local emission_interval = period / rate
local increment = emission_interval * cost
local burst_offset = emission_interval * burst

local tat = redis.call("GET", rate_limit_key)

if not tat then
    tat = now
else
    tat = tonumber(tat)
end

tat = math.max(tat, now)

local new_tat = tat + increment
local allow_at = new_tat - burst_offset
local diff = now - allow_at

local retry_in
local reset_in
local remaining
local limited


if diff < 0 then
    limited = 1
    if increment <= burst_offset then
        retry_in = diff * -1
    else
        retry_in = -1
    end
    reset_in = tat - now
    remaining = 0
else
    limited = 0
    retry_in = 0
    reset_in = new_tat - now
    remaining = math.floor(diff / emission_interval + 0.5) -- poor man's round
    if increment > 0 then
        redis.call("SET", rate_limit_key, new_tat, "EX", reset_in)
    end
end

return {limited, remaining, retry_in, reset_in}

It's based off of https://github.com/throttled/throttled/blob/master/rate.go

Remaining might still need a little work

from redis-gcra.

spacetc62 avatar spacetc62 commented on August 25, 2024

Sorry for the delay, never even noticed the issue filed on here!

I actually ran into this same thing myself, and decided to just get rid of the rounding completely (to be honest, I'm not sure why we were rounding in the first place).

#4

from redis-gcra.

Related Issues (2)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.