Skip to content

Commit

Permalink
rework checkAndPickPort
Browse files Browse the repository at this point in the history
  • Loading branch information
alexballas committed Dec 30, 2023
1 parent 19cfc57 commit 14a6f9f
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions soapcalls/utils/iptools.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,25 @@ func URLtoListenIPandPort(u string) (string, error) {
}

func checkAndPickPort(ip string, port int) (string, error) {
var numberOfchecks int
CHECK:
numberOfchecks++
conn, err := net.Listen("tcp", net.JoinHostPort(ip, strconv.Itoa(port)))
if err != nil {
if errors.Is(err, syscall.EADDRINUSE) {
if numberOfchecks == 1000 {
return "", fmt.Errorf("port pick error. Checked 1000 ports: %w", err)
const maxAttempts = 1000
for attempt := 1; attempt <= maxAttempts; attempt++ {
conn, err := net.Listen("tcp", net.JoinHostPort(ip, strconv.Itoa(port)))
if err != nil {
if errors.Is(err, syscall.EADDRINUSE) {
if attempt == maxAttempts {
break
}
port++
continue
}
port++
goto CHECK
}

return "", fmt.Errorf("port pick error: %w", err)
return "", fmt.Errorf("port pick error: %w", err)
}
conn.Close()
return strconv.Itoa(port), nil
}
conn.Close()
return strconv.Itoa(port), nil

return "", fmt.Errorf("port pick error. Exceeded maximum attempts")
}

// HostPortIsAlive - We use this function to periodically
Expand Down

0 comments on commit 14a6f9f

Please sign in to comment.