Open xeoncross opened 5 years ago
Instead of using the default resolver to lookup the MX record. I recommend using a custom resolver with a context timeout to prevent long-lived connections.
https://github.com/badoux/checkmail/blob/master/checkmail.go#L49
const timeout = 10 * time.Millisecond ctx, cancel := context.WithTimeout(context.TODO(), timeout) defer cancel() // important to avoid a resource leak var r net.Resolver names, err := r.LookupMX(ctx, "127.0.0.1") if err == nil && len(names) > 0 { fmt.Println(names[0]) // "localhost" }
https://play.golang.org/p/HTW6-2o0qeT
You might also want to look at caching DNS queries.
What if the lookup function took the context as an argument so users of the library could set their own timeout duration?
Instead of using the default resolver to lookup the MX record. I recommend using a custom resolver with a context timeout to prevent long-lived connections.
https://github.com/badoux/checkmail/blob/master/checkmail.go#L49
https://play.golang.org/p/HTW6-2o0qeT