Merge: - dhcp: store lease data in database on each change rather than once on app stop

Close #852

* commit '0b3ba8224255247fa751a9922f83154e71a26c02':
  - dhcp: store lease data in database on each change rather than once on app stop
  - dhcp: fix race introduced by static lease add/remove from UI thread
This commit is contained in:
Simon Zolin 2019-06-27 10:56:32 +03:00
commit d1987e711d

View File

@ -207,8 +207,6 @@ func (s *Server) Stop() error {
s.cond.Wait()
}
s.mutex.Unlock()
s.dbStore()
return nil
}
@ -235,6 +233,10 @@ func (s *Server) reserveLease(p dhcp4.Packet) (*Lease, error) {
lease := &Lease{HWAddr: hwaddr, Hostname: string(hostname)}
log.Tracef("Lease not found for %s: creating new one", hwaddr)
s.leasesLock.Lock()
defer s.leasesLock.Unlock()
ip, err := s.findFreeIP(hwaddr)
if err != nil {
i := s.findExpiredLease()
@ -245,9 +247,8 @@ func (s *Server) reserveLease(p dhcp4.Packet) (*Lease, error) {
log.Tracef("Assigning IP address %s to %s (lease for %s expired at %s)",
s.leases[i].IP, hwaddr, s.leases[i].HWAddr, s.leases[i].Expiry)
lease.IP = s.leases[i].IP
s.leasesLock.Lock()
s.leases[i] = lease
s.leasesLock.Unlock()
s.dbStore()
s.reserveIP(lease.IP, hwaddr)
return lease, nil
@ -255,9 +256,8 @@ func (s *Server) reserveLease(p dhcp4.Packet) (*Lease, error) {
log.Tracef("Assigning to %s IP address %s", hwaddr, ip.String())
lease.IP = ip
s.leasesLock.Lock()
s.leases = append(s.leases, lease)
s.leasesLock.Unlock()
s.dbStore()
return lease, nil
}
@ -405,11 +405,12 @@ func (s *Server) addrAvailable(target net.IP) bool {
// Add the specified IP to the black list for a time period
func (s *Server) blacklistLease(lease *Lease) {
hw := make(net.HardwareAddr, 6)
s.reserveIP(lease.IP, hw)
s.leasesLock.Lock()
s.reserveIP(lease.IP, hw)
lease.HWAddr = hw
lease.Hostname = ""
lease.Expiry = time.Now().Add(s.leaseTime)
s.dbStore()
s.leasesLock.Unlock()
}
@ -608,19 +609,18 @@ func (s *Server) Leases() []Lease {
// StaticLeases returns the list of statically-configured DHCP leases (thread-safe)
func (s *Server) StaticLeases() []Lease {
s.leasesLock.Lock()
defer s.leasesLock.Unlock()
if s.IPpool == nil {
s.dbLoad()
}
s.leasesLock.Unlock()
var result []Lease
s.leasesLock.RLock()
for _, lease := range s.leases {
if lease.Expiry.Unix() == 1 {
result = append(result, *lease)
}
}
s.leasesLock.RUnlock()
return result
}
@ -650,6 +650,6 @@ func (s *Server) FindIPbyMAC(mac net.HardwareAddr) net.IP {
func (s *Server) reset() {
s.leasesLock.Lock()
s.leases = nil
s.leasesLock.Unlock()
s.IPpool = make(map[[4]byte]net.HardwareAddr)
s.leasesLock.Unlock()
}