Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error handling for getHostByName function #319

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 21 additions & 4 deletions network.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,27 @@ package sprig
import (
"math/rand"
"net"
"fmt"
"time"
)

func getHostByName(name string) string {
addrs, _ := net.LookupHost(name)
//TODO: add error handing when release v3 comes out
return addrs[rand.Intn(len(addrs))]
// Error check loop runs 3 times with 15 second intervals only if DNS/connection type errors,
// if host does not exist "" blank gets returned
func getHostByName(name string) (string, error) {
err := error(nil)
for tries := 3; tries > 0; tries-- {
addrs, err := net.LookupHost(name)
if err == nil {
return addrs[rand.Intn(len(addrs))], nil
}
dnsErr, ok := err.(*net.DNSError)
if !ok {
return "", fmt.Errorf("DNS Error, %v", err)
}
if dnsErr.IsNotFound {
return "", nil
}
time.Sleep(15 * time.Second)
}
return "", fmt.Errorf("Failure in looking up %s: ", err)
}