-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
177 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package client | ||
|
||
import "net/netip" | ||
|
||
// RuntimeIndex stores information about runtime clients. | ||
type RuntimeIndex struct { | ||
// index maps IP address to runtime client. | ||
index map[netip.Addr]*Runtime | ||
} | ||
|
||
// NewRuntimeIndex returns initialized runtime index. | ||
func NewRuntimeIndex() (ri *RuntimeIndex) { | ||
return &RuntimeIndex{ | ||
index: map[netip.Addr]*Runtime{}, | ||
} | ||
} | ||
|
||
// Client returns the saved runtime client by ip. If no such client exists, | ||
// returns nil. | ||
func (ri *RuntimeIndex) Client(ip netip.Addr) (rc *Runtime) { | ||
return ri.index[ip] | ||
} | ||
|
||
// Add saves the runtime client in the index. IP address of a client must be | ||
// unique. See [Runtime.Client]. rc must not be nil. | ||
func (ri *RuntimeIndex) Add(rc *Runtime) { | ||
ip := rc.Addr() | ||
ri.index[ip] = rc | ||
} | ||
|
||
// Size returns the number of the runtime clients. | ||
func (ri *RuntimeIndex) Size() (n int) { | ||
return len(ri.index) | ||
} | ||
|
||
// Range calls f for each runtime client in an undefined order. | ||
func (ri *RuntimeIndex) Range(f func(rc *Runtime) (cont bool)) { | ||
for _, rc := range ri.index { | ||
if !f(rc) { | ||
return | ||
} | ||
} | ||
} | ||
|
||
// Delete removes the runtime client by ip. | ||
func (ri *RuntimeIndex) Delete(ip netip.Addr) { | ||
delete(ri.index, ip) | ||
} | ||
|
||
// DeleteBySource removes all runtime clients that have information only from | ||
// the specified source and returns the number of removed clients. | ||
func (ri *RuntimeIndex) DeleteBySource(src Source) (n int) { | ||
for ip, rc := range ri.index { | ||
rc.Unset(src) | ||
|
||
if rc.IsEmpty() { | ||
delete(ri.index, ip) | ||
n++ | ||
} | ||
} | ||
|
||
return n | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package client_test | ||
|
||
import ( | ||
"net/netip" | ||
"testing" | ||
|
||
"github.com/AdguardTeam/AdGuardHome/internal/client" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRuntimeIndex(t *testing.T) { | ||
const cliSrc = client.SourceARP | ||
|
||
var ( | ||
ip1 = netip.MustParseAddr("1.1.1.1") | ||
ip2 = netip.MustParseAddr("2.2.2.2") | ||
ip3 = netip.MustParseAddr("3.3.3.3") | ||
) | ||
|
||
ri := client.NewRuntimeIndex() | ||
currentSize := 0 | ||
|
||
testCases := []struct { | ||
ip netip.Addr | ||
name string | ||
hosts []string | ||
src client.Source | ||
}{{ | ||
src: cliSrc, | ||
ip: ip1, | ||
name: "1", | ||
hosts: []string{"host1"}, | ||
}, { | ||
src: cliSrc, | ||
ip: ip2, | ||
name: "2", | ||
hosts: []string{"host2"}, | ||
}, { | ||
src: cliSrc, | ||
ip: ip3, | ||
name: "3", | ||
hosts: []string{"host3"}, | ||
}} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
rc := client.NewRuntime(tc.ip) | ||
rc.SetInfo(tc.src, tc.hosts) | ||
|
||
ri.Add(rc) | ||
currentSize++ | ||
|
||
got := ri.Client(tc.ip) | ||
assert.Equal(t, rc, got) | ||
}) | ||
} | ||
|
||
t.Run("size", func(t *testing.T) { | ||
assert.Equal(t, currentSize, ri.Size()) | ||
}) | ||
|
||
t.Run("range", func(t *testing.T) { | ||
s := 0 | ||
|
||
ri.Range(func(rc *client.Runtime) (cont bool) { | ||
s++ | ||
|
||
return true | ||
}) | ||
|
||
assert.Equal(t, currentSize, s) | ||
}) | ||
|
||
t.Run("delete", func(t *testing.T) { | ||
ri.Delete(ip1) | ||
currentSize-- | ||
|
||
assert.Equal(t, currentSize, ri.Size()) | ||
}) | ||
|
||
t.Run("delete_by_src", func(t *testing.T) { | ||
assert.Equal(t, currentSize, ri.DeleteBySource(cliSrc)) | ||
assert.Equal(t, 0, ri.Size()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters