-
Notifications
You must be signed in to change notification settings - Fork 22
/
main.go
142 lines (105 loc) · 3.32 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
package main
import (
"context"
"github.com/germanoeich/nirn-proxy/lib"
"github.com/hashicorp/memberlist"
_ "github.com/joho/godotenv/autoload"
"github.com/sirupsen/logrus"
"net"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"
)
var logger = logrus.New()
// token : queue map
var bufferSize = 50
func setupLogger() {
logLevel := lib.EnvGet("LOG_LEVEL", "info")
lvl, err := logrus.ParseLevel(logLevel)
if err != nil {
panic("Failed to parse log level")
}
logger.SetLevel(lvl)
lib.SetLogger(logger)
}
func initCluster(proxyPort string, manager *lib.QueueManager) *memberlist.Memberlist {
port := lib.EnvGetInt("CLUSTER_PORT", 7946)
memberEnv := os.Getenv("CLUSTER_MEMBERS")
dns := os.Getenv("CLUSTER_DNS")
if memberEnv == "" && dns == "" {
logger.Info("Running in stand-alone mode")
return nil
}
logger.Info("Attempting to create/join cluster")
var members []string
if memberEnv != "" {
members = strings.Split(memberEnv, ",")
} else {
ips, err := net.LookupIP(dns)
if err != nil {
logger.Panic(err)
}
if len(ips) == 0 {
logger.Panic("no ips returned by dns")
}
for _, ip := range ips {
members = append(members, ip.String())
}
}
return lib.InitMemberList(members, port, proxyPort, manager)
}
func main() {
outboundIp := os.Getenv("OUTBOUND_IP")
timeout := lib.EnvGetInt("REQUEST_TIMEOUT", 5000)
disableHttp2 := lib.EnvGetBool("DISABLE_HTTP_2", true)
globalOverrides := lib.EnvGet("BOT_RATELIMIT_OVERRIDES", "")
disableGlobalRatelimitDetection := lib.EnvGetBool("DISABLE_GLOBAL_RATELIMIT_DETECTION", false)
lib.ConfigureDiscordHTTPClient(outboundIp, time.Duration(timeout)*time.Millisecond, disableHttp2, globalOverrides, disableGlobalRatelimitDetection)
port := lib.EnvGet("PORT", "8080")
bindIp := lib.EnvGet("BIND_IP", "0.0.0.0")
setupLogger()
bufferSize = lib.EnvGetInt("BUFFER_SIZE", 50)
maxBearerLruSize := lib.EnvGetInt("MAX_BEARER_COUNT", 1024)
manager := lib.NewQueueManager(bufferSize, maxBearerLruSize)
mux := manager.CreateMux()
s := &http.Server{
Addr: bindIp + ":" + port,
Handler: mux,
ReadHeaderTimeout: 10 * time.Second,
IdleTimeout: 10 * time.Second,
WriteTimeout: 1 * time.Hour,
MaxHeaderBytes: 1 << 20,
}
if os.Getenv("ENABLE_PPROF") == "true" {
go lib.StartProfileServer()
}
if os.Getenv("ENABLE_METRICS") != "false" {
port := lib.EnvGet("METRICS_PORT", "9000")
go lib.StartMetrics(bindIp + ":" + port)
}
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
go func() {
if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.WithFields(logrus.Fields{"function": "http.ListenAndServe"}).Panic(err)
}
}()
logger.Info("Started proxy on " + bindIp + ":" + port)
// Wait for the http server to ready before joining the cluster
<-time.After(1 * time.Second)
initCluster(port, manager)
<-done
logger.Info("Server received shutdown signal")
logger.Info("Broadcasting leave message to cluster, if in cluster mode")
manager.Shutdown()
logger.Info("Gracefully shutting down HTTP server")
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
if err := s.Shutdown(ctx); err != nil {
logger.WithFields(logrus.Fields{"function": "http.Shutdown"}).Error(err)
}
logger.Info("Bye bye")
}