Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

downloader: Number of DNS requests seem excessive (#5145) #10693

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions erigon-lib/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2718,6 +2718,9 @@ func openClient(ctx context.Context, dbDir, snapDir string, cfg *torrent.ClientC
//})
cfg.DefaultStorage = m

dnsResolver := &downloadercfg.DnsCacheResolver{RefreshTimeout: 24 * time.Hour}
cfg.TrackerDialContext = dnsResolver.DialContext
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dnsResolver object just created without setting DialContext field. Did you mean opposite assignment? Or did you forgot to set DialContext field?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DialContext is a method 🧙


err = func() error {
defer func() {
if err := recover(); err != nil {
Expand All @@ -2736,5 +2739,9 @@ func openClient(ctx context.Context, dbDir, snapDir string, cfg *torrent.ClientC
return nil, nil, nil, nil, fmt.Errorf("torrentcfg.openClient: %w", err)
}

go func() {
dnsResolver.Run(ctx)
}()

return db, c, m, torrentClient, nil
}
49 changes: 49 additions & 0 deletions erigon-lib/downloader/downloadercfg/dns_cache_resolver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package downloadercfg

import (
"context"
"net"
"time"

"github.com/rs/dnscache"
)

// DnsCacheResolver resolves DNS requests for an HTTP client using an in-memory cache.
type DnsCacheResolver struct {
RefreshTimeout time.Duration

resolver dnscache.Resolver
}

func (r *DnsCacheResolver) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
host, port, err := net.SplitHostPort(address)
if err != nil {
return nil, err
}
ips, err := r.resolver.LookupHost(ctx, host)
if err != nil {
return nil, err
}
var conn net.Conn
for _, ip := range ips {
var dialer net.Dialer
conn, err = dialer.DialContext(ctx, network, net.JoinHostPort(ip, port))
if err == nil {
break
}
}
return conn, err
}

func (r *DnsCacheResolver) Run(ctx context.Context) {
ticker := time.NewTicker(r.RefreshTimeout)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
r.resolver.Refresh(true)
}
}
}
2 changes: 1 addition & 1 deletion erigon-lib/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/ledgerwatch/interfaces v0.0.0-20240517122128-635f85ab7b28
github.com/ledgerwatch/log/v3 v3.9.0
github.com/ledgerwatch/secp256k1 v1.0.0
github.com/rs/dnscache v0.0.0-20211102005908-e0241e321417
)

require (
Expand Down Expand Up @@ -129,7 +130,6 @@ require (
github.com/prometheus/procfs v0.12.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/rs/dnscache v0.0.0-20211102005908-e0241e321417 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/showwin/speedtest-go v1.7.5
github.com/sirupsen/logrus v1.9.3 // indirect
Expand Down
Loading