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:
GnomeZworc 2026-10-25 20:32:06 +01:00
commit aa1cc58ae3
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
20 changed files with 2235 additions and 0 deletions

View 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
}