Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prototype to support SkipList keys #186

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ type Context struct {

// Limiter is the limiter instance.
type Limiter struct {
Store Store
Rate Rate
Options Options
Store Store
Rate Rate
Options Options
SkipList SkipList
}

// New returns an instance of Limiter.
Expand All @@ -37,15 +38,22 @@ func New(store Store, rate Rate, options ...Option) *Limiter {
for _, o := range options {
o(&opt)
}
whiteList := SkipList{
Keys: opt.SkipList,
}
return &Limiter{
Store: store,
Rate: rate,
Options: opt,
Store: store,
Rate: rate,
Options: opt,
SkipList: whiteList,
}
}

// Get returns the limit for given identifier.
func (limiter *Limiter) Get(ctx context.Context, key string) (Context, error) {
if limiter.SkipList.HasKey(key) {
return Context{}, nil
}
return limiter.Store.Get(ctx, key, limiter.Rate)
}

Expand Down
13 changes: 13 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ type Options struct {
// proxy is not configured properly to forward a trustworthy client IP.
// Please read the section "Limiter behind a reverse proxy" in the README for further information.
ClientIPHeader string

// SkipList represents list of keys which will be skipped by limiter
SkipList []string
}

// WithIPv4Mask will configure the limiter to use given mask for IPv4 address.
Expand Down Expand Up @@ -59,3 +62,13 @@ func WithClientIPHeader(header string) Option {
o.ClientIPHeader = header
}
}

// WithSkipList will configure the limiter to use list of provided
// exception keys (which may be IP addresses or custome header values).
// This list will be consulted at every request and if key will be found in
// SkipList the limiter functionality will be skipped for that key.
func WithSkipList(wlist []string) Option {
return func(o *Options) {
o.SkipList = wlist
}
}
16 changes: 16 additions & 0 deletions whitelist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package limiter

// SkipList represent white list of keys
type SkipList struct {
Keys []string
}

// HasKey implements basic look-up of given key in SkipList
func (w *SkipList) HasKey(key string) bool {
for _, k := range w.Keys {
if k == key {
return true
}
}
return false
}