Hi,

You don't *need* to wait for the goroutines to exit, per se, but, as far as 
I can tell, your code will panic when it "return"s and one of the 
still-running resolver goroutines try to send to the closed channel.

One solution would be *not to close the channel upon return*. The channel 
will be collected when the last goroutine exits. You must, though, make 
sure that the resolver goroutines will *not block* sending on recvChann. 
Use something like this:
 
  select {
  case: recvChan <- r
  default:
  }


This would work, but it feels a bit dirty: you leave goroutines running 
while you no longer need them. It would be better if you could make your 
resolver goroutines cancel-able:


 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
  

Then you must make your resolvers quit when quitChannel is closed; 
depending on what your resolvers do, it may be complicated. It is arguable 
whether this extra complication is worth the gain.

(you could also wait for the resolvers to stop after canceling them, but I 
see no practical advantage in doing so...)


-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to