-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
56 lines (51 loc) · 1.19 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
package main
import (
"fmt"
"log"
"net/http"
"sync"
"github.com/gcinterceptor/gci-go/httphandler"
"github.com/kelseyhightower/envconfig"
)
type config struct {
UseGCI bool `default:"false" envconfig:"USE_GCI"`
Port int `default:"3000" envconfig:"PORT"`
WindowSize int `default:"0" envconfig:"WINDOW_SIZE"`
MsgSize int `default:"1024" envconfig:"MSG_SIZE"`
}
var (
msgCount = 0
buffer [][]byte
mu sync.Mutex
)
func main() {
var c config
err := envconfig.Process("", &c)
if err != nil {
log.Fatal(err.Error())
}
fmt.Printf("Configuration: %+v\n", c)
// Inspiration: https://making.pusher.com/golangs-real-time-gc-in-theory-and-practice/
if c.WindowSize > 0 {
buffer = make([][]byte, c.WindowSize)
}
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
m := make([]byte, c.MsgSize)
for i := range m {
m[i] = byte(i)
}
if c.WindowSize > 0 {
mu.Lock()
buffer[msgCount] = m
msgCount = (msgCount + 1) % c.WindowSize
mu.Unlock()
}
})
if c.UseGCI {
http.Handle("/", httphandler.GCI(handler))
fmt.Println("==< Using GCI >==")
} else {
http.Handle("/", handler)
}
http.ListenAndServe(fmt.Sprintf(":%d", c.Port), nil)
}