[#694] pkg/network: Use single function to get or create client instance

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2021-07-13 14:58:26 +03:00 committed by Alex Vanin
parent 2c02261709
commit aed76f6b5a

View file

@ -61,18 +61,9 @@ func (x *multiClient) iterateClients(f func(client.Client) error) error {
var firstErr error var firstErr error
x.addr.IterateAddresses(func(addr network.Address) bool { x.addr.IterateAddresses(func(addr network.Address) bool {
x.mtx.Lock()
strAddr := addr.String()
var err error var err error
c, cached := x.clients[strAddr] c := x.client(addr)
if !cached {
c = x.createForAddress(addr)
}
x.mtx.Unlock()
err = f(c) err = f(c)
@ -322,6 +313,10 @@ func (x *multiClient) Close() error {
} }
func (x *multiClient) RawForAddress(addr network.Address) *rawclient.Client { func (x *multiClient) RawForAddress(addr network.Address) *rawclient.Client {
return x.client(addr).Raw()
}
func (x *multiClient) client(addr network.Address) client.Client {
x.mtx.Lock() x.mtx.Lock()
strAddr := addr.String() strAddr := addr.String()
@ -333,5 +328,5 @@ func (x *multiClient) RawForAddress(addr network.Address) *rawclient.Client {
x.mtx.Unlock() x.mtx.Unlock()
return c.Raw() return c
} }