mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-08-17 06:30:37 +03:00
client: imp code
This commit is contained in:
parent
54212e975b
commit
7faddd8aad
@ -337,14 +337,13 @@ func (ci *Index) Range(f func(c *Persistent) (cont bool)) {
|
||||
}
|
||||
}
|
||||
|
||||
// SortedRange is like [Index.Range] but sorts the keys before iterating
|
||||
// ensuring a predictable order.
|
||||
func (ci *Index) SortedRange(
|
||||
s func(a, b *Persistent) (n int),
|
||||
f func(c *Persistent) (cont bool),
|
||||
) {
|
||||
// RangeByName is like [Index.Range] but sorts the persistent clients by name
|
||||
// before iterating ensuring a predictable order.
|
||||
func (ci *Index) RangeByName(f func(c *Persistent) (cont bool)) {
|
||||
cs := maps.Values(ci.uidToClient)
|
||||
slices.SortFunc(cs, s)
|
||||
slices.SortFunc(cs, func(a, b *Persistent) (n int) {
|
||||
return strings.Compare(a.Name, b.Name)
|
||||
})
|
||||
|
||||
for _, c := range cs {
|
||||
if !f(c) {
|
||||
@ -355,12 +354,8 @@ func (ci *Index) SortedRange(
|
||||
|
||||
// CloseUpstreams closes upstream configurations of persistent clients.
|
||||
func (ci *Index) CloseUpstreams() (err error) {
|
||||
sortFunc := func(a, b *Persistent) (n int) {
|
||||
return strings.Compare(a.Name, b.Name)
|
||||
}
|
||||
|
||||
var errs []error
|
||||
ci.SortedRange(sortFunc, func(c *Persistent) (cont bool) {
|
||||
ci.RangeByName(func(c *Persistent) (cont bool) {
|
||||
err = c.CloseUpstreams()
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
|
@ -125,14 +125,12 @@ func TestClientIndex(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("sorted_range", func(t *testing.T) {
|
||||
sortFunc := func(a, b *Persistent) (n int) {
|
||||
slices.SortFunc(clients, func(a, b *Persistent) (n int) {
|
||||
return strings.Compare(a.Name, b.Name)
|
||||
}
|
||||
|
||||
slices.SortFunc(clients, sortFunc)
|
||||
})
|
||||
|
||||
got := []*Persistent{}
|
||||
ci.SortedRange(sortFunc, func(c *Persistent) (cont bool) {
|
||||
ci.RangeByName(func(c *Persistent) (cont bool) {
|
||||
got = append(got, c)
|
||||
|
||||
return true
|
||||
|
Loading…
Reference in New Issue
Block a user