f-86: hostsvc: relais DNS et NTP par hôte, socket de contrôle #86
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
da6295baf1
commit
aa1cc58ae3
20 changed files with 2235 additions and 0 deletions
34
internal/hostsvc/limiter.go
Normal file
34
internal/hostsvc/limiter.go
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
package hostsvc
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type limiter struct {
|
||||
mu sync.Mutex
|
||||
rate float64
|
||||
tokens float64
|
||||
last time.Time
|
||||
now func() time.Time
|
||||
}
|
||||
|
||||
func newLimiter(rate int, now func() time.Time) *limiter {
|
||||
return &limiter{rate: float64(rate), tokens: float64(rate), last: now(), now: now}
|
||||
}
|
||||
|
||||
func (l *limiter) allow() bool {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
t := l.now()
|
||||
l.tokens += t.Sub(l.last).Seconds() * l.rate
|
||||
if l.tokens > l.rate {
|
||||
l.tokens = l.rate
|
||||
}
|
||||
l.last = t
|
||||
if l.tokens < 1 {
|
||||
return false
|
||||
}
|
||||
l.tokens--
|
||||
return true
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue