Is there a way to force refresh hosts in go?
Asked Answered
B

1

9

I am running a Beego application that relies on updates in /etc/hosts (via Docker links) to find other servers. /etc/hosts updates fine but the application will not use new hosts unless it is either restarted or after it has waited way too long. After reviewing the documentation for src/net/hosts it looks like I am locked into a 5 minute refresh time.

Is there a way to force this cache to refresh or am I looking at this problem the wrong way?

Byelection answered 13/10, 2015 at 10:17 Comment(6)
I don't see a way to do it without assembly, either.Cupreous
I could change the timeout constant in src/net/hosts.go and roll my own custom go build but there has to be another way. Glad to see I'm not the only one a but stuck on this one.Byelection
Normally dynamic changes should be done via another mechanism, like dns or another config db. If you build the binary with cgo enabled, it will use the system resolver to lookup hosts (that also doesn't guarantee it won't be cached for a short period by your system, but it's usually less than 5 minutes in most cases)Olen
@Byelection I think you might also make a copy of the library that uses hosts.go, and patch it to use a modified version. That's an option unless you use large amount of affected libraries in your solution. Other than that, it's the way it is probably by design... You probably have already selected a solution? Would you please document it as an answer :)Siderite
I just tested this with the following code. When I run it and add the host to my host file, it resolves it immediately. Am I missing something? play.golang.org/p/HwOLn3w9BP Do you depend on another function than net.LookupHost()?Johnnie
I'm also facing similar issue. Hosts added through extra_hosts parameter in Docker Compose is not being identified when calling http.Get methodMoltke
D
0

The hosts file cache time in Go was shortened to 5 seconds. The behaviour of Go differs whether the pure Go (default, with exceptions) or Cgo resolver is used. To avoid the Go internal cache of the pure Go resolver there is currently no way other than to force the Cgo resolver.

There are various ways to force Go to use the Cgo resolver. The way to force Go to use the Cgo resolver suggested by the documentation is to set the environment variable GODEBUG to netdns=cgo.

Dianetics answered 18/7, 2017 at 19:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.