Skip to content

Commit

Permalink
add sliding window
Browse files Browse the repository at this point in the history
  • Loading branch information
denpeshkov committed Sep 6, 2024
1 parent d314c43 commit a20abc2
Show file tree
Hide file tree
Showing 8 changed files with 733 additions and 13 deletions.
14 changes: 8 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,26 @@ jobs:
build:
name: Build, lint and test
runs-on: ubuntu-latest
strategy:
matrix:
go: ["1.21", "1.22", "1.23"]
steps:
- name: Checkout Code
uses: actions/checkout@v4

- name: Setup Go Environment
uses: actions/setup-go@v4.1.0
- name: Setup Go Environment (go${{ matrix.go }})
uses: actions/setup-go@v5
with:
go-version: 1.21
go-version: ${{ matrix.go }}
cache: false # managed by golangci-lint

- name: Download Dependencies
run: go mod download -x

- name: Lint
uses: golangci/golangci-lint-action@v3
uses: golangci/golangci-lint-action@v6
with:
version: latest

- name: Test
run: go test -v -race ./...

2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# syntax=docker/dockerfile:1
ARG GO_VERSION=latest
ARG GO_VERSION=1.23
ARG GOLANGCI_LINT_VERSION=latest-alpine

FROM --platform=$BUILDPLATFORM golang:${GO_VERSION} AS deps
Expand Down
9 changes: 5 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,25 @@ all: help tidy lint test test/cover

.PHONY: help
help: ## Display this help screen
awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

.PHONY: tidy
tidy: ## Tidy
go mod tidy -v
go mod verify
go fmt ./...
go vet ./..
go vet ./...
staticcheck ./...

.PHONY: lint
lint: ## Lint
golangci-lint run

.PHONY: test
test: ## Test
go test -v -race -buildvcs ./...
go test -v -race -buildvcs -count=1 ./...m

.PHONY: test/cover
test/cover: ## Test and cover
go test -v -race -buildvcs -coverprofile=/tmp/coverage.out ./...
go test -v -race -buildvcs -count=1 -coverprofile=/tmp/coverage.out ./...
go tool cover -html=/tmp/coverage.out
29 changes: 29 additions & 0 deletions allow.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
-- key is a key to associate with this rate limiter
-- limit_events is the maximum number of events to be allowed in a limiting interval. It must be > 0
-- limit_interval is the duration of the limiting interval in milliseconds
-- now is the current Unix time in milliseconds
-- key_ttl is an optional timeout in milliseconds for the key. It must be >= than limit_interval
-- returns limited, remaining, delay
local key = KEYS[1]
local limit_events = tonumber(ARGV[1])
local limit_interval = tonumber(ARGV[2])
local now = tonumber(ARGV[3])
local key_ttl = tonumber(ARGV[4])

local trim_time = now - limit_interval
redis.call('ZREMRANGEBYSCORE', key, "-inf", trim_time)

local limited = 1
local count = redis.call('ZCARD', key)
if count < limit_events then
redis.call('ZADD', key, now, now)
redis.call('PEXPIRE', key, key_ttl)

limited = 0
end

local min = tonumber(redis.call("ZRANGE", key, -limit_events, -limit_events)[1])
if not min then
return { limited, limit_events - count - 1, 0 }
end
return { limited, 0, min - trim_time }
62 changes: 60 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,61 @@
module github.com/denpeshkov/go-template
module github.com/denpeshkov/throttle

go 1.22
go 1.21

require (
github.com/redis/go-redis/v9 v9.6.1
github.com/testcontainers/testcontainers-go/modules/redis v0.33.0
)

require (
dario.cat/mergo v1.0.1 // indirect
github.com/AdaLogics/go-fuzz-headers v0.0.0-20240806141605-e8a1dd7889d6 // indirect
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/containerd/platforms v0.2.1 // indirect
github.com/cpuguy83/dockercfg v0.3.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/distribution/reference v0.6.0 // indirect
github.com/docker/docker v27.2.0+incompatible // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/lufia/plan9stats v0.0.0-20240819163618-b1d8f4d146e7 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
github.com/moby/patternmatcher v0.6.0 // indirect
github.com/moby/sys/sequential v0.6.0 // indirect
github.com/moby/sys/user v0.3.0 // indirect
github.com/moby/sys/userns v0.1.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
github.com/shirou/gopsutil/v3 v3.24.5 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/testcontainers/testcontainers-go v0.33.0 // indirect
github.com/tklauser/go-sysconf v0.3.14 // indirect
github.com/tklauser/numcpus v0.8.0 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.54.0 // indirect
go.opentelemetry.io/otel v1.29.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.29.0 // indirect
go.opentelemetry.io/otel/metric v1.29.0 // indirect
go.opentelemetry.io/otel/sdk v1.29.0 // indirect
go.opentelemetry.io/otel/trace v1.29.0 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/time v0.6.0 // indirect
)
Loading

0 comments on commit a20abc2

Please sign in to comment.