-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
171 lines (157 loc) · 4.56 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
package main
import (
"bytes"
"flag"
"fmt"
client "github.com/coreos/etcd/clientv3"
"log"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"
)
var (
listen string
destination string
updateRate time.Duration
quotaDir string
usersFile string
requestTimeout time.Duration
endpoints []string
storage Storage
state = make(State)
quota = make(Quota)
scheduler chan struct{}
directoryWatcher chan struct{}
)
func scheduleCapacitiesUpdate() chan struct{} {
ticker := time.NewTicker(updateRate)
quit := make(chan struct{})
go func() {
for {
select {
case <-ticker.C:
refreshAllCapacities()
case <-quit:
{
ticker.Stop()
return
}
}
}
}()
return quit
}
func refreshAllCapacities() {
for quotaName, quotaState := range state {
for browserId, browserState := range *quotaState {
maxConnections := quota.MaxConnections(
quotaName,
browserId.Name,
browserId.Version,
)
refreshCapacities(maxConnections, *browserState)
}
}
}
func init() {
flag.StringVar(&listen, "listen", ":8080", "Host and port to listen to")
flag.StringVar(&destination, "destination", ":4444", "Host and port to proxy to")
flag.DurationVar(&updateRate, "updateRate", 1*time.Second, "Time between refreshing queue lengths like 1s or 500ms")
flag.StringVar("aDir, "quotaDir", "quota", "Directory to search for quota XML files")
flag.StringVar(&usersFile, "users", "users.properties", "Path of the list of users")
flag.DurationVar(&requestTimeout, "timeout", 300*time.Second, "Session timeout like 3s or 500ms")
var list string
flag.StringVar(&list, "endpoints", "http://127.0.0.1:2379", "comma-separated list of etcd endpoints")
flag.Parse()
endpoints = strings.Split(list, ",")
}
func dumpState() chan os.Signal {
ch := make(chan os.Signal)
signal.Notify(ch, syscall.SIGUSR2)
go func() {
for {
<-ch
var bb bytes.Buffer
bb.WriteString("\n")
bb.WriteString("==========\n")
bb.WriteString("STATE DUMP\n")
bb.WriteString("==========\n")
for quotaName, quotaState := range state {
bb.WriteString(fmt.Sprintf("Quota: %s\n", quotaName))
for browserId, browserState := range *quotaState {
bb.WriteString("---\n")
bb.WriteString(fmt.Sprintf("Browser: %s %s\n", browserId.Name, browserId.Version))
for processName, process := range *browserState {
bb.WriteString(fmt.Sprintf("Process: name=%s priority=%d queued=%d lastUpdate=%s\n", processName, process.Priority, len(process.AwaitQueue), process.LastActivity.Format(time.UnixDate)))
bb.WriteString(process.CapacityQueue.Dump())
}
}
}
bb.WriteString("\n")
bb.WriteString("=============\n")
bb.WriteString("SESSIONS DUMP\n")
bb.WriteString("=============\n")
for sessionId, process := range sessions {
bb.WriteString("---\n")
bb.WriteString(fmt.Sprintf("Session: id=%s\n", sessionId))
bb.WriteString(process.CapacityQueue.Dump())
}
bb.WriteString("\n")
bb.WriteString("====================\n")
bb.WriteString("TIMEOUT CANCELS DUMP\n")
bb.WriteString("====================\n")
for sessionId := range timeoutCancels {
bb.WriteString(fmt.Sprintf("Cancel: sessionId=%s\n", sessionId))
}
bb.WriteString("\n")
bb.WriteString("===========\n")
bb.WriteString("LEASES DUMP\n")
bb.WriteString("===========\n")
for sessionId, lease := range leases {
bb.WriteString(fmt.Sprintf("Lease: sessionId=%s lease=%s\n", sessionId, lease))
}
log.Println(bb.String())
}
}()
return ch
}
func waitForShutdown(shutdownAction func()) {
ch := make(chan os.Signal)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
<-ch
shutdownAction()
}
func createStorage() Storage {
cfg := client.Config{
Endpoints: endpoints,
DialTimeout: 10 * time.Second,
}
c, err := client.New(cfg)
if err != nil {
log.Fatal(err)
}
log.Printf("Connected to storage with endpoints: %s\n", endpoints)
return NewEtcdStorage(c)
}
func main() {
directoryWatcher = LoadAndWatch(quotaDir, "a)
defer close(directoryWatcher)
scheduler = scheduleCapacitiesUpdate()
defer close(scheduler)
storage = createStorage()
defer storage.Close()
dumpChan := dumpState()
defer close(dumpChan)
go waitForShutdown(func() {
log.Println("shutting down server")
//TODO: wait for all connections to close with timeout
os.Exit(0)
})
log.Println("listening on", listen)
log.Println("destination host is", destination)
server := &http.Server{Addr: listen, Handler: mux(), ReadTimeout: requestTimeout, WriteTimeout: requestTimeout}
server.ListenAndServe()
}