-(home): fix deadlock in clients

This commit is contained in:
Andrey Meshkov 2019-12-23 16:27:24 +03:00
parent ec7a62e123
commit 9b93d43ac6
1 changed files with 9 additions and 4 deletions

View File

@ -185,14 +185,19 @@ func (clients *clientsContainer) Exists(ip string, source clientSource) bool {
// Find searches for a client by IP // Find searches for a client by IP
func (clients *clientsContainer) Find(ip string) (Client, bool) { func (clients *clientsContainer) Find(ip string) (Client, bool) {
clients.lock.Lock()
defer clients.lock.Unlock()
return clients.findByIP(ip)
}
// Find searches for a client by IP (and does not lock anything)
func (clients *clientsContainer) findByIP(ip string) (Client, bool) {
ipAddr := net.ParseIP(ip) ipAddr := net.ParseIP(ip)
if ipAddr == nil { if ipAddr == nil {
return Client{}, false return Client{}, false
} }
clients.lock.Lock()
defer clients.lock.Unlock()
c, ok := clients.idIndex[ip] c, ok := clients.idIndex[ip]
if ok { if ok {
return *c, true return *c, true
@ -456,7 +461,7 @@ func (clients *clientsContainer) AddHost(ip, host string, source clientSource) (
defer clients.lock.Unlock() defer clients.lock.Unlock()
// check existing clients first // check existing clients first
_, ok := clients.Find(ip) _, ok := clients.findByIP(ip)
if ok { if ok {
return false, nil return false, nil
} }