-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
75 lines (65 loc) · 1.34 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"flag"
"fmt"
"net"
"os"
"strings"
"sync"
"time"
)
type servicesType []string
var timeout int
var services servicesType
func (s *servicesType) String() string {
return fmt.Sprintf("%+v", *s)
}
func (s *servicesType) Set(value string) error {
*s = strings.Split(value, ",")
return nil
}
// waitForServices tests and waits on the availability of a TCP host and port
func waitForServices(services []string, timeOut time.Duration) error {
var depChan = make(chan struct{})
var wg sync.WaitGroup
wg.Add(len(services))
go func() {
for _, s := range services {
go func(s string) {
defer wg.Done()
for {
_, err := net.Dial("tcp", s)
if err == nil {
return
}
time.Sleep(1 * time.Second)
}
}(s)
}
wg.Wait()
close(depChan)
}()
select {
case <-depChan: // services are ready
return nil
case <-time.After(timeOut):
return fmt.Errorf("services aren't ready in %s", timeOut)
}
}
func init() {
flag.IntVar(&timeout, "t", 20, "timeout")
flag.Var(&services, "it", "<host:port> [host2:port,...] comma seperated list of services")
}
func main() {
flag.Parse()
if len(services) == 0 {
flag.Usage()
os.Exit(1)
}
if err := waitForServices(services, time.Duration(timeout)*time.Second); err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("services are ready!")
os.Exit(0)
}