From 149367cab9c84d3d34271e90ff0d052fad9c6a22 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Wed, 23 Jun 2021 12:17:37 +0300 Subject: [PATCH] [#607] network: Sort addresses in AddressGroup.FromIterator method Implement `sort.Interface` interface on `AddressGroup` and perform sorting in `AddressGroup.FromIterator` method. Addresses with enabled TLS are "less" in terms of slice position. Signed-off-by: Leonard Lyubich --- pkg/network/group.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pkg/network/group.go b/pkg/network/group.go index d5fedae42..b2bf5fabe 100644 --- a/pkg/network/group.go +++ b/pkg/network/group.go @@ -2,6 +2,7 @@ package network import ( "errors" + "sort" "github.com/nspcc-dev/neofs-api-go/pkg/netmap" ) @@ -39,6 +40,22 @@ func (x AddressGroup) IterateAddresses(f func(Address) bool) { } } +// Len returns number of addresses in AddressGroup. +func (x AddressGroup) Len() int { + return len(x) +} + +// Less returns true if i-th address in AddressGroup supports TLS +// and j-th one doesn't. +func (x AddressGroup) Less(i, j int) bool { + return x[i].TLSEnabled() && !x[j].TLSEnabled() +} + +// Swap swaps i-th and j-th addresses in AddressGroup. +func (x AddressGroup) Swap(i, j int) { + x[i], x[j] = x[j], x[i] +} + // MultiAddressIterator is an interface of network address group. type MultiAddressIterator interface { // Must iterate over network addresses and pass each one @@ -50,6 +67,7 @@ type MultiAddressIterator interface { } // FromIterator forms AddressGroup from MultiAddressIterator structure. +// The result is sorted with sort.Sort. // // Returns an error in the absence of addresses or if any of the addresses are incorrect. func (x *AddressGroup) FromIterator(iter MultiAddressIterator) (err error) { @@ -81,6 +99,7 @@ func (x *AddressGroup) FromIterator(iter MultiAddressIterator) (err error) { }) if err == nil { + sort.Sort(as) *x = as }