Accountant: Inventar, where, Checkmk, Abrechnung

This commit is contained in:
br committed 2026-10-18 17:08:58 +02:00
1 parent 9f16148cde
commit dd93277750
59 files changed
+7522 -113

No files matched your search

+16 -14
View File
@@ -1,15 +1,17 @@
package server
// Package ratelimit begrenzt Versuche je Schlüssel (z. B. Client-IP) –
// genutzt für die Enrollment-Endpunkte von Server und Accountant.
package ratelimit
import (
"sync"
"time"
)
// rateLimiter begrenzt Versuche je Schlüssel (Client-IP) auf limit pro
// window (gleitendes Fenster). limit < 0 = unbegrenzt.
type rateLimiter struct {
limit int
window time.Duration
// Limiter begrenzt Versuche je Schlüssel (Client-IP) auf Limit pro Window
// (gleitendes Fenster). Limit < 0 = unbegrenzt. Parallel nutzbar.
type Limiter struct {
Limit int
Window time.Duration
now func() time.Time
mu sync.Mutex
@@ -17,22 +19,22 @@ type rateLimiter struct {
lastPurge time.Time
}
// allow verbucht einen Versuch; bei Ablehnung liefert es die Wartezeit.
func (l *rateLimiter) allow(key string) (bool, time.Duration) {
if l.limit < 0 {
// Allow verbucht einen Versuch; bei Ablehnung liefert es die Wartezeit.
func (l *Limiter) Allow(key string) (bool, time.Duration) {
if l.Limit < 0 {
return true, 0
}
now := time.Now()
if l.now != nil {
now = l.now()
}
cutoff := now.Add(-l.window)
cutoff := now.Add(-l.Window)
l.mu.Lock()
defer l.mu.Unlock()
if l.hits == nil {
l.hits = map[string][]time.Time{}
}
if now.Sub(l.lastPurge) >= l.window { // vergessen, was nicht mehr zählt
if now.Sub(l.lastPurge) >= l.Window { // vergessen, was nicht mehr zählt
for k, ts := range l.hits {
if !ts[len(ts)-1].After(cutoff) {
delete(l.hits, k)
@@ -46,15 +48,15 @@ func (l *rateLimiter) allow(key string) (bool, time.Duration) {
i++
}
ts = ts[i:]
if len(ts) >= l.limit {
if len(ts) >= l.Limit {
l.hits[key] = ts
return false, ts[0].Add(l.window).Sub(now)
return false, ts[0].Add(l.Window).Sub(now)
}
l.hits[key] = append(ts, now)
return true, 0
}
func (l *rateLimiter) size() int {
func (l *Limiter) size() int {
l.mu.Lock()
defer l.mu.Unlock()
return len(l.hits)