Skip to content

in which cases expression for addTokens can be negative (clocks skew) in lua script? #16

@kashif-ghafoor

Description

@kashif-ghafoor

Question

I am trying to understand the implementation of token bucket in lua script. I cam across following expression and it's comment from rollinglimit.lua

-- tokens that should have been added by now
-- note math.max in case this ends up negative (clock skew?)
-- now that we call 'TIME' this is less likely to happen
local addTokens = math.max(((nowMS - lastUpdateMS) / intervalMS) * limit, 0)

comment mentions the use of math.max in cases where expression ((nowMS - lastUpdateMS) / intervalMS) * limit can be negative.

I believe expression can only be negative when lastUpdateMs is in future but that is unlikely happen without external change of value.

I want to understand and make sure the robustness of implementation. Can you specify In which cases expression can end up negative?
I searched about clock skew but it seems to be a low level circuit concept than a programming concpet.

Currently I have one redis instance. May be I am unware of sitation where expression can end up in negative.

Thank you for your time and effort.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions