-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmain.go
221 lines (183 loc) · 5.96 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
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
package main
import (
"context"
"errors"
"fmt"
"log"
"log/slog"
"math/rand"
"net/http"
"os"
"os/signal"
"strconv"
"syscall"
"time"
"github.com/tmaxmax/go-sse"
)
const (
topicRandomNumbers = "numbers"
topicMetrics = "metrics"
)
func newSSE() *sse.Server {
rp, _ := sse.NewValidReplayer(time.Minute*5, true)
rp.GCInterval = time.Minute
return &sse.Server{
Provider: &sse.Joe{Replayer: rp},
// If you are using a 3rd party library to generate a per-request logger, this
// can just be a simple wrapper over it.
Logger: func(r *http.Request) *slog.Logger {
return getLogger(r.Context())
},
OnSession: func(w http.ResponseWriter, r *http.Request) (topics []string, permitted bool) {
topics = r.URL.Query()["topic"]
for _, topic := range topics {
if topic != topicRandomNumbers && topic != topicMetrics {
fmt.Fprintf(w, "invalid topic %q; supported are %q, %q", topic, topicRandomNumbers, topicMetrics)
// NOTE: if you are returning false to reject the subscription, we strongly recommend writing
// your own response code. Clients will receive a 200 code otherwise, which may be confusing.
w.WriteHeader(http.StatusBadRequest)
return nil, false
}
}
if len(topics) == 0 {
// Provide default topics, if none are given.
topics = []string{topicRandomNumbers, topicMetrics}
}
// the shutdown message is sent on the default topic
return append(topics, sse.DefaultTopic), true
},
}
}
func cors(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
h.ServeHTTP(w, r)
})
}
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer cancel()
handler := slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelDebug})
logger := slog.New(handler)
logMiddleware := withLogger(logger)
sseHandler := newSSE()
mux := http.NewServeMux()
mux.HandleFunc("/stop", func(w http.ResponseWriter, _ *http.Request) {
cancel()
w.WriteHeader(http.StatusOK)
})
mux.Handle("/", SnapshotHTTPEndpoint)
mux.Handle("/events", sseHandler)
httpLogger := slog.NewLogLogger(handler, slog.LevelWarn)
s := &http.Server{
Addr: "0.0.0.0:8080",
Handler: cors(logMiddleware(mux)),
ReadHeaderTimeout: time.Second * 10,
ErrorLog: httpLogger,
}
s.RegisterOnShutdown(func() {
e := &sse.Message{Type: sse.Type("close")}
// Adding data is necessary because spec-compliant clients
// do not dispatch events without data.
e.AppendData("bye")
// Broadcast a close message so clients can gracefully disconnect.
_ = sseHandler.Publish(e)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
// We use a context with a timeout so the program doesn't wait indefinitely
// for connections to terminate. There may be misbehaving connections
// which may hang for an unknown timespan, so we just stop waiting on Shutdown
// after a certain duration.
_ = sseHandler.Shutdown(ctx)
})
go recordMetric(ctx, sseHandler, "ops", time.Second*2)
go recordMetric(ctx, sseHandler, "cycles", time.Millisecond*500)
go func() {
duration := func() time.Duration {
return time.Duration(2000+rand.Intn(1000)) * time.Millisecond
}
timer := time.NewTimer(duration())
defer timer.Stop()
for {
select {
case <-timer.C:
_ = sseHandler.Publish(generateRandomNumbers(), topicRandomNumbers)
case <-ctx.Done():
return
}
timer.Reset(duration())
}
}()
if err := runServer(ctx, s); err != nil {
log.Println("server closed", err)
}
}
func recordMetric(ctx context.Context, sseHandler *sse.Server, metric string, frequency time.Duration) {
ticker := time.NewTicker(frequency)
defer ticker.Stop()
for {
select {
case <-ticker.C:
v := Inc(metric)
e := &sse.Message{
Type: sse.Type(metric),
}
e.AppendData(strconv.FormatInt(v, 10))
_ = sseHandler.Publish(e, topicMetrics)
case <-ctx.Done():
return
}
}
}
func runServer(ctx context.Context, s *http.Server) error {
shutdownError := make(chan error)
go func() {
<-ctx.Done()
sctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
shutdownError <- s.Shutdown(sctx)
}()
if err := s.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
return err
}
return <-shutdownError
}
func generateRandomNumbers() *sse.Message {
e := &sse.Message{}
count := 1 + rand.Intn(5)
for i := 0; i < count; i++ {
e.AppendData(strconv.FormatUint(rand.Uint64(), 10))
}
return e
}
type loggerCtxKey struct{}
// withLogger is a net/http compatable middleware that generates a logger with request-specific fields
// added to it and attaches it to the request context for later retrieval with getLogger().
// Third party logging packages may offer similar middlewares to add a logger to the request or maybe
// just a helper to add a logger to context; in the second case you can build your own middleware
// function around it, similar to this one.
func withLogger(logger *slog.Logger) func(h http.Handler) http.Handler {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
l := logger.With(
"UserAgent", r.UserAgent(),
"RemoteAddr", r.RemoteAddr,
"Host", r.Host,
"Origin", r.Header.Get("origin"),
)
r = r.WithContext(context.WithValue(r.Context(), loggerCtxKey{}, l))
h.ServeHTTP(w, r)
})
}
}
// getLogger retrieves the request-specific logger from a request's context. This is
// similar to how existing per-request http logging libraries work, just very simplified.
func getLogger(ctx context.Context) *slog.Logger {
logger, ok := ctx.Value(loggerCtxKey{}).(*slog.Logger)
if !ok {
// We are accepting an arbitrary context object, so it's better to explicitly return
// nil here since the exact behavior of getting the value of an undefined key is undefined
return nil
}
return logger
}