-
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add connections & logins rate limiting per IP block
- Loading branch information
1 parent
9e453d6
commit a870c82
Showing
7 changed files
with
181 additions
and
33 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,54 @@ | ||
package quotautil | ||
|
||
import ( | ||
"github.com/golang/groupcache/lru" | ||
"golang.org/x/time/rate" | ||
"net" | ||
"sync" | ||
) | ||
|
||
// Quota implements a simple IP-based rate limiter. | ||
// Each set of incoming IP addresses with the same | ||
// low-order byte gets events per second. | ||
// Information is kept in an LRU cache of size maxEntries. | ||
type Quota struct { | ||
eps float32 // allowed events per second | ||
burst int // maximum event per second (queue) | ||
mu sync.Mutex // protects cache | ||
cache *lru.Cache | ||
} | ||
|
||
func (q *Quota) Blocked(addr net.Addr) bool { | ||
var limiter *rate.Limiter | ||
key := ipKey(addr) | ||
if key != "" { | ||
q.mu.Lock() | ||
if v, ok := q.cache.Get(key); ok { | ||
limiter = v.(*rate.Limiter) | ||
} else { | ||
limiter = rate.NewLimiter(rate.Limit(q.eps), q.burst) | ||
q.cache.Add(key, limiter) | ||
} | ||
q.mu.Unlock() | ||
} | ||
return limiter != nil && !limiter.Allow() | ||
} | ||
|
||
func NewQuota(eventsPerSecond float32, burst, maxEntries int) *Quota { | ||
return &Quota{ | ||
eps: eventsPerSecond, | ||
burst: burst, | ||
cache: lru.New(maxEntries), | ||
} | ||
} | ||
|
||
func ipKey(addr net.Addr) string { | ||
host, _, _ := net.SplitHostPort(addr.String()) | ||
ip := net.ParseIP(host) | ||
if ip == nil { | ||
return "" | ||
} | ||
// Zero out last byte, to cover ranges. | ||
ip[len(ip)-1] = 0 | ||
return ip.String() | ||
} |
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