Last active
September 1, 2020 13:23
-
-
Save simonswine/f1826127e499673df1dbf539df5923b0 to your computer and use it in GitHub Desktop.
Golang DNS test
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
$ GODEBUG=netdns=cgo+1 go run main.go | |
go package net: using cgo DNS resolver | |
2020/08/26 11:15:12 #01 212.58.233.253:443 | |
2020/08/26 11:15:12 #02 212.58.237.251:443 | |
2020/08/26 11:15:13 #03 212.58.233.251:443 | |
2020/08/26 11:15:13 #04 212.58.233.251:443 | |
2020/08/26 11:15:13 #05 212.58.237.253:443 | |
2020/08/26 11:15:13 #06 212.58.237.254:443 | |
2020/08/26 11:15:14 #07 212.58.237.254:443 | |
2020/08/26 11:15:14 #08 212.58.233.254:443 | |
2020/08/26 11:15:14 #09 212.58.233.254:443 | |
$ GODEBUG=netdns=go+1 go run main.go | |
go package net: GODEBUG setting forcing use of Go's resolver | |
2020/08/26 11:15:17 #01 212.58.233.254:443 | |
2020/08/26 11:15:17 #02 212.58.233.253:443 | |
2020/08/26 11:15:18 #03 212.58.233.251:443 | |
2020/08/26 11:15:18 #04 212.58.233.254:443 | |
2020/08/26 11:15:18 #05 212.58.233.253:443 | |
2020/08/26 11:15:19 #06 212.58.233.254:443 | |
2020/08/26 11:15:19 #07 212.58.233.253:443 | |
2020/08/26 11:15:19 #08 212.58.233.253:443 | |
2020/08/26 11:15:19 #09 212.58.233.253:443 |
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
package main | |
import ( | |
"context" | |
"log" | |
"net" | |
"net/http" | |
) | |
func main() { | |
var remoteAddr string | |
client := &http.Client{ | |
Transport: &http.Transport{ | |
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { | |
conn, err := net.Dial(network, addr) | |
remoteAddr = conn.RemoteAddr().String() | |
return conn, err | |
}, | |
}, | |
} | |
for i := 1; i < 10; i++ { | |
resp, err := client.Get("https://www.bbc.co.uk/") | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer resp.Body.Close() | |
log.Printf("#%02d %v %s", i, remoteAddr, resp.Request.RemoteAddr) | |
} | |
} |
Running your code I get a fairly equal distribution with both DNS methods. I think you might have something caching locally (like a nscd daemon)
Gosh darn it, I looked for systemd-resolved because I thought that NixOS was pretty systemd heavy but it turns out that indeed I have nscd running. I thought most distributions moved away from nscd...
Good catch. It was indeed good evidence for local DNS caching (although weirdly the Go resolver seemed to bypass it anyway).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Interestingly I get very different behavior on my system running a slightly modified version of the above Gist: