Skip to content

Commit

Permalink
all: imp code
Browse files Browse the repository at this point in the history
  • Loading branch information
schzhn committed Mar 25, 2024
1 parent 1428d60 commit 81e8b94
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 33 deletions.
46 changes: 19 additions & 27 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"encoding"
"fmt"
"net/netip"
"sync"

"github.com/AdguardTeam/AdGuardHome/internal/whois"
)
Expand Down Expand Up @@ -58,6 +57,9 @@ func (cs Source) MarshalText() (text []byte, err error) {

// Runtime is a client information from different sources.
type Runtime struct {
// ip is an IP address of a client.
ip netip.Addr

// whois is the filtered WHOIS information of a client.
whois *whois.Info

Expand All @@ -82,6 +84,13 @@ type Runtime struct {
hostsFile []string
}

// NewRuntime constructs a new runtime client.
func NewRuntime(ip netip.Addr) (r *Runtime) {
return &Runtime{
ip: ip,
}
}

// Info returns a client information from the highest-priority source.
func (r *Runtime) Info() (cs Source, host string) {
info := []string{}
Expand Down Expand Up @@ -160,76 +169,59 @@ func (r *Runtime) IsEmpty() (ok bool) {
r.hostsFile == nil
}

// Addr returns an IP address of the client.
func (r *Runtime) Addr() (ip netip.Addr) {
return r.ip
}

// RuntimeIndex stores information about runtime clients.
type RuntimeIndex struct {
// indexMu protects index.
indexMu *sync.RWMutex

// index maps IP address to runtime client.
index map[netip.Addr]*Runtime
}

// NewRuntimeIndex returns initialized runtime index.
func NewRuntimeIndex() (ri *RuntimeIndex) {
return &RuntimeIndex{
indexMu: &sync.RWMutex{},
index: map[netip.Addr]*Runtime{},
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, ok bool) {
ri.indexMu.RLock()
defer ri.indexMu.RUnlock()

rc, ok = ri.index[ip]

return rc, ok
}

// Add saves the runtime client by ip.
func (ri *RuntimeIndex) Add(ip netip.Addr, rc *Runtime) {
ri.indexMu.Lock()
defer ri.indexMu.Unlock()

ri.index[ip] = rc
}

// Size returns the number of the runtime clients.
func (ri *RuntimeIndex) Size() (n int) {
ri.indexMu.RLock()
defer ri.indexMu.RUnlock()

return len(ri.index)
}

// Range calls cb for each runtime client.
func (ri *RuntimeIndex) Range(cb func(ip netip.Addr, rc *Runtime) (cont bool)) {
ri.indexMu.RLock()
defer ri.indexMu.RUnlock()

for ip, rc := range ri.index {
if !cb(ip, rc) {
func (ri *RuntimeIndex) Range(cb func(rc *Runtime) (cont bool)) {
for _, rc := range ri.index {
if !cb(rc) {
return
}
}
}

// Delete removes the runtime client by ip.
func (ri *RuntimeIndex) Delete(ip netip.Addr) {
ri.indexMu.Lock()
defer ri.indexMu.Unlock()

delete(ri.index, ip)
}

// DeleteBySrc removes all runtime clients that have information only from the
// specified source and returns the number of removed clients.
func (ri *RuntimeIndex) DeleteBySrc(src Source) (n int) {
ri.indexMu.Lock()
defer ri.indexMu.Unlock()

for ip, rc := range ri.index {
rc.Unset(src)

Expand Down
9 changes: 5 additions & 4 deletions internal/home/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,9 +559,10 @@ func (clients *clientsContainer) runtimeClient(ip netip.Addr) (rc *client.Runtim
return nil, false
}

rc, ok = clients.runtimeIndex.Client(ip)
clients.lock.Lock()
defer clients.lock.Unlock()

return rc, ok
return clients.runtimeIndex.Client(ip)
}

// findRuntimeClient finds a runtime client by their IP.
Expand All @@ -571,7 +572,7 @@ func (clients *clientsContainer) findRuntimeClient(ip netip.Addr) (rc *client.Ru

if host != "" {
if !ok {
rc = &client.Runtime{}
rc = client.NewRuntime(ip)
}

rc.SetInfo(client.SourceDHCP, []string{host})
Expand Down Expand Up @@ -803,7 +804,7 @@ func (clients *clientsContainer) addHostLocked(
}
}

rc = &client.Runtime{}
rc = client.NewRuntime(ip)
clients.runtimeIndex.Add(ip, rc)
}

Expand Down
4 changes: 2 additions & 2 deletions internal/home/clientshttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ func (clients *clientsContainer) handleGetClients(w http.ResponseWriter, r *http
data.Clients = append(data.Clients, cj)
}

clients.runtimeIndex.Range(func(ip netip.Addr, rc *client.Runtime) (cont bool) {
clients.runtimeIndex.Range(func(rc *client.Runtime) (cont bool) {
src, host := rc.Info()
cj := runtimeClientJSON{
WHOIS: whoisOrEmpty(rc),
Name: host,
Source: src,
IP: ip,
IP: rc.Addr(),
}

data.RuntimeClients = append(data.RuntimeClients, cj)
Expand Down

0 comments on commit 81e8b94

Please sign in to comment.