kdhcp/iptools/pool.go

153 lines
3.0 KiB
Go
Raw Normal View History

2023-05-03 02:40:31 +00:00
package iptools
import (
2023-05-06 05:21:27 +00:00
"fmt"
2023-05-03 02:40:31 +00:00
"net/netip"
2023-05-06 05:21:27 +00:00
"sync"
2023-05-03 02:40:31 +00:00
"time"
2023-05-06 05:21:27 +00:00
"git.wntrmute.dev/kyle/goutils/assert"
2023-05-03 02:40:31 +00:00
)
2023-05-06 05:21:27 +00:00
const DefaultExpiry = 168 * time.Hour
2023-05-03 02:40:31 +00:00
type Pool struct {
2023-05-06 05:21:27 +00:00
Name string `yaml:"name"`
Range *Range `yaml:"range"`
Expiry time.Duration `yaml:"expiry"`
Available []*LeaseInfo `yaml:"available"`
Active map[netip.Addr]*LeaseInfo `yaml:"active"`
Limbo map[netip.Addr]*LeaseInfo `yaml:"limbo"` // leases that are currently being offered
NoHostName bool `yaml:"no_hostname"` // don't set the hostname
mtx *sync.Mutex
}
func (p *Pool) lock() {
if p.lock == nil {
p.mtx = &sync.Mutex{}
}
p.mtx.Lock()
}
func (p *Pool) unlock() {
if p.mtx == nil {
panic("pool: attempted to unlock uninitialized mutex")
}
p.mtx.Unlock()
2023-05-03 02:40:31 +00:00
}
2023-05-06 05:21:27 +00:00
func NewPool(name string, r *Range) (*Pool, error) {
if r.Expiry == 0 {
r.Expiry = DefaultExpiry
}
2023-05-03 02:40:31 +00:00
p := &Pool{
2023-05-06 05:21:27 +00:00
Name: name,
Expiry: r.Expiry,
NoHostName: r.NoHostName,
Available: enumerateRange(name, r, true),
2023-05-03 02:40:31 +00:00
}
return p, nil
}
2023-05-06 05:21:27 +00:00
func (p *Pool) sort() {
2023-05-03 02:40:31 +00:00
p.Available = SortLeases(p.Available)
}
2023-05-06 05:21:27 +00:00
func (p *Pool) Sort() {
p.lock()
defer p.unlock()
p.sort()
}
2023-05-03 02:40:31 +00:00
func (p *Pool) IsAddressAvailable() bool {
return len(p.Available) > 0
}
2023-05-06 05:21:27 +00:00
// Peek returns the first available address from the pool and moves it
// from available to limbo. When the client is given the address, Accept
// should be called on the address to move it from limbo to active.
func (p *Pool) Peek(t time.Time, waitFor time.Duration) *LeaseInfo {
p.lock()
defer p.unlock()
li := p.peek(t, waitFor)
return li
}
func (p *Pool) peek(t time.Time, waitFor time.Duration) *LeaseInfo {
if len(p.Available) == 0 {
return nil
}
2023-05-03 02:40:31 +00:00
lease := p.Available[0]
p.Available = p.Available[1:]
lease.ResetExpiry(t, waitFor)
p.Limbo[lease.Addr] = lease
2023-05-06 05:21:27 +00:00
return lease
}
func (p *Pool) accept(addr netip.Addr) error {
assert.Bool(p.Active[addr] == nil, fmt.Sprintf("limbo address %s is already active: %#v", addr, *p.Active[addr]))
p.Active[addr] = p.Limbo[addr]
delete(p.Limbo, addr)
return nil
}
// Accept will move an address out limbo and mark it as active.
func (p *Pool) Accept(addr netip.Addr, t time.Time) error {
p.lock()
defer p.unlock()
p.update(t)
p.accept(addr)
return nil
2023-05-03 02:40:31 +00:00
}
// Update checks expirations on leases, moving any expired leases back
// to the available pool and removing any limbo leases that are expired.
func (p *Pool) Update(t time.Time) {
2023-05-06 05:21:27 +00:00
p.lock()
defer p.unlock()
p.update(t)
}
func (p *Pool) update(t time.Time) {
evictedHosts := []netip.Addr{}
2023-05-03 02:40:31 +00:00
for k, v := range p.Active {
if v.IsExpired(t) {
2023-05-06 05:21:27 +00:00
evictedHosts = append(evictedHosts, k)
2023-05-03 02:40:31 +00:00
v = v.Reset()
p.Available = append(p.Available, v)
}
}
2023-05-06 05:21:27 +00:00
for _, ip := range evictedHosts {
delete(p.Active, ip)
}
2023-05-03 02:40:31 +00:00
for k, v := range p.Limbo {
if v.IsExpired(t) {
delete(p.Limbo, k)
v = v.Reset()
p.Available = append(p.Available, v)
}
}
p.Sort()
}
func (p *Pool) Renew(lease LeaseInfo, t time.Time) {
}