forked from GopeedLab/gopeed
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: excessive torrent tracker dns resolve (GopeedLab#634)
- Loading branch information
Showing
7 changed files
with
234 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package bt | ||
|
||
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) | ||
} | ||
} | ||
} |
Oops, something went wrong.