-
Notifications
You must be signed in to change notification settings - Fork 12
/
webserver.go
266 lines (234 loc) · 8.5 KB
/
webserver.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Copyright 2022 The searKing Author. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package webserver
import (
"context"
"errors"
"fmt"
"log/slog"
"net"
"net/http"
"sync"
"time"
"github.com/gin-gonic/gin"
slog_ "github.com/searKing/golang/go/log/slog"
runtime_ "github.com/searKing/golang/go/runtime"
"github.com/searKing/golang/pkg/webserver/healthz"
"github.com/searKing/golang/third_party/github.com/grpc-ecosystem/grpc-gateway-v2/grpc"
)
type WebHandler interface {
SetRoutes(ginRouter gin.IRouter, grpcRouter *grpc.Gateway)
}
type WebServer struct {
Name string
// BindAddress is the host name to use for bind (local internet) facing URLs (e.g. Loopback)
// Will default to a value based on secure serving info and available ipv4 IPs.
BindAddress string
// ExternalAddress is the host name to use for external (public internet) facing URLs (e.g. Swagger)
// Will default to a value based on secure serving info and available ipv4 IPs.
ExternalAddress string
PreferRegisterHTTPFromEndpoint bool // prefer register http handler from endpoint
ginBackend *gin.Engine
grpcBackend *grpc.Gateway
// PostStartHooks are each called after the server has started listening, in a separate go func for each
// with no guarantee of ordering between them. The map key is a name used for error reporting.
// It may kill the process with a panic if it wishes to by returning an error.
postStartHookLock sync.Mutex
postStartHooks map[string]postStartHookEntry
postStartHookOrderedKeys []string // ordered keys..., other keys in random order
postStartHooksCalled bool
preShutdownHookLock sync.Mutex
preShutdownHooks map[string]preShutdownHookEntry
preShutdownHookOrderedKeys []string // other keys in random order, ordered keys in reverse order
preShutdownHooksCalled bool
// healthz checks
healthzLock sync.Mutex
healthzChecks []healthz.HealthChecker
healthzChecksInstalled bool
// livez checks
livezLock sync.Mutex
livezChecks []healthz.HealthChecker
livezChecksInstalled bool
// readyz checks
readyzLock sync.Mutex
readyzChecks []healthz.HealthChecker
readyzChecksInstalled bool
livezGracePeriod time.Duration
// the readiness stop channel is used to signal that the apiserver has initiated a shutdown sequence, this
// will cause readyz to return unhealthy.
readinessStopCh chan struct{}
// ShutdownDelayDuration allows to block shutdown for some time, e.g. until endpoints pointing to this API server
// have converged on all node. During this time, the API server keeps serving, /healthz will return 200,
// but /readyz will return failure.
ShutdownDelayDuration time.Duration
// ShutdownTimeout is the timeout used for server shutdown. This specifies the timeout before server
// gracefully shutdown returns.
ShutdownTimeout time.Duration
}
func NewWebServer(fc FactoryConfig, configs ...FactoryConfigFunc) (*WebServer, error) {
f, err := NewFactory(fc, configs...)
if err != nil {
return nil, err
}
return f.New()
}
// preparedWebServer is a private wrapper that enforces a call of PrepareRun() before Run can be invoked.
type preparedWebServer struct {
*WebServer
}
// PrepareRun does post API installation setup steps. It calls recursively the same function of the delegates.
func (s *WebServer) PrepareRun() (preparedWebServer, error) {
if s.grpcBackend != nil {
s.grpcBackend.Handler = s.ginBackend
}
s.installHealthz()
s.installLivez()
err := s.addReadyzShutdownCheck(s.readinessStopCh)
if err != nil {
slog.With(slog_.Error(err)).Error("Failed to add readyz shutdown check")
return preparedWebServer{}, err
}
s.installReadyz()
// Register audit backend preShutdownHook.
return preparedWebServer{s}, nil
}
// Run spawns the secure http server. It only returns if stopCh is closed
// or the secure port cannot be listened on initially.
func (s preparedWebServer) Run(ctx context.Context) error {
slog.InfoContext(ctx, fmt.Sprintf("Serving securely on %s", s.grpcBackend.BindAddr()))
ctx, cancel := context.WithCancel(ctx)
defer cancel()
stoppedHttpServerCtx, stopHttpServer := context.WithCancel(context.Background())
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
defer stopHttpServer()
defer slog.Info("[graceful-termination] shutdown executed")
<-ctx.Done()
// As soon as shutdown is initiated, /readyz should start returning failure.
// This gives the load balancer a window defined by ShutdownDelayDuration to detect that /readyz is red
// and stop sending traffic to this server.
close(s.readinessStopCh)
slog.InfoContext(ctx, fmt.Sprintf("[graceful-termination] shutdown is initiated and delayed after %d", s.ShutdownDelayDuration))
time.Sleep(s.ShutdownDelayDuration)
}()
// close socket after delayed stopCh
stopHttpServerCtx, stoppedHttpServerCtx, err := s.NonBlockingRun(stoppedHttpServerCtx)
if err != nil {
cancel()
return err
}
slog.Info("[graceful-termination] waiting for shutdown to be initiated")
// wait for stoppedCh that is closed when the graceful termination (server.Shutdown) is finished.
<-stopHttpServerCtx.Done()
// run shutdown hooks directly. This includes deregistering from the kubernetes endpoint in case of web server.
func() {
defer cancel()
defer func() {
slog.Info("[graceful-termination] pre-shutdown hooks completed", slog.String("name", s.Name))
}()
err = s.RunPreShutdownHooks()
}()
if err != nil {
return err
}
// wait for the delayed stopCh before closing the handler chain (it rejects everything after Wait has been called).
slog.Info("[graceful-termination] waiting for http server to be stopped")
<-stoppedHttpServerCtx.Done()
slog.Info("[graceful-termination] waiting for http server to be shutdown executed")
wg.Wait()
slog.Info("[graceful-termination] webserver is exiting")
return nil
}
// NonBlockingRun spawns the secure http|grpc server. An error is
// returned if the secure port cannot be listened on.
// The returned context is done when the (asynchronous) termination is finished.
func (s preparedWebServer) NonBlockingRun(ctx context.Context) (stopCtx, stoppedCtx context.Context, err error) {
// Shutdown server gracefully.
stopCtx, stop := context.WithCancel(ctx)
stoppedCtx, stopped := context.WithCancel(context.Background())
// Start the shutdown daemon before any request comes in.
go func() {
defer stopped()
select {
case <-stopCtx.Done():
}
// Now that listener have bound successfully, it is the
// responsibility of the caller to close the provided channel to
// ensure cleanup.
if s.ShutdownTimeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(context.Background(), s.ShutdownTimeout)
defer cancel()
}
err := s.grpcBackend.Shutdown(ctx)
msg := fmt.Sprintf("Have shutdown http server on %s", s.grpcBackend.BindAddr())
if err != nil {
slog.With(slog_.Error(err)).Error(msg)
return
}
slog.Info(msg)
}()
// await when [Accept] will be called immediately inside [Serve].
startedCtx, started := context.WithCancel(context.Background())
// Start the post start hooks daemon before any request comes in.
go func() {
defer runtime_.LogPanic.Recover()
select {
case <-stoppedCtx.Done(): // exit early
return
case <-startedCtx.Done(): // wait for start
slog.Info(fmt.Sprintf("Startted listening on %s", s.grpcBackend.BindAddr()))
}
var err error
defer func() {
if err != nil {
stop()
}
}()
err = s.RunPostStartHooks(stopCtx)
msg := fmt.Sprintf("RunPostStartHooks on %s", s.grpcBackend.BindAddr())
if err == nil {
slog.Info(msg)
return
}
slog.With(slog_.Error(err)).Error(msg)
}()
go func() {
defer runtime_.LogPanic.Recover()
defer stop()
baseCtx := s.grpcBackend.BaseContext
s.grpcBackend.BaseContext = func(lis net.Listener) context.Context {
defer started()
if baseCtx == nil {
return context.Background()
}
return baseCtx(lis)
}
var err error
err = s.grpcBackend.ListenAndServe()
msg := fmt.Sprintf("Stopped listening on %s", s.grpcBackend.BindAddr())
if err == nil || errors.Is(err, http.ErrServerClosed) {
slog.Info(msg)
return
}
select {
case <-stoppedCtx.Done():
slog.Info(msg)
default: // not caused by Shutdown
slog.With(slog_.Error(err)).Error(msg)
}
return
}()
return stopCtx, stoppedCtx, nil
}
func (s *WebServer) InstallWebHandlers(handlers ...WebHandler) {
for _, h := range handlers {
if h == nil {
continue
}
h.SetRoutes(s.ginBackend, s.grpcBackend)
}
}