-
Notifications
You must be signed in to change notification settings - Fork 329
/
waitfor.go
181 lines (152 loc) · 3.71 KB
/
waitfor.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package server
import (
"context"
"fmt"
"net"
"net/http"
"net/url"
"os"
"strings"
"sync"
"time"
"github.com/go-chi/chi/v5"
"go.uber.org/zap"
)
// WaitFor sets up a simple status page, delays execution and probes services
func (s server) WaitFor(ctx context.Context) {
var (
opt = s.opts.WaitFor
services = opt.GetServices()
)
if len(services) == 0 && opt.Delay == 0 {
// Nothing to do here..
return
}
var (
log = s.log.Named("wait-for")
depChan = make(chan struct{})
wg sync.WaitGroup
serviceAddr string
serviceURL *url.URL
err error
)
// Setup a simple HTTP server that will inform the impatient users
listener, err := net.Listen("tcp", s.opts.HTTPServer.Addr)
if err != nil {
s.log.Error("cannot start server", zap.Error(err))
os.Exit(1)
}
defer listener.Close()
go func() {
router := chi.NewRouter()
router.Get("/*", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusPreconditionFailed)
w.Write([]byte("waiting for services..."))
})
_ = http.Serve(listener, router)
}()
if opt.Delay > 0 {
s.log.Info("delaying", zap.Duration("delay", opt.Delay))
// First delay execution
select {
case <-ctx.Done():
log.Debug("canceled")
return
case <-time.After(opt.Delay):
// all good...
}
}
if len(services) == 0 {
return
}
log.Info("waiting for services", zap.Strings("services", services))
// Probe services
wg.Add(len(services))
go func() {
for _, service := range services {
slog := log.With(zap.String("service", service))
go func(ctx context.Context, service string) {
defer wg.Done()
if serviceAddr, serviceURL, err = s.resolveService(service); err != nil {
log.Error("could not resolve service", zap.Error(err))
}
for {
ctx, cancelFn := context.WithTimeout(ctx, opt.ServicesProbeTimeout)
defer cancelFn()
if serviceURL == nil {
if err = s.probeService(ctx, serviceAddr); err != nil {
slog.Warn("service probe failed", zap.Error(err))
time.Sleep(opt.ServicesProbeInterval)
continue
}
} else {
if err = s.probeServiceURL(ctx, serviceURL); err != nil {
slog.Warn("service URL probe failed", zap.Error(err))
time.Sleep(opt.ServicesProbeInterval)
continue
}
}
slog.Debug("service ready")
return
}
}(ctx, service)
}
wg.Wait()
close(depChan)
}()
select {
case <-ctx.Done():
log.Debug("canceled")
return
case <-depChan: // services are ready
log.Debug("all services ready")
return
case <-time.After(opt.ServicesTimeout):
log.Debug("services not ready")
os.Exit(1)
}
}
func (s server) resolveService(service string) (addr string, u *url.URL, err error) {
addr = service
if strings.Contains(addr, "://") {
// Is service an URL?
u, err = url.Parse(addr)
if err != nil {
return
}
addr = u.Host
if u.Port() == "" {
if u.Scheme == "https" {
addr += ":443"
}
}
}
// Default to port 80
if !strings.Contains(addr, ":") {
addr += ":80"
}
return
}
func (s server) probeService(ctx context.Context, addr string) (err error) {
if err != nil {
return err
}
dialer := net.Dialer{}
_, err = dialer.DialContext(ctx, "tcp", addr)
return
}
func (s server) probeServiceURL(ctx context.Context, u *url.URL) error {
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return fmt.Errorf("failed to assemble service request: %w", err)
}
rsp, err := http.DefaultClient.Do(req.WithContext(ctx))
if err != nil {
return fmt.Errorf("service URL request failed: %w", err)
}
defer rsp.Body.Close()
if rsp.StatusCode == http.StatusOK {
return nil
}
return fmt.Errorf("service responded with unexpected status '%s'", rsp.Status)
}