/
ooo.go
375 lines (335 loc) · 9.67 KB
/
ooo.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package ooo
import (
"context"
"log"
"net"
"net/http"
"os"
"os/signal"
"sync"
"sync/atomic"
"syscall"
"time"
"github.com/benitogf/coat"
"github.com/benitogf/ooo/meta"
"github.com/benitogf/ooo/stream"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/rs/cors"
)
const deadlineMsg = "ooo: server deadline reached"
// audit requests function
// will define approval or denial by the return value
// r: the request to be audited
// returns
// true: approve the request
// false: deny the request
type audit func(r *http.Request) bool
// Server application
//
// Router: can be predefined with routes and passed to be extended
//
// NoBroadcastKeys: array of keys that should not broadcast on changes
//
// DbOpt: options for storage
//
// Audit: function to audit requests
//
// Workers: number of workers to use as readers of the storage->broadcast channel
//
// ForcePatch: flag to force patch operations even if the patch is bigger than the snapshot
//
// OnSubscribe: function to monitor subscribe events
//
// OnUnsubscribe: function to monitor unsubscribe events
//
// OnClose: function that triggers before closing the application
//
// Deadline: time duration of a request before timing out
//
// AllowedOrigins: list of allowed origins for cross domain access, defaults to ["*"]
//
// AllowedMethods: list of allowed methods for cross domain access, defaults to ["GET", "POST", "DELETE", "PUT"]
//
// AllowedHeaders: list of allowed headers for cross domain access, defaults to ["Authorization", "Content-Type"]
//
// ExposedHeaders: list of exposed headers for cross domain access, defaults to nil
//
// Storage: database interdace implementation
//
// Silence: output silence flag
//
// Static: static routing flag
//
// Tick: time interval between ticks on the clock subscription
//
// Signal: os signal channel
//
// Client: http client to make requests
type Server struct {
wg sync.WaitGroup
server *http.Server
Router *mux.Router
Stream stream.Stream
filters filters
Pivot string
NoBroadcastKeys []string
DbOpt interface{}
Audit audit
Workers int
ForcePatch bool
NoPatch bool
OnSubscribe stream.Subscribe
OnUnsubscribe stream.Unsubscribe
OnClose func()
Deadline time.Duration
AllowedOrigins []string
AllowedMethods []string
AllowedHeaders []string
ExposedHeaders []string
Storage Database
Address string
closing int64
active int64
Silence bool
Static bool
Tick time.Duration
Console *coat.Console
Signal chan os.Signal
Client *http.Client
ReadTimeout time.Duration
WriteTimeout time.Duration
ReadHeaderTimeout time.Duration
IdleTimeout time.Duration
}
// tcpKeepAliveListener sets TCP keep-alive timeouts on accepted
// connections. It's used by ListenAndServe and ListenAndServeTLS so
// dead TCP connections (e.g. closing laptop mid-download) eventually
// go away.
type tcpKeepAliveListener struct {
*net.TCPListener
}
func (app *Server) waitListen() {
var err error
err = app.Storage.Start(StorageOpt{
NoBroadcastKeys: app.NoBroadcastKeys,
DbOpt: app.DbOpt,
})
if err != nil {
log.Fatal(err)
}
app.server = &http.Server{
WriteTimeout: app.WriteTimeout,
ReadTimeout: app.ReadTimeout,
ReadHeaderTimeout: app.ReadHeaderTimeout,
IdleTimeout: app.IdleTimeout,
Addr: app.Address,
Handler: cors.New(cors.Options{
AllowedMethods: app.AllowedMethods,
AllowedOrigins: app.AllowedOrigins,
AllowedHeaders: app.AllowedHeaders,
ExposedHeaders: app.ExposedHeaders,
// AllowCredentials: true,
// Debug: true,
}).Handler(handlers.CompressHandler(app.Router))}
ln, err := net.Listen("tcp4", app.Address)
if err != nil {
log.Fatal("failed to start tcp, ", err)
}
app.Address = ln.Addr().String()
atomic.StoreInt64(&app.active, 1)
app.wg.Done()
err = app.server.Serve(tcpKeepAliveListener{ln.(*net.TCPListener)})
if atomic.LoadInt64(&app.closing) != 1 {
log.Fatal(err)
}
}
// Active check if the server is active
func (app *Server) Active() bool {
return atomic.LoadInt64(&app.active) == 1 && atomic.LoadInt64(&app.closing) == 0
}
func (app *Server) waitStart() {
if atomic.LoadInt64(&app.active) == 0 || !app.Storage.Active() {
log.Fatal("server start failed")
}
for i := 0; i < app.Workers; i++ {
go app.watch(app.Storage.Watch())
}
app.Console.Log("glad to serve[" + app.Address + "]")
}
// Fetch data, update cache and apply filter
func (app *Server) fetch(key string) (stream.Cache, error) {
err := app.filters.Read.checkStatic(key, app.Static)
if err != nil {
return stream.Cache{}, err
}
return app.Stream.Refresh(key, app.getFilteredData), nil
}
// getFilteredData
func (app *Server) getFilteredData(key string) ([]byte, error) {
raw, _ := app.Storage.Get(key)
if len(raw) == 0 {
raw = meta.EmptyObject
}
filteredData, err := app.filters.Read.check(key, raw, app.Static)
if err != nil {
return []byte(""), err
}
return filteredData, nil
}
func (app *Server) watch(sc StorageChan) {
broadcastOpt := stream.BroadcastOpt{
Get: app.getFilteredData,
Callback: nil,
}
for {
ev := <-sc
if ev.Key != "" {
app.Console.Log("broadcast[" + ev.Key + "]")
app.Stream.Broadcast(ev.Key, broadcastOpt)
}
if !app.Storage.Active() {
break
}
}
}
// defaults will populate the server fields with their zero values
func (app *Server) defaults() {
if app.Router == nil {
app.Router = mux.NewRouter()
}
if app.Deadline.Nanoseconds() == 0 {
app.Deadline = time.Second * 10
}
if app.OnClose == nil {
app.OnClose = func() {}
}
if app.AllowedOrigins == nil || len(app.AllowedOrigins) == 0 {
app.AllowedOrigins = []string{"*"}
}
if app.AllowedMethods == nil || len(app.AllowedMethods) == 0 {
app.AllowedMethods = []string{"GET", "POST", "DELETE", "PUT"}
}
if app.AllowedHeaders == nil || len(app.AllowedHeaders) == 0 {
app.AllowedHeaders = []string{"Authorization", "Content-Type"}
}
if app.Console == nil {
app.Console = coat.NewConsole(app.Address, app.Silence)
}
if app.Stream.Console == nil {
app.Stream.Console = app.Console
}
if app.Storage == nil {
app.Storage = &MemoryStorage{}
}
if app.Tick == 0 {
app.Tick = 1 * time.Second
}
if app.ReadTimeout == 0 {
app.ReadTimeout = 1 * time.Minute
}
if app.WriteTimeout == 0 {
app.WriteTimeout = 1 * time.Minute
}
if app.ReadHeaderTimeout == 0 {
app.ReadHeaderTimeout = 10 * time.Second
}
if app.IdleTimeout == 0 {
app.IdleTimeout = 10 * time.Second
}
if app.Audit == nil {
app.Audit = func(r *http.Request) bool { return true }
}
if app.OnSubscribe == nil {
app.OnSubscribe = func(key string) error { return nil }
}
if app.Stream.OnSubscribe == nil {
app.Stream.OnSubscribe = app.OnSubscribe
}
if app.OnUnsubscribe == nil {
app.OnUnsubscribe = func(key string) {}
}
if app.Stream.OnUnsubscribe == nil {
app.Stream.OnUnsubscribe = app.OnUnsubscribe
}
if app.Workers == 0 {
app.Workers = 6
}
if app.NoBroadcastKeys == nil {
app.NoBroadcastKeys = []string{}
}
if app.Client == nil {
app.Client = &http.Client{
Timeout: 10 * time.Second,
Transport: &http.Transport{
Dial: (&net.Dialer{
Timeout: 1 * time.Second,
KeepAlive: 10 * time.Second,
}).Dial,
IdleConnTimeout: 10 * time.Second,
ResponseHeaderTimeout: 10 * time.Second,
MaxConnsPerHost: 3000,
MaxIdleConns: 10000,
MaxIdleConnsPerHost: 1000,
DisableKeepAlives: false,
},
}
}
app.Stream.ForcePatch = app.ForcePatch
app.Stream.NoPatch = app.NoPatch
if app.Stream.ForcePatch && app.Stream.NoPatch {
app.Console.Err("both ForcePatch and NoPatch are enabled, only NoPatch will be used")
}
app.Stream.InitClock()
}
// Start : initialize and start the http server and database connection
func (app *Server) Start(address string) {
app.Address = address
if atomic.LoadInt64(&app.active) == 1 {
app.Console.Err("server already active")
return
}
atomic.StoreInt64(&app.active, 0)
atomic.StoreInt64(&app.closing, 0)
app.defaults()
// https://ieftimov.com/post/make-resilient-golang-net-http-servers-using-timeouts-deadlines-context-cancellation/
app.Router.HandleFunc("/", app.getStats).Methods("GET")
// https://www.calhoun.io/why-cant-i-pass-this-function-as-an-http-handler/
app.Router.Handle("/{key:[a-zA-Z\\*\\d\\/]+}", http.TimeoutHandler(
http.HandlerFunc(app.unpublish), app.Deadline, deadlineMsg)).Methods("DELETE")
app.Router.Handle("/{key:[a-zA-Z\\*\\d\\/]+}", http.TimeoutHandler(
http.HandlerFunc(app.publish), app.Deadline, deadlineMsg)).Methods("POST")
app.Router.HandleFunc("/{key:[a-zA-Z\\*\\d\\/]+}", app.read).Methods("GET")
app.Router.HandleFunc("/{key:[a-zA-Z\\*\\d\\/]+}", app.read).Queries("v", "{[\\d]}").Methods("GET")
app.wg.Add(1)
go app.waitListen()
app.wg.Wait()
app.waitStart()
app.Console = coat.NewConsole(app.Address, app.Silence)
go app.tick()
}
// Close : shutdown the http server and database connection
func (app *Server) Close(sig os.Signal) {
if atomic.LoadInt64(&app.closing) != 1 {
atomic.StoreInt64(&app.closing, 1)
atomic.StoreInt64(&app.active, 0)
app.Storage.Close()
app.OnClose()
app.Console.Err("shutdown", sig)
if app.server != nil {
app.server.Shutdown(context.Background())
}
}
}
// WaitClose : Blocks waiting for SIGINT, SIGTERM, SIGKILL, SIGHUP
func (app *Server) WaitClose() {
app.Signal = make(chan os.Signal, 1)
done := make(chan bool, 1)
signal.Notify(app.Signal, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
go func() {
sig := <-app.Signal
app.Close(sig)
done <- true
}()
<-done
}