Skip to content

Commit

Permalink
add tcpx.WaitHosts
Browse files Browse the repository at this point in the history
  • Loading branch information
UlricQin committed Oct 15, 2023
1 parent 382ac87 commit 6c264e7
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions net/tcpx/tcpx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package tcpx

import (
"fmt"
"net"
"os"
"strings"
"time"
)

func WaitHosts() {
hosts := os.Getenv("WAIT_HOSTS")
if len(hosts) == 0 {
return
}

hosts = strings.ReplaceAll(hosts, ",", " ")
array := strings.Fields(hosts)

for _, host := range array {
waitHost(host)
}
}

func waitHost(host string) {
for {
fmt.Printf("[%s] Waiting for host: %s\n", time.Now(), host)

conn, err := net.DialTimeout("tcp", host, time.Second)
if err == nil {
conn.Close()
fmt.Printf("[%s] Host is ready: %s\n", time.Now(), host)
return
}

time.Sleep(time.Second)
}
}

0 comments on commit 6c264e7

Please # to comment.