I use this function to limit response time from DNS server
func LookupHost(hostname string, timeout time.Duration) ([]string, error) {
c1 := make(chan []string)
c2 := make(chan error)
go func() {
var ipaddr []string
ipaddr, err := net.LookupHost(hostname)
if err != nil {
c2 <- err
close(c2)
}
c1 <- ipaddr
close(c1)
}()
select {
case ipaddr := <-c1:
return ipaddr, nil
case err := <-c2:
return []string{}, err
case <-time.After(timeout):
return []string{}, errors.New("timeout")
}
}
The problem is that this function eat memory.
I think it is because I break net.LookupHost(hostname)
syscall.
Any way to avoid this?
May be some other way how to query DNS servers with timeouts?