Skip to content

Commit

Permalink
downloader: Number of DNS requests seem excessive (#5145) (#10739)
Browse files Browse the repository at this point in the history
cherry-pick #10693 to release
  • Loading branch information
battlmonstr authored Jun 13, 2024
1 parent 2222a11 commit c637e37
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 @@ -2591,10 +2591,17 @@ 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

torrentClient, err = torrent.NewClient(cfg)
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("torrent.NewClient: %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-20240320062914-b57f05746087
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 @@ -125,7 +126,6 @@ require (
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // 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.6.12
github.com/sirupsen/logrus v1.9.3 // indirect
Expand Down

0 comments on commit c637e37

Please sign in to comment.