[#1157] network/cache: Cache multiclients based on public key only

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-03-21 15:02:27 +03:00 committed by Alex Vanin
parent de5a2f6574
commit a4261243fc
2 changed files with 31 additions and 16 deletions

View file

@ -67,6 +67,32 @@ func (x *multiClient) createForAddress(addr network.Address) clientcore.Client {
return &c
}
// updateGroup replaces current multiClient addresses with a new group.
// Old addresses not present in group are removed.
func (x *multiClient) updateGroup(group network.AddressGroup) {
// Firstly, remove old clients.
cache := make([]string, 0, group.Len())
group.IterateAddresses(func(a network.Address) bool {
cache = append(cache, a.String())
return false
})
x.mtx.Lock()
defer x.mtx.Unlock()
loop:
for a := range x.clients {
for i := range cache {
if cache[i] == a {
continue loop
}
}
delete(x.clients, a)
}
// Then add new clients.
x.addr = group
}
func (x *multiClient) iterateClients(ctx context.Context, f func(clientcore.Client) error) error {
var firstErr error