recvChan := make(chan *dnsRecord, 1)
defer close(recvChan)
for _, server := range DnsServers {
go doResolve(server, req, recvChan) // query ip address
}
select {
case r := <-recvChan: // receive address
responseRecord(w, req, r) // response to client
case <-time.After(2 * time.Second): // timeout
break
}
return
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
select {
case: recvChan <- r
default:
}
recvChan := make(chan *dnsRecord, 1)
quitChan := make(chan struct{})
defer close(quitChan)
for _, server := range DnsServers {
go doResolve(server, req, recvChan, quitChan) // query ip address
}
select {
case r := <-recvChan: // receive address
responseRecord(w, req, r) // response to client
case <-time.After(2 * time.Second): // timeout
}
// chanQuit will be closed
return
I suggest using errgroup (and/or context.Context in general) instead of rolling your own cancellation and timeout logic. It should make it pretty much trivial to kick off all the queries and cancel the context once you got the first result. It will also allow you to propagate that cancellation seamlessly through the ecosystem (e.g. cancellation traverses network boundaries when used with grpc and it will kill child processes if they are spawned with a context). The errgroup documentation has specific examples for how to do this kind of thing.