* control: DHCP: don't return expired leases

- fix potential race when lease's data can be modified while UI thread is reading it
This commit is contained in:
Simon Zolin 2019-03-19 15:54:35 +03:00
parent 6f56eb4c12
commit ef789acee4
1 changed files with 9 additions and 2 deletions

View File

@ -517,10 +517,17 @@ func (s *Server) handleDecline(p dhcp4.Packet, options dhcp4.Options) dhcp4.Pack
}
// Leases returns the list of current DHCP leases (thread-safe)
func (s *Server) Leases() []*Lease {
func (s *Server) Leases() []Lease {
var result []Lease
now := time.Now().Unix()
s.RLock()
result := s.leases
for _, lease := range s.leases {
if lease.Expiry.Unix() > now {
result = append(result, *lease)
}
}
s.RUnlock()
return result
}