-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
144 lines (121 loc) · 3.22 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
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
package main
import (
"context"
"fmt"
"github.com/gorilla/mux"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
addr := "localhost:8080"
if len(os.Args) > 1 {
addr = os.Args[1]
}
logger := setupLogging()
defer logger.Sync()
server := newServer(ctx, logger, addr)
runningCtx, runningCancel := context.WithCancel(ctx)
defer runningCancel()
go func() {
logger.Info("starting server", zap.String("addr", addr))
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.Error("starting failed", zap.Error(err))
runningCancel() // initiate shutdown sequence
}
}()
<-runningCtx.Done()
logger.Info("received termination signal, shutting down")
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), time.Minute)
defer shutdownCancel()
if err := server.Shutdown(shutdownCtx); err != nil {
logger.Warn("failed to shutdown server", zap.Error(err))
}
logger.Info("server shutdown complete")
}
type Server struct {
ctx context.Context
logger *zap.Logger
}
func newServer(ctx context.Context, logger *zap.Logger, addr string) *http.Server {
srv := Server{ctx: ctx, logger: logger}
return &http.Server{
Addr: addr,
Handler: srv.handler(),
}
}
func (s *Server) handler() http.Handler {
r := mux.NewRouter()
r.HandleFunc("/slow/{duration}", s.slow)
r.HandleFunc("/fail", s.fail)
return r
}
func (s *Server) fail(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusGatewayTimeout)
}
func (s *Server) slow(rw http.ResponseWriter, req *http.Request) {
logger := s.logger.With(
zap.String("method", req.Method),
zap.String("url", req.URL.String()),
)
logger.With(zap.Any("header", req.Header)).Info("incoming request headers")
duration := mux.Vars(req)["duration"]
if duration == "" {
logger.Info("using default duration")
duration = "10s"
}
pause, err := time.ParseDuration(duration)
if err != nil {
logger.With(zap.Error(err)).Error("failed to parse duration")
rw.WriteHeader(http.StatusBadRequest)
}
logger.Info("starting request")
logger.Sugar().Infof("pausing for %s", pause)
timer := time.NewTimer(pause)
ticker := time.NewTicker(time.Second)
defer logger.Info("finishing request")
for {
select {
case <-req.Context().Done():
logger.Info("request context cancelled")
return
case <-s.ctx.Done():
return
case <-timer.C:
return
case tick := <-ticker.C:
logger.Info("tick")
_, err := rw.Write([]byte(fmt.Sprintf("tick: %s\n", tick)))
if err != nil {
logger.With(zap.Error(err)).Error("failed to write tick")
return
}
if f, ok := rw.(http.Flusher); ok {
logger.Info("flush")
f.Flush()
}
}
}
}
func setupLogging() *zap.Logger {
conf := zap.Config{
Level: zap.NewAtomicLevelAt(zapcore.InfoLevel),
Development: false,
Encoding: "json",
EncoderConfig: zap.NewProductionEncoderConfig(),
DisableStacktrace: true,
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
}
logger, err := conf.Build()
if err != nil {
panic(err)
}
return logger
}