Skip to content

Instantly share code, notes, and snippets.

View florentchauveau's full-sized avatar

Florent CHAUVEAU florentchauveau

View GitHub Profile
@florentchauveau
florentchauveau / bucket_add.lua
Created August 28, 2018 11:56
Redis script (Lua) to add an event to a token bucket
-- Redis script to add an event to a token bucket
-- see https://medium.com/callr-techblog/rate-limiting-for-distributed-systems-with-redis-and-lua-eeea745cb260
-- (c) Florent CHAUVEAU <[email protected]>
local ts = tonumber(ARGV[1])
-- set the token bucket to 1 second (rolling)
local min = ts -1
-- iterate overs keys
-- Redis script to get the size of a token bucket
-- see https://medium.com/callr-techblog/rate-limiting-for-distributed-systems-with-redis-and-lua-eeea745cb260
-- (c) Florent CHAUVEAU <[email protected]>
local ts = tonumber(ARGV[1])
local key = KEYS[1]
-- remove tokens < min (older than now() -1s)
local min = ts -1
@florentchauveau
florentchauveau / leaky_bucket.lua
Created August 28, 2018 12:50
Redis script (Lua) to implement a leaky bucket
-- Redis script to implement a leaky bucket
-- see https://medium.com/callr-techblog/rate-limiting-for-distributed-systems-with-redis-and-lua-eeea745cb260
-- (c) Florent CHAUVEAU <[email protected]>
local ts = tonumber(ARGV[1])
local cps = tonumber(ARGV[2])
local key = KEYS[1]
-- remove tokens < min (older than now() -1s)
local min = ts -1
// ConvertTS converts a UNIX timestamp string (with sub second precision) "epoch" into a time.Time
// (c) Florent CHAUVEAU <[email protected]>
func ConvertTS(ts string) time.Time {
value := strings.SplitN(ts, ".", 2)
var sec int64
var nsec int64
if len(value) == 0 {
@florentchauveau
florentchauveau / .gitlab-ci.yml
Last active March 30, 2024 05:21
GitLab CI yaml file for building docker images
# This is a GitLab CI configuration to build the project as a docker image
# The file is generic enough to be dropped in a project containing a working Dockerfile
# Author: Florent CHAUVEAU <[email protected]>
# Mentioned here: https://blog.callr.tech/building-docker-images-with-gitlab-ci-best-practices/
# do not use "latest" here, if you want this to work in the future
image: docker:20
stages:
- build