Pull request: 2552 rm context.TODO() instances
Merge in DNS/adguard-home from 2552-context to master
Closes #2552.
Squashed commit of the following:
commit 3d1cef33da529f4611869c4a0f2f294a3c8afcaf
Author: Eugene Burkov <e.burkov@adguard.com>
Date: Tue Jan 26 19:28:23 2021 +0300
all: fix docs
commit d08c78cf4b96419b928e73c497768f40c9e47bc2
Author: Eugene Burkov <e.burkov@adguard.com>
Date: Tue Jan 26 19:22:00 2021 +0300
all: doc changes
commit c2814f4d0025be74f38299e7e66e7c0193b6c15f
Merge: 100a1a09 44c7221a
Author: Eugene Burkov <e.burkov@adguard.com>
Date: Tue Jan 26 19:12:55 2021 +0300
Merge branch 'master' into 2552-context
commit 100a1a0957bc22bfaccb1693e6b9b1c5cb53ed13
Author: Eugene Burkov <e.burkov@adguard.com>
Date: Tue Jan 26 19:10:03 2021 +0300
home: imp docs, fix naming
commit 22717abe6c0e4c1016a53ff2fac1689d0762c462
Author: Eugene Burkov <e.burkov@adguard.com>
Date: Tue Jan 26 18:14:07 2021 +0300
home: improve code quality
commit 5c96f77a2b315e2c1ad4a11cc7a64f61bdba52a3
Author: Eugene Burkov <e.burkov@adguard.com>
Date: Mon Jan 25 20:28:51 2021 +0300
home: add docs
commit 323fc013a57a5c06ec391003133b12f4eb2721cd
Author: Eugene Burkov <e.burkov@adguard.com>
Date: Mon Jan 25 14:50:11 2021 +0300
home: rm context.TODO() instances
This commit is contained in:
@@ -35,19 +35,20 @@ type Whois struct {
|
||||
ipAddrs cache.Cache
|
||||
}
|
||||
|
||||
// Create module context
|
||||
// initWhois creates the Whois module context.
|
||||
func initWhois(clients *clientsContainer) *Whois {
|
||||
w := Whois{}
|
||||
w.timeoutMsec = 5000
|
||||
w.clients = clients
|
||||
w := Whois{
|
||||
timeoutMsec: 5000,
|
||||
clients: clients,
|
||||
ipAddrs: cache.New(cache.Config{
|
||||
EnableLRU: true,
|
||||
MaxCount: 10000,
|
||||
}),
|
||||
ipChan: make(chan net.IP, 255),
|
||||
}
|
||||
|
||||
cconf := cache.Config{}
|
||||
cconf.EnableLRU = true
|
||||
cconf.MaxCount = 10000
|
||||
w.ipAddrs = cache.New(cconf)
|
||||
|
||||
w.ipChan = make(chan net.IP, 255)
|
||||
go w.workerLoop()
|
||||
|
||||
return &w
|
||||
}
|
||||
|
||||
@@ -120,12 +121,12 @@ func whoisParse(data string) map[string]string {
|
||||
const MaxConnReadSize = 64 * 1024
|
||||
|
||||
// Send request to a server and receive the response
|
||||
func (w *Whois) query(target, serverAddr string) (string, error) {
|
||||
func (w *Whois) query(ctx context.Context, target, serverAddr string) (string, error) {
|
||||
addr, _, _ := net.SplitHostPort(serverAddr)
|
||||
if addr == "whois.arin.net" {
|
||||
target = "n + " + target
|
||||
}
|
||||
conn, err := customDialContext(context.TODO(), "tcp", serverAddr)
|
||||
conn, err := customDialContext(ctx, "tcp", serverAddr)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -153,11 +154,11 @@ func (w *Whois) query(target, serverAddr string) (string, error) {
|
||||
}
|
||||
|
||||
// Query WHOIS servers (handle redirects)
|
||||
func (w *Whois) queryAll(target string) (string, error) {
|
||||
func (w *Whois) queryAll(ctx context.Context, target string) (string, error) {
|
||||
server := net.JoinHostPort(defaultServer, defaultPort)
|
||||
const maxRedirects = 5
|
||||
for i := 0; i != maxRedirects; i++ {
|
||||
resp, err := w.query(target, server)
|
||||
resp, err := w.query(ctx, target, server)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -183,9 +184,9 @@ func (w *Whois) queryAll(target string) (string, error) {
|
||||
}
|
||||
|
||||
// Request WHOIS information
|
||||
func (w *Whois) process(ip net.IP) [][]string {
|
||||
func (w *Whois) process(ctx context.Context, ip net.IP) [][]string {
|
||||
data := [][]string{}
|
||||
resp, err := w.queryAll(ip.String())
|
||||
resp, err := w.queryAll(ctx, ip.String())
|
||||
if err != nil {
|
||||
log.Debug("Whois: error: %s IP:%s", err, ip)
|
||||
return data
|
||||
@@ -232,12 +233,13 @@ func (w *Whois) Begin(ip net.IP) {
|
||||
}
|
||||
}
|
||||
|
||||
// Get IP address from channel; get WHOIS info; associate info with a client
|
||||
// workerLoop processes the IP addresses it got from the channel and associates
|
||||
// the retrieving WHOIS info with a client.
|
||||
func (w *Whois) workerLoop() {
|
||||
for {
|
||||
ip := <-w.ipChan
|
||||
|
||||
info := w.process(ip)
|
||||
info := w.process(context.Background(), ip)
|
||||
if len(info) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user