Fix race conditions found by -race
This commit is contained in:
parent
368e2d1ebd
commit
4d3f1b83a6
@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/hmage/golibs/log"
|
"github.com/hmage/golibs/log"
|
||||||
@ -47,6 +48,7 @@ type Server struct {
|
|||||||
IPpool map[[4]byte]net.HardwareAddr
|
IPpool map[[4]byte]net.HardwareAddr
|
||||||
|
|
||||||
ServerConfig
|
ServerConfig
|
||||||
|
sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start will listen on port 67 and serve DHCP requests.
|
// Start will listen on port 67 and serve DHCP requests.
|
||||||
@ -179,7 +181,9 @@ func (s *Server) reserveLease(p dhcp4.Packet) (*Lease, error) {
|
|||||||
log.Tracef("Assigning to %s IP address %s", hwaddr, ip.String())
|
log.Tracef("Assigning to %s IP address %s", hwaddr, ip.String())
|
||||||
hostname := p.ParseOptions()[dhcp4.OptionHostName]
|
hostname := p.ParseOptions()[dhcp4.OptionHostName]
|
||||||
lease := &Lease{HWAddr: hwaddr, IP: ip, Hostname: string(hostname)}
|
lease := &Lease{HWAddr: hwaddr, IP: ip, Hostname: string(hostname)}
|
||||||
|
s.Lock()
|
||||||
s.leases = append(s.leases, lease)
|
s.leases = append(s.leases, lease)
|
||||||
|
s.Unlock()
|
||||||
return lease, nil
|
return lease, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -387,5 +391,8 @@ func (s *Server) ServeDHCP(p dhcp4.Packet, msgType dhcp4.MessageType, options dh
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Leases() []*Lease {
|
func (s *Server) Leases() []*Lease {
|
||||||
return s.leases
|
s.RLock()
|
||||||
|
result := s.leases
|
||||||
|
s.RUnlock()
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
@ -142,6 +142,8 @@ func statsRotator() {
|
|||||||
type counter struct {
|
type counter struct {
|
||||||
name string // used as key in periodic stats
|
name string // used as key in periodic stats
|
||||||
value int64
|
value int64
|
||||||
|
|
||||||
|
sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func newDNSCounter(name string) *counter {
|
func newDNSCounter(name string) *counter {
|
||||||
@ -156,7 +158,9 @@ func (c *counter) IncWithTime(when time.Time) {
|
|||||||
statistics.PerMinute.Inc(c.name, when)
|
statistics.PerMinute.Inc(c.name, when)
|
||||||
statistics.PerHour.Inc(c.name, when)
|
statistics.PerHour.Inc(c.name, when)
|
||||||
statistics.PerDay.Inc(c.name, when)
|
statistics.PerDay.Inc(c.name, when)
|
||||||
|
c.Lock()
|
||||||
c.value++
|
c.value++
|
||||||
|
c.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *counter) Inc() {
|
func (c *counter) Inc() {
|
||||||
@ -167,6 +171,8 @@ type histogram struct {
|
|||||||
name string // used as key in periodic stats
|
name string // used as key in periodic stats
|
||||||
count int64
|
count int64
|
||||||
total float64
|
total float64
|
||||||
|
|
||||||
|
sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func newDNSHistogram(name string) *histogram {
|
func newDNSHistogram(name string) *histogram {
|
||||||
@ -180,8 +186,10 @@ func (h *histogram) ObserveWithTime(value float64, when time.Time) {
|
|||||||
statistics.PerMinute.Observe(h.name, when, value)
|
statistics.PerMinute.Observe(h.name, when, value)
|
||||||
statistics.PerHour.Observe(h.name, when, value)
|
statistics.PerHour.Observe(h.name, when, value)
|
||||||
statistics.PerDay.Observe(h.name, when, value)
|
statistics.PerDay.Observe(h.name, when, value)
|
||||||
|
h.Lock()
|
||||||
h.count++
|
h.count++
|
||||||
h.total += value
|
h.total += value
|
||||||
|
h.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *histogram) Observe(value float64) {
|
func (h *histogram) Observe(value float64) {
|
||||||
|
Loading…
Reference in New Issue
Block a user