-
Notifications
You must be signed in to change notification settings - Fork 0
/
liveness_gateway.go
52 lines (45 loc) · 1.22 KB
/
liveness_gateway.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
package fxgrpc
import (
"context"
"net"
"net/http"
"time"
"go.uber.org/fx"
"go.uber.org/zap"
)
type LivenessGatewayConfig struct {
Host string `default:"0.0.0.0"`
Port string `default:"8086"`
}
type LivenessGatewayHandler struct {
Handler http.Handler
}
func RunLivenessGateway(lc fx.Lifecycle, s fx.Shutdowner, logger *zap.Logger, config *Config, h *LivenessGatewayHandler) error {
addr := net.JoinHostPort(config.LivenessGateway.Host, config.LivenessGateway.Port)
var httpServer *http.Server
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
go func() {
logger.Info("starting liveness gateway...", zap.String("addr", addr))
httpServer = &http.Server{Addr: addr, Handler: h.Handler}
err := httpServer.ListenAndServe()
if err != nil {
logger.Error("Failed to run liveness gateway", zap.Error(err))
_ = s.Shutdown()
}
}()
return nil
},
OnStop: func(ctx context.Context) error {
if httpServer != nil {
timeout, can := context.WithTimeout(context.Background(), 10*time.Second)
defer can()
if err := httpServer.Shutdown(timeout); err != nil {
logger.Error("Stopped http server after grpc failure", zap.Error(err))
}
}
return nil
},
})
return nil
}