-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
81 lines (62 loc) · 2.13 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"time"
"github.com/CoderCookE/goaround/internal/customflags"
"github.com/CoderCookE/goaround/internal/gracefulserver"
"github.com/CoderCookE/goaround/internal/pool"
"github.com/CoderCookE/goaround/internal/stats"
)
func main() {
portString, metricPortString, backends, numConns, cacert, privkey, enableCache := parseFlags()
config := &pool.Config{
Backends: backends,
NumConns: *numConns,
EnableCache: *enableCache,
}
log.Printf("Starting with conf, %s %s %d", portString, backends, *numConns)
connectionPool := pool.New(config)
defer connectionPool.Shutdown()
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
defer r.Body.Close()
connectionPool.Fetch(w, r)
duration := time.Since(start).Seconds()
stats.Durations.WithLabelValues("handle").Observe(duration)
})
go stats.StartUp(metricPortString)
server := &http.Server{
Addr: portString,
Handler: handler,
ReadTimeout: 10 * time.Second,
WriteTimeout: 30 * time.Second,
}
defer server.Close()
graceful := gracefulserver.New(server)
var err error
if *cacert != "" && *privkey != "" {
err = graceful.ListenAndServeTLS(*cacert, *privkey)
} else {
err = graceful.ListenAndServe()
}
if err != nil {
log.Printf("ListenAndServe: %s", err)
}
}
func parseFlags() (portString, metricPortString string, backends customflags.Backend, numConns *int, cacert *string, privkey *string, enableCache *bool) {
port := flag.Int("p", 3000, "Load Balancer Listen Port (default: 3000)")
numConns = flag.Int("n", 3, "Max number of connections per backend")
backends = make(customflags.Backend, 0)
flag.Var(&backends, "b", "Backend location ex: http://localhost:9000")
cacert = flag.String("cacert", "", "cacert location")
privkey = flag.String("privkey", "", "privkey location")
metricPort := flag.Int("prometheus-port", 8080, "The address to listen on for HTTP requests.")
enableCache = flag.Bool("cache", false, "Enable request cache")
flag.Parse()
portString = fmt.Sprintf(":%d", *port)
metricPortString = fmt.Sprintf(":%d", *metricPort)
return
}