[#339] pool/tree: Close replaced connection in client map

There is a race condition: multiple clients are created and dialled,
but only one is stored in the map. Others are remaining active but not
used.

With this change, new connection replaces old connection and closes
it.

Signed-off-by: Alex Vanin <a.vanin@yadro.com>
This commit is contained in:
Alexey Vanin 2025-03-04 12:41:07 +03:00
parent c5991fc66d
commit 2d08fa5240

View file

@ -1012,7 +1012,7 @@ LOOP:
continue
}
p.addClientToMap(cnrNode.Hash(), treeCl)
treeCl = p.addClientToMap(cnrNode.Hash(), treeCl)
}
attempts--
@ -1047,10 +1047,16 @@ func (p *Pool) getClientFromMap(hash uint64) (client, bool) {
return cl, ok
}
func (p *Pool) addClientToMap(hash uint64, cl client) {
func (p *Pool) addClientToMap(hash uint64, cl client) client {
p.mutex.Lock()
defer p.mutex.Unlock()
if old, ok := p.clientMap[hash]; ok {
_ = cl.close()
return old
}
p.clientMap[hash] = cl
p.mutex.Unlock()
return cl
}
func (p *Pool) deleteClientFromMap(hash uint64) {