Use a RWMutex instead of a Mutex for authosts
This commit is contained in:
parent
bdff46ec1d
commit
f893df7e64
@ -21,7 +21,7 @@ type onChangedT func()
|
|||||||
// AutoHosts - automatic DNS records
|
// AutoHosts - automatic DNS records
|
||||||
type AutoHosts struct {
|
type AutoHosts struct {
|
||||||
// lock protects table and tableReverse.
|
// lock protects table and tableReverse.
|
||||||
lock sync.Mutex
|
lock sync.RWMutex
|
||||||
// table is the host-to-IPs map.
|
// table is the host-to-IPs map.
|
||||||
table map[string][]net.IP
|
table map[string][]net.IP
|
||||||
// tableReverse is the IP-to-hosts map.
|
// tableReverse is the IP-to-hosts map.
|
||||||
@ -119,14 +119,14 @@ func (a *AutoHosts) Process(host string, qtype uint16) []net.IP {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var ipsCopy []net.IP
|
var ipsCopy []net.IP
|
||||||
a.lock.Lock()
|
a.lock.RLock()
|
||||||
|
|
||||||
if ips, ok := a.table[host]; ok {
|
if ips, ok := a.table[host]; ok {
|
||||||
ipsCopy = make([]net.IP, len(ips))
|
ipsCopy = make([]net.IP, len(ips))
|
||||||
copy(ipsCopy, ips)
|
copy(ipsCopy, ips)
|
||||||
}
|
}
|
||||||
|
|
||||||
a.lock.Unlock()
|
a.lock.RUnlock()
|
||||||
|
|
||||||
log.Debug("AutoHosts: answer: %s -> %v", host, ipsCopy)
|
log.Debug("AutoHosts: answer: %s -> %v", host, ipsCopy)
|
||||||
return ipsCopy
|
return ipsCopy
|
||||||
@ -145,8 +145,8 @@ func (a *AutoHosts) ProcessReverse(addr string, qtype uint16) (hosts []string) {
|
|||||||
|
|
||||||
ipStr := ipReal.String()
|
ipStr := ipReal.String()
|
||||||
|
|
||||||
a.lock.Lock()
|
a.lock.RLock()
|
||||||
defer a.lock.Unlock()
|
defer a.lock.RUnlock()
|
||||||
|
|
||||||
hosts = a.tableReverse[ipStr]
|
hosts = a.tableReverse[ipStr]
|
||||||
|
|
||||||
@ -161,8 +161,8 @@ func (a *AutoHosts) ProcessReverse(addr string, qtype uint16) (hosts []string) {
|
|||||||
|
|
||||||
// List returns an IP-to-hostnames table. It is safe for concurrent use.
|
// List returns an IP-to-hostnames table. It is safe for concurrent use.
|
||||||
func (a *AutoHosts) List() (ipToHosts map[string][]string) {
|
func (a *AutoHosts) List() (ipToHosts map[string][]string) {
|
||||||
a.lock.Lock()
|
a.lock.RLock()
|
||||||
defer a.lock.Unlock()
|
defer a.lock.RUnlock()
|
||||||
|
|
||||||
ipToHosts = make(map[string][]string, len(a.tableReverse))
|
ipToHosts = make(map[string][]string, len(a.tableReverse))
|
||||||
for k, v := range a.tableReverse {
|
for k, v := range a.tableReverse {
|
||||||
|
Loading…
Reference in New Issue
Block a user