1
1
Fork 0
mirror of https://tildegit.org/solderpunk/molly-brown synced 2024-04-28 06:45:03 +02:00

Continue to increment drips once bucket is overflowing.

This commit is contained in:
Solderpunk 2023-03-18 15:45:35 +01:00
parent a6170a355d
commit 3c5835f033
2 changed files with 8 additions and 9 deletions

View File

@ -53,8 +53,9 @@ func handleGeminiRequest(conn net.Conn, sysConfig SysConfig, config UserConfig,
if sysConfig.RateLimitEnable {
noPort := logEntry.RemoteAddr.String()
noPort = noPort[0:strings.LastIndex(noPort, ":")]
if !rl.Allowed(noPort) {
conn.Write([]byte("44 10 second cool down, please!\r\n"))
drips, allowed := rl.Allowed(noPort)
if !allowed {
conn.Write([]byte("44 " + strconv.Itoa(drips) + " second cool down, please!\r\n"))
logEntry.Status = 44
return
}

View File

@ -35,18 +35,16 @@ func newRateLimiter(rate int, burst int) RateLimiter {
return *rl
}
func (rl *RateLimiter) Allowed(addr string) bool {
func (rl *RateLimiter) Allowed(addr string) (int, bool) {
rl.mu.Lock()
defer rl.mu.Unlock()
drips, present := rl.bucket[addr]
if !present {
rl.bucket[addr] = 1
return true
return 1, true
}
if drips == rl.burst {
return false
}
rl.bucket[addr] = drips + 1
return true
drips += 1
rl.bucket[addr] = drips
return drips, drips < rl.burst
}