优化 IpPool

This commit is contained in:
bjdgyc 2022-10-09 17:55:16 +08:00
parent 83263ff635
commit 219a74b118
1 changed files with 9 additions and 8 deletions

View File

@ -14,11 +14,11 @@ var (
IpPool = &ipPoolConfig{} IpPool = &ipPoolConfig{}
ipActive = map[string]bool{} ipActive = map[string]bool{}
// ipKeep and ipLease ipAddr => type // ipKeep and ipLease ipAddr => type
ipLease = map[string]bool{} ipLease = map[string]bool{}
ipPoolMux sync.Mutex
) )
type ipPoolConfig struct { type ipPoolConfig struct {
mux sync.Mutex
// 计算动态ip // 计算动态ip
Ipv4Gateway net.IP Ipv4Gateway net.IP
Ipv4Mask net.IP Ipv4Mask net.IP
@ -69,17 +69,18 @@ func getIpLease() {
base.Error(err) base.Error(err)
} }
// fmt.Println(keepIpMaps) // fmt.Println(keepIpMaps)
IpPool.mux.Lock() ipPoolMux.Lock()
ipLease = map[string]bool{}
for _, v := range keepIpMaps { for _, v := range keepIpMaps {
ipLease[v.IpAddr] = true ipLease[v.IpAddr] = true
} }
IpPool.mux.Unlock() ipPoolMux.Unlock()
} }
// AcquireIp 获取动态ip // AcquireIp 获取动态ip
func AcquireIp(username, macAddr string) net.IP { func AcquireIp(username, macAddr string) net.IP {
IpPool.mux.Lock() ipPoolMux.Lock()
defer IpPool.mux.Unlock() defer ipPoolMux.Unlock()
tNow := time.Now() tNow := time.Now()
@ -143,8 +144,8 @@ func AcquireIp(username, macAddr string) net.IP {
// 回收ip // 回收ip
func ReleaseIp(ip net.IP, macAddr string) { func ReleaseIp(ip net.IP, macAddr string) {
IpPool.mux.Lock() ipPoolMux.Lock()
defer IpPool.mux.Unlock() defer ipPoolMux.Unlock()
delete(ipActive, ip.String()) delete(ipActive, ip.String())