From 0d11ac9d8aed085ff1f5bc57bb90e8cf64b884cb Mon Sep 17 00:00:00 2001 From: ionutboangiu Date: Tue, 3 Jun 2025 17:14:32 +0300 Subject: [PATCH] Replace Units with Address in IPUsage Units are not needed anymore, as only one address can be allocated at once. --- ips/ips.go | 19 +------------------ utils/ips.go | 13 ++----------- 2 files changed, 3 insertions(+), 29 deletions(-) diff --git a/ips/ips.go b/ips/ips.go index e9e7ab583..ed98b50c7 100644 --- a/ips/ips.go +++ b/ips/ips.go @@ -70,7 +70,6 @@ type ipAllocations struct { IPAllocations *utils.IPAllocations lkID string // ID of the lock used when matching the ipAllocations ttl *time.Duration // time to leave for these ip allocations, picked up on each IPAllocations initialization out of config - tUsage *float64 // sum of all usages dirty *bool // the usages were modified, needs save, *bool so we only save if enabled in config cfg *ipProfile // for ordering purposes } @@ -113,22 +112,12 @@ func (a *ipAllocations) removeExpiredUnits() { return } for _, uID := range a.IPAllocations.TTLIdx[:firstActive] { - usage, has := a.IPAllocations.Usages[uID] - if !has { + if _, has := a.IPAllocations.Usages[uID]; !has { continue } delete(a.IPAllocations.Usages, uID) - if a.tUsage != nil { // total usage was not yet calculated so we do not need to update it - *a.tUsage -= usage.Units - if *a.tUsage < 0 { // something went wrong - utils.Logger.Warning( - fmt.Sprintf("resetting total usage for IP allocations %q, usage smaller than 0: %f", a.IPAllocations.ID, *a.tUsage)) - a.tUsage = nil - } - } } a.IPAllocations.TTLIdx = a.IPAllocations.TTLIdx[firstActive:] - a.tUsage = nil } // recordUsage records a new usage @@ -144,9 +133,6 @@ func (a *ipAllocations) recordUsage(usage *utils.IPUsage) error { usage.ExpiryTime = time.Now().Add(*a.ttl) } a.IPAllocations.Usages[usage.ID] = usage - if a.tUsage != nil { - *a.tUsage += usage.Units - } if !usage.ExpiryTime.IsZero() { a.IPAllocations.TTLIdx = append(a.IPAllocations.TTLIdx, usage.ID) } @@ -167,9 +153,6 @@ func (a *ipAllocations) clearUsage(usageID string) error { } } } - if a.tUsage != nil { - *a.tUsage -= usage.Units - } delete(a.IPAllocations.Usages, usageID) return nil } diff --git a/utils/ips.go b/utils/ips.go index 7241a6808..f0d8d2b49 100644 --- a/utils/ips.go +++ b/utils/ips.go @@ -19,6 +19,7 @@ along with this program. If not, see package utils import ( + "net/netip" "slices" "time" ) @@ -330,7 +331,7 @@ type IPUsage struct { Tenant string ID string ExpiryTime time.Time - Units float64 + Address netip.Addr } // TenantID returns the concatenated key between tenant and ID. @@ -395,16 +396,6 @@ func (a *IPAllocations) TenantID() string { return ConcatenatedKey(a.Tenant, a.ID) } -// TotalUsage returns the sum of all usage units -// Exported to be used in FilterS -func (a *IPAllocations) TotalUsage() float64 { - var tu float64 - for _, ru := range a.Usages { - tu += ru.Units - } - return tu -} - // IPAllocationsLockKey returns the ID used to lock IP allocations with guardian func IPAllocationsLockKey(tnt, id string) string { return ConcatenatedKey(CacheIPAllocations, tnt, id)