-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain.go
81 lines (63 loc) · 1.81 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 (
"context"
"flag"
"fmt"
"log"
stderrLogger "log"
stdoutLogger "log"
"net"
"os"
"os/signal"
"sync"
"syscall"
"time"
)
var (
udpListenAddress = flag.String("udp-listen-address", "0.0.0.0:9090", "The address to listen on for udp stats requests.")
httpListenAddress = flag.String("http-listen-address", "0.0.0.0:9091", "The address to listen on for http stat and telemetry requests.")
)
func main() {
flag.Parse()
stderrLogger.SetOutput(os.Stderr)
stdoutLogger.SetOutput(os.Stdout)
errrorLogger := stderrLogger.Default()
infoLogger := stdoutLogger.Default()
infoLogger.Print("welcome to messagebird/pushprom")
ctx, cancel := context.WithCancel(context.Background())
var err error
*udpListenAddress, err = ListenAddress(*udpListenAddress)
if err != nil {
errrorLogger.Fatalf(err.Error())
}
*httpListenAddress, err = ListenAddress(*httpListenAddress)
if err != nil {
errrorLogger.Fatalf(err.Error())
}
infoLogger.Print("starting listeners")
var wg sync.WaitGroup
go listenUDP(&wg, ctx, errrorLogger, infoLogger)
go listenHTTP(&wg, ctx, errrorLogger, infoLogger)
wg.Add(2)
handleSIGTERM(&wg, cancel, infoLogger)
}
func handleSIGTERM(wg *sync.WaitGroup, cancel func(), infoLogger *log.Logger) {
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGTERM, os.Interrupt)
<-sigc
infoLogger.Println("received termination/interrupt signal, will terminate")
cancel()
defer close(sigc)
infoLogger.Println("waiting for all servers to gracefully terminate")
wg.Wait()
time.Sleep(1 * time.Second)
infoLogger.Println("all goroutines gracefully finished")
}
// ListenAddress Format a correct listen address
func ListenAddress(s string) (string, error) {
host, port, err := net.SplitHostPort(s)
if err != nil {
return "", err
}
return fmt.Sprintf("%s:%s", host, port), nil
}