Replace Units with Address in IPUsage

Units are not needed anymore, as only one address can be allocated
at once.
This commit is contained in:
ionutboangiu
2025-06-03 17:14:32 +03:00
committed by Dan Christian Bogos
parent d22727782e
commit 0d11ac9d8a
2 changed files with 3 additions and 29 deletions

View File

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

View File

@@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
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)