Lua token bucket with burst allowance

Lua
local function token_bucket(rate, burst, clock)
  clock = clock or os.time
  local tokens, last = burst, clock()
  local function take(n)
    n = n or 1
    assert(n <= burst, 'request exceeds burst size')
    local now = clock()
    tokens = math.min(burst, tokens + (now - last) * rate)
    last = now
    if tokens >= n then
      tokens = tokens - n
      return true
    end
    return false, (n - tokens) / rate -- seconds until enough tokens
  end
  return { take = take }
end

return token_bucket