feat: initial commit
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
package ratelimiter
|
||||
|
||||
import (
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type FixedWindowLimiter struct {
|
||||
sync.RWMutex
|
||||
clients map[string]int
|
||||
limit int
|
||||
window time.Duration
|
||||
}
|
||||
|
||||
func NewFixedWindowLimiter(limit int, window time.Duration) *FixedWindowLimiter {
|
||||
return &FixedWindowLimiter{
|
||||
clients: make(map[string]int),
|
||||
limit: limit,
|
||||
window: window,
|
||||
}
|
||||
}
|
||||
|
||||
func (rl *FixedWindowLimiter) Allow(ip string) (bool, time.Duration) {
|
||||
rl.RLock()
|
||||
count, exists := rl.clients[ip]
|
||||
rl.RUnlock()
|
||||
|
||||
log.Println(count, exists)
|
||||
|
||||
if !exists || count < rl.limit {
|
||||
rl.Lock()
|
||||
if !exists {
|
||||
go rl.resetCount(ip)
|
||||
}
|
||||
|
||||
rl.clients[ip]++
|
||||
rl.Unlock()
|
||||
return true, 0
|
||||
}
|
||||
|
||||
return false, rl.window
|
||||
}
|
||||
|
||||
func (rl *FixedWindowLimiter) resetCount(ip string) {
|
||||
time.Sleep(rl.window)
|
||||
|
||||
rl.Lock()
|
||||
delete(rl.clients, ip)
|
||||
rl.Unlock()
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package ratelimiter
|
||||
|
||||
import "time"
|
||||
|
||||
//! Use Redis for rate limiting when in multiple instances
|
||||
|
||||
type Limiter interface {
|
||||
Allow(ip string) (bool, time.Duration)
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
RequestsPerTimeFrame int
|
||||
TimeFrame time.Duration
|
||||
Enabled bool
|
||||
}
|
||||
Reference in New Issue
Block a user