1
0

feature/rate-limit (#91)

This commit is contained in:
konrad
2019-07-21 21:27:30 +00:00
committed by Gitea
parent 2e599e792e
commit 4327a559e5
33 changed files with 1520 additions and 86 deletions

60
vendor/github.com/ulule/limiter/v3/limiter.go generated vendored Normal file
View File

@ -0,0 +1,60 @@
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)
}