generated from denpeshkov/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d314c43
commit a20abc2
Showing
8 changed files
with
733 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
Oops, something went wrong.