Skip to content

Commit

Permalink
fix: getHostByName
Browse files Browse the repository at this point in the history
- added error handling to avoid panics
  • Loading branch information
lib0xidium committed Oct 9, 2024
1 parent fc7fc0d commit e7c443f
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions network.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ import (
"net"
)

func getHostByName(name string) string {
addrs, _ := net.LookupHost(name)
//TODO: add error handing when release v3 comes out
return addrs[rand.Intn(len(addrs))]
const NUM_TRIES = 3

func getHostByName(name string) (string, error) {
err := error(nil)
addrs := []string{}
for tries := NUM_TRIES; tries > 0; tries-- {
if addrs, err = net.LookupHost(name); err == nil {
return addrs[rand.Intn(len(addrs))], nil
}
}
return "", err
}

0 comments on commit e7c443f

Please sign in to comment.