Reputation: 33
I have following code
package main
import (
"fmt"
"net/http"
"time"
)
func Get(url string, client *http.Client, header []string) (*http.Response, error) {
start := time.Now()
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
for i := 0; i < len(header)/2; i++ {
req.Header.Add(header[2*i], header[2*i+1])
}
resp, err := client.Do(req)
fmt.Println(time.Until(start))
return resp, nil
}
func main() {
header := []string{"accept-encoding", "gzip, deflate, br"}
cl := &http.Client{}
url1 := "https://example1.com"
url2 := "https://example2.com"
url3 := "https://www.google.com/"
resp, _ := Get(url1, cl, header)
resp, _ = Get(url2, cl, header)
resp, _ = Get(url3, cl, header)
fmt.Println(resp)
}
The time of resp, _ := Get(url1, cl, header)
is always slow
Regardless of url1
change to https://example2.com or https://www.google.com/
like this
1 254.156959ms
2 34.183151ms
2 87.880252ms
Upvotes: 2
Views: 1154
Reputation: 5395
There is a beautiful article on medium on resolving go's DNS issues. Shortly it refers to the Name_Resolution of the net
package documentation.
More info can be found in src/net/lookup.go
, if you like to read code (and comments).
TL/DR: go can use different dns resolvers, wich you can enable/or test with
GODEBUG=netdns=1 go run main.go
> go package net: hostLookupOrder(redis) = cgo
// osx/macosx case
GODEBUG=netdns=go+2 go run main.go
> go package net: GODEBUG setting forcing use of Go’s resolver go
> package net: hostLookupOrder(redis) = files,dns
Upvotes: 2