Skip to content

Commit

Permalink
downloader: Number of DNS requests seem excessive (#5145) (#10693)
Browse files Browse the repository at this point in the history
  • Loading branch information
battlmonstr authored Jun 13, 2024
1 parent 2d3db5b commit 7a69c3d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
7 changes: 7 additions & 0 deletions erigon-lib/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2717,6 +2717,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

err = func() error {
defer func() {
if err := recover(); err != nil {
Expand All @@ -2735,5 +2738,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 @@ -131,7 +132,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

0 comments on commit 7a69c3d

Please sign in to comment.