forked from tmaxmax/go-sse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
174 lines (144 loc) · 3.97 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
package main
import (
"context"
"errors"
"fmt"
"log"
"math/rand"
"net/http"
"os"
"os/signal"
"strconv"
"syscall"
"time"
"github.com/tmaxmax/go-sse"
)
const (
topicRandomNumbers = "numbers"
topicMetrics = "metrics"
)
var sseHandler = &sse.Server{
Provider: &sse.Joe{
ReplayProvider: &sse.ValidReplayProvider{
TTL: time.Minute * 5,
GCInterval: time.Minute,
AutoIDs: true,
},
},
Logger: logger{log.New(os.Stderr, "", 0)},
OnSession: func(s *sse.Session) (sse.Subscription, bool) {
topics := s.Req.URL.Query()["topic"]
for _, topic := range topics {
if topic != topicRandomNumbers && topic != topicMetrics {
fmt.Fprintf(s.Res, "invalid topic %q; supported are %q, %q", topic, topicRandomNumbers, topicMetrics)
s.Res.WriteHeader(http.StatusBadRequest)
return sse.Subscription{}, false
}
}
if len(topics) == 0 {
// Provide default topics, if none are given.
topics = []string{topicRandomNumbers, topicMetrics}
}
return sse.Subscription{
Client: s,
LastEventID: s.LastEventID,
Topics: append(topics, sse.DefaultTopic), // the shutdown message is sent on the default topic
}, 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()
mux := http.NewServeMux()
mux.HandleFunc("/stop", func(w http.ResponseWriter, _ *http.Request) {
cancel()
w.WriteHeader(http.StatusOK)
})
mux.Handle("/", SnapshotHTTPEndpoint)
mux.Handle("/events", sseHandler)
s := &http.Server{
Addr: "0.0.0.0:8080",
Handler: cors(withLogger(mux)),
ReadHeaderTimeout: time.Second * 10,
}
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, "ops", time.Second*2)
go recordMetric(ctx, "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, 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
}