forked from solderpunk/molly-brown
Continue to increment drips once bucket is overflowing.
This commit is contained in:
parent
a6170a355d
commit
3c5835f033
|
@ -53,8 +53,9 @@ func handleGeminiRequest(conn net.Conn, sysConfig SysConfig, config UserConfig,
|
||||||
if sysConfig.RateLimitEnable {
|
if sysConfig.RateLimitEnable {
|
||||||
noPort := logEntry.RemoteAddr.String()
|
noPort := logEntry.RemoteAddr.String()
|
||||||
noPort = noPort[0:strings.LastIndex(noPort, ":")]
|
noPort = noPort[0:strings.LastIndex(noPort, ":")]
|
||||||
if !rl.Allowed(noPort) {
|
drips, allowed := rl.Allowed(noPort)
|
||||||
conn.Write([]byte("44 10 second cool down, please!\r\n"))
|
if !allowed {
|
||||||
|
conn.Write([]byte("44 " + strconv.Itoa(drips) + " second cool down, please!\r\n"))
|
||||||
logEntry.Status = 44
|
logEntry.Status = 44
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
12
ratelim.go
12
ratelim.go
|
@ -35,18 +35,16 @@ func newRateLimiter(rate int, burst int) RateLimiter {
|
||||||
return *rl
|
return *rl
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rl *RateLimiter) Allowed(addr string) bool {
|
func (rl *RateLimiter) Allowed(addr string) (int, bool) {
|
||||||
rl.mu.Lock()
|
rl.mu.Lock()
|
||||||
defer rl.mu.Unlock()
|
defer rl.mu.Unlock()
|
||||||
drips, present := rl.bucket[addr]
|
drips, present := rl.bucket[addr]
|
||||||
if !present {
|
if !present {
|
||||||
rl.bucket[addr] = 1
|
rl.bucket[addr] = 1
|
||||||
return true
|
return 1, true
|
||||||
}
|
}
|
||||||
if drips == rl.burst {
|
drips += 1
|
||||||
return false
|
rl.bucket[addr] = drips
|
||||||
}
|
return drips, drips < rl.burst
|
||||||
rl.bucket[addr] = drips + 1
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue