1
0
mirror of https://github.com/coalaura/up.git synced 2025-07-18 21:53:23 +00:00

global limit & log time

This commit is contained in:
Laura
2025-06-20 22:44:16 +02:00
parent 3056a936c9
commit 9c1cfd5e79
3 changed files with 35 additions and 6 deletions

View File

@ -7,8 +7,11 @@ import (
type RateLimiter struct {
sync.Map
total atomic.Uint32
}
const MinusOne uint32 = ^uint32(0)
func NewRateLimiter() *RateLimiter {
return &RateLimiter{}
}
@ -25,6 +28,12 @@ func (rl *RateLimiter) Get(key string) *atomic.Uint32 {
}
func (rl *RateLimiter) Inc(key string) (uint32, func(), func()) {
if rl.total.Add(1) > MaxGlobalParallel {
rl.total.Add(MinusOne)
return 0, nil, nil
}
val := rl.Get(key)
new := val.Add(1)
@ -39,14 +48,17 @@ func (rl *RateLimiter) Inc(key string) (uint32, func(), func()) {
return
}
val.Add(^uint32(0))
rl.total.Add(MinusOne)
val.Add(MinusOne)
}
return new, pass, fail
}
func (rl *RateLimiter) Dec(key string) uint32 {
rl.total.Add(MinusOne)
val := rl.Get(key)
return val.Add(^uint32(0))
return val.Add(MinusOne)
}