Pull request: 2846 cover aghnet vol.3
Merge in DNS/adguard-home from 2846-cover-aghnet-vol.3 to master Updates #2846. Squashed commit of the following: commit cb22987c43c17bbc8d098e65639cc84e2284bc7b Merge: cf995e9df31ffcc5Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Mar 30 15:01:10 2022 +0300 Merge branch 'master' into 2846-cover-aghnet-vol.3 commit cf995e9dce635f16e10406a61e2ab12f06407f1f Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Mar 28 18:29:50 2022 +0300 aghnet: imp tests commit bc225fe8800633b29216840bc7d5b82d7c2d2bfb Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Mar 28 18:03:06 2022 +0300 aghnet: imp tests commit a82eb6045495b94a2e81ced9a3ef5bfe65788e56 Merge: f80812490d562a7bAuthor: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Mar 28 17:39:13 2022 +0300 Merge branch 'master' into 2846-cover-aghnet-vol.3 commit f80812490c49f69655d409c6f015b069affa2f19 Merge: edccaa793603b1fcAuthor: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Mar 28 17:29:20 2022 +0300 Merge branch 'master' into 2846-cover-aghnet-vol.3 commit edccaa79fca061ffeea1985c293eed123b16a09c Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Mar 28 13:53:40 2022 +0300 aghnet: imp tests commit 7c5028c92f0a6680516bda67c73e794182c9b825 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Mar 25 18:01:28 2022 +0300 aghnet: imp code & docs commit 7897c6b13e9be340ae8a71947a8a0bab82c682eb Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Mar 25 17:11:46 2022 +0300 aghnet: imp coverage commit 1eef110af3bf721a0275c695bf27c31815abff04 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Mar 23 21:10:29 2022 +0300 all: return byte slice
This commit is contained in:
@@ -2,13 +2,11 @@ package aghnet
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
||||
"github.com/AdguardTeam/golibs/errors"
|
||||
"github.com/AdguardTeam/golibs/netutil"
|
||||
)
|
||||
@@ -116,50 +114,33 @@ func (ns *neighs) reset(with []Neighbor) {
|
||||
// of Neighbors.
|
||||
type parseNeighsFunc func(sc *bufio.Scanner, lenHint int) (ns []Neighbor)
|
||||
|
||||
// runCmdFunc is the function that runs some command and returns its output
|
||||
// wrapped to be a io.Reader.
|
||||
type runCmdFunc func() (r io.Reader, err error)
|
||||
|
||||
// cmdARPDB is the implementation of the ARPDB that uses command line to
|
||||
// retrieve data.
|
||||
type cmdARPDB struct {
|
||||
parse parseNeighsFunc
|
||||
runcmd runCmdFunc
|
||||
ns *neighs
|
||||
parse parseNeighsFunc
|
||||
ns *neighs
|
||||
cmd string
|
||||
args []string
|
||||
}
|
||||
|
||||
// type check
|
||||
var _ ARPDB = (*cmdARPDB)(nil)
|
||||
|
||||
// runCmd runs the cmd with it's args and returns the result wrapped to be an
|
||||
// io.Reader. The error is returned either if the exit code retured by command
|
||||
// not equals 0 or the execution itself failed.
|
||||
func runCmd(cmd string, args ...string) (r io.Reader, err error) {
|
||||
var code int
|
||||
var out string
|
||||
code, out, err = aghos.RunCommand(cmd, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if code != 0 {
|
||||
return nil, fmt.Errorf("unexpected exit code %d", code)
|
||||
}
|
||||
|
||||
return strings.NewReader(out), nil
|
||||
}
|
||||
|
||||
// Refresh implements the ARPDB interface for *cmdARPDB.
|
||||
func (arp *cmdARPDB) Refresh() (err error) {
|
||||
defer func() { err = errors.Annotate(err, "cmd arpdb: %w") }()
|
||||
|
||||
var r io.Reader
|
||||
r, err = arp.runcmd()
|
||||
code, out, err := aghosRunCommand(arp.cmd, arp.args...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("running command: %w", err)
|
||||
} else if code != 0 {
|
||||
return fmt.Errorf("running command: unexpected exit code %d", code)
|
||||
}
|
||||
|
||||
sc := bufio.NewScanner(r)
|
||||
sc := bufio.NewScanner(bytes.NewReader(out))
|
||||
ns := arp.parse(sc, arp.ns.len())
|
||||
if err = sc.Err(); err != nil {
|
||||
// TODO(e.burkov): This error seems unreachable. Investigate.
|
||||
return fmt.Errorf("scanning the output: %w", err)
|
||||
}
|
||||
|
||||
@@ -180,8 +161,7 @@ func (arp *cmdARPDB) Neighbors() (ns []Neighbor) {
|
||||
type arpdbs struct {
|
||||
// arps is the set of ARPDB implementations to range through.
|
||||
arps []ARPDB
|
||||
// last is the last succeeded ARPDB index.
|
||||
last int
|
||||
neighs
|
||||
}
|
||||
|
||||
// newARPDBs returns a properly initialized *arpdbs. It begins refreshing from
|
||||
@@ -189,7 +169,10 @@ type arpdbs struct {
|
||||
func newARPDBs(arps ...ARPDB) (arp *arpdbs) {
|
||||
return &arpdbs{
|
||||
arps: arps,
|
||||
last: 0,
|
||||
neighs: neighs{
|
||||
mu: &sync.RWMutex{},
|
||||
ns: make([]Neighbor, 0),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -199,20 +182,18 @@ var _ ARPDB = (*arpdbs)(nil)
|
||||
// Refresh implements the ARPDB interface for *arpdbs.
|
||||
func (arp *arpdbs) Refresh() (err error) {
|
||||
var errs []error
|
||||
l := len(arp.arps)
|
||||
// Start from the last succeeded implementation.
|
||||
for i := 0; i < l; i++ {
|
||||
cur := (arp.last + i) % l
|
||||
err = arp.arps[cur].Refresh()
|
||||
if err == nil {
|
||||
// The succeeded implementation found so update the last succeeded
|
||||
// index.
|
||||
arp.last = cur
|
||||
|
||||
return nil
|
||||
for _, a := range arp.arps {
|
||||
err = a.Refresh()
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
errs = append(errs, err)
|
||||
arp.reset(a.Neighbors())
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(errs) > 0 {
|
||||
@@ -223,10 +204,8 @@ func (arp *arpdbs) Refresh() (err error) {
|
||||
}
|
||||
|
||||
// Neighbors implements the ARPDB interface for *arpdbs.
|
||||
//
|
||||
// TODO(e.burkov): Think of a way to avoid cloning the slice twice.
|
||||
func (arp *arpdbs) Neighbors() (ns []Neighbor) {
|
||||
if l := len(arp.arps); l > 0 && arp.last < l {
|
||||
return arp.arps[arp.last].Neighbors()
|
||||
}
|
||||
|
||||
return nil
|
||||
return arp.clone()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user