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

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-03-21 15:07:55 +03:00 committed by Alex Vanin
parent a4261243fc
commit 2a69aaf976

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 { func (x *multiClient) client(addr network.Address) clientcore.Client {
x.mtx.Lock()
strAddr := addr.String() strAddr := addr.String()
x.mtx.RLock()
c, cached := x.clients[strAddr] c, cached := x.clients[strAddr]
x.mtx.RUnlock()
if !cached {
x.mtx.Lock()
c, cached = x.clients[strAddr]
if !cached { if !cached {
c = x.createForAddress(addr) c = x.createForAddress(addr)
} }
x.mtx.Unlock() x.mtx.Unlock()
}
return c return c
} }