如何使go dns查找在可用时强制使用nscd?

huangapple go评论136阅读模式
英文:

How do i make go dns lookup force usage of nscd if available?

问题

有没有办法强制一个Go二进制文件在同一台主机上执行DNS查找并从nscd缓存中获取响应,如果可用的话?
我猜如果我使用自定义解析器,可以强制执行,但我想避免这样做。想知道netdns=cgo是否会在这里有所帮助?

英文:

Is there any way i can force a go binary to perform dns lookup's and get response from the nscd cache on the same host if it is available?
I assume it can be forced if i use a custom resolver, but i'd like to avoid that. Wondering if netdns=cgo would help here?

答案1

得分: 1

是的,但这取决于你的环境。

你可以通过在终端中输入以下命令来使用cgo解析器:

export GODEBUG=netdns=cgo

然后你的Go应用程序将使用cgo解析器。

如果你的操作系统是UNIX,cgo解析器将使用getaddrinfo来解析域名。

而且,如果nscd服务正在运行,getaddrinfo将使用nscd

参考资料:

[1] https://pkg.go.dev/net#hdr-Name_Resolution

[2] https://stackoverflow.com/questions/70468536/is-golang-caching-dns

[3] https://revs.runtime-revolution.com/golang-dns-resolving-a56ed59981fe

英文:

Yes but it depends on your environment.
You can use cgo resolver by typing to terminal

export GODEBUG=netdns=cgo

Then your go application will use cgo resolver.

cgo resolver use getaddrinfo when resolving domain names if your OS is UNIX.

And getaddrinfo will use nscd if nscd service is running.

ref.

[1] https://pkg.go.dev/net#hdr-Name_Resolution

[2] https://stackoverflow.com/questions/70468536/is-golang-caching-dns

[3] https://revs.runtime-revolution.com/golang-dns-resolving-a56ed59981fe

huangapple
  • 本文由 发表于 2022年3月26日 16:35:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/71626490.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定