badoux / checkmail

Golang package for email validation
MIT License
710 stars 92 forks source link

Set a MX timeout #25

Open xeoncross opened 5 years ago

xeoncross commented 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

xeoncross commented 5 years ago

You might also want to look at caching DNS queries.

DCtheTall commented 5 years ago

What if the lookup function took the context as an argument so users of the library could set their own timeout duration?