-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
71 lines (58 loc) · 1.59 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
package main
import (
"flag"
"github.com/austingebauer/go-tcp-metrics-proxy/proxy"
"log"
"os"
"os/signal"
"syscall"
)
var (
listenAddress string
targetAddress string
metricAddress string
)
func init() {
flag.StringVar(&listenAddress, "listen", "127.0.0.1:3000",
"IP address and port number that the proxy will listen on")
flag.StringVar(&targetAddress, "target", "127.0.0.1:3001",
"IP address and port number that the proxy will forward to")
flag.StringVar(&metricAddress, "metrics", "127.0.0.1:3002",
"IP address and port number to expose prometheus metrics on")
}
func main() {
// Parse flags and assign to configuration
flag.Parse()
config := proxy.NewConfig(listenAddress, targetAddress, metricAddress)
// Set up channels and signal handling
errorCh := make(chan error)
doneCh := make(chan struct{})
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, syscall.SIGINT, syscall.SIGTERM)
// Configure and run the proxy
p := proxy.NewProxy(config, doneCh)
go func() {
errorCh <- p.Start()
}()
var finalError error
// Block until an error or signal is received
select {
case sig := <-signalCh:
log.Printf("received signal: %v\n", sig)
// Stop gracefully for SIGTERM and SIGINT
p.StopGraceful()
case err := <-errorCh:
finalError = err
// Stop forcefully for errors
p.StopForceful()
}
// Block until the done channel has been closed by the proxy
<-doneCh
// If the proxy stopped due to an error, then log fatally
if finalError != nil {
log.Fatal(finalError)
}
// Otherwise, the proxy stopped due to a signal, so exit 0
log.Println("exit: 0")
os.Exit(0)
}