[#1157] network/cache: Optimize `client` fetch from `multiClient`

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
remotes/KirillovDenis/master
Evgenii Stratonikov 2022-03-21 15:07:55 +03:00 committed by Alex Vanin
parent a4261243fc
commit 2a69aaf976
1 changed files with 11 additions and 6 deletions

View File

@ -235,16 +235,21 @@ func (x *multiClient) RawForAddress(addr network.Address, f func(client *rawclie
}
func (x *multiClient) client(addr network.Address) clientcore.Client {
x.mtx.Lock()
strAddr := addr.String()
x.mtx.RLock()
c, cached := x.clients[strAddr]
if !cached {
c = x.createForAddress(addr)
}
x.mtx.RUnlock()
x.mtx.Unlock()
if !cached {
x.mtx.Lock()
c, cached = x.clients[strAddr]
if !cached {
c = x.createForAddress(addr)
}
x.mtx.Unlock()
}
return c
}