summaryrefslogtreecommitdiff
path: root/vendor/github.com/ulule/limiter/v3/limiter.go
diff options
context:
space:
mode:
authorLibravatar nya1 <nya1git@imap.cc>2022-08-31 12:06:14 +0200
committerLibravatar GitHub <noreply@github.com>2022-08-31 12:06:14 +0200
commitbee8458a2d12bdd42079fcb2c4ca88ebeafe305b (patch)
treec114acf28a460c1ebaa85965c89f2e7fb540eecc /vendor/github.com/ulule/limiter/v3/limiter.go
parent[feature] Sort follow requests, followers, and following by updated_at (#774) (diff)
downloadgotosocial-bee8458a2d12bdd42079fcb2c4ca88ebeafe305b.tar.xz
[feature] add rate limit middleware (#741)
* feat: add rate limit middleware * chore: update vendor dir * chore: update readme with new dependency * chore: add rate limit infos to swagger.md file * refactor: add ipv6 mask limiter option Add IPv6 CIDR /64 mask * refactor: increase rate limit to 1000 Address https://github.com/superseriousbusiness/gotosocial/pull/741#discussion_r945584800 Co-authored-by: tobi <31960611+tsmethurst@users.noreply.github.com>
Diffstat (limited to 'vendor/github.com/ulule/limiter/v3/limiter.go')
-rw-r--r--vendor/github.com/ulule/limiter/v3/limiter.go65
1 files changed, 65 insertions, 0 deletions
diff --git a/vendor/github.com/ulule/limiter/v3/limiter.go b/vendor/github.com/ulule/limiter/v3/limiter.go
new file mode 100644
index 000000000..1da6fceea
--- /dev/null
+++ b/vendor/github.com/ulule/limiter/v3/limiter.go
@@ -0,0 +1,65 @@
+package limiter
+
+import (
+ "context"
+)
+
+// -----------------------------------------------------------------
+// Context
+// -----------------------------------------------------------------
+
+// Context is the limit context.
+type Context struct {
+ Limit int64
+ Remaining int64
+ Reset int64
+ Reached bool
+}
+
+// -----------------------------------------------------------------
+// Limiter
+// -----------------------------------------------------------------
+
+// Limiter is the limiter instance.
+type Limiter struct {
+ Store Store
+ Rate Rate
+ Options Options
+}
+
+// New returns an instance of Limiter.
+func New(store Store, rate Rate, options ...Option) *Limiter {
+ opt := Options{
+ IPv4Mask: DefaultIPv4Mask,
+ IPv6Mask: DefaultIPv6Mask,
+ TrustForwardHeader: false,
+ }
+ for _, o := range options {
+ o(&opt)
+ }
+ return &Limiter{
+ Store: store,
+ Rate: rate,
+ Options: opt,
+ }
+}
+
+// Get returns the limit for given identifier.
+func (limiter *Limiter) Get(ctx context.Context, key string) (Context, error) {
+ return limiter.Store.Get(ctx, key, limiter.Rate)
+}
+
+// Peek returns the limit for given identifier, without modification on current values.
+func (limiter *Limiter) Peek(ctx context.Context, key string) (Context, error) {
+ return limiter.Store.Peek(ctx, key, limiter.Rate)
+}
+
+// Reset sets the limit for given identifier to zero.
+func (limiter *Limiter) Reset(ctx context.Context, key string) (Context, error) {
+ return limiter.Store.Reset(ctx, key, limiter.Rate)
+}
+
+// Increment increments the limit by given count & gives back the new limit for given identifier
+func (limiter *Limiter) Increment(ctx context.Context, key string, count int64) (Context, error) {
+ return limiter.Store.Increment(ctx, key, count, limiter.Rate)
+}