package ldap import ( "net/netip" "time" "tug/internal/platform/ratelimit" ) const ( ActionBind = "ldap.bind" bindWindowEntries = 10000 bindWindowEvery = time.Hour ) var bindWindowRate = ratelimit.Rate{Burst: 1, Every: bindWindowEvery} type bindKey struct { userID string peer netip.Prefix } type bindWindow struct { table *ratelimit.Table[bindKey] } func newBindWindow(now func() time.Time, max int) *bindWindow { return &bindWindow{table: ratelimit.New[bindKey](max, now)} } func (w *bindWindow) first(userID string, addr netip.Addr) bool { _, ok := w.table.Take(bindKey{userID: userID, peer: ratelimit.Prefix(addr)}, bindWindowRate) return ok } func (w *bindWindow) len() int { return w.table.Len() }