-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
cherry-pick #10693 to release
- Loading branch information
1 parent
2222a11
commit c637e37
Showing
3 changed files
with
57 additions
and
1 deletion.
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
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) | ||
} | ||
} | ||
} |
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