mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-29 10:28:53 +03:00
fd25dcacbd
Squashed commit of the following:
commit d0b37e3de1552ea42d776461045a76ff0ae18128
Merge: 025c29bcd ee619b2db
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Thu Apr 4 18:58:08 2024 +0300
Merge branch 'master' into AG-27492-client-runtime-index
commit 025c29bcd279b5448908df7c3a1a997a64095641
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Mon Apr 1 17:20:15 2024 +0300
client: imp code
commit 548a15c000db3989b1398e68fa4c05a450e93ea0
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Thu Mar 28 13:43:17 2024 +0300
all: add tests
commit c9015e732f1e0475ec8cf95487c9ec56cd69a395
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Mon Mar 25 16:33:30 2024 +0300
all: imp docs
commit 81e8b944928176733b2971b2b6400b55496a7843
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Mon Mar 25 15:33:17 2024 +0300
all: imp code
commit 1428d60bf72d7a0ffd9dc854403391646f82c6cc
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Mon Mar 25 14:45:01 2024 +0300
all: client runtime index
63 lines
1.5 KiB
Go
63 lines
1.5 KiB
Go
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
|
|
}
|