-
Notifications
You must be signed in to change notification settings - Fork 3
/
loadbalancer.go
60 lines (53 loc) · 1.1 KB
/
loadbalancer.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
package main
import (
"flag"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"github.com/bborbe/loadbalancer/server"
"github.com/bborbe/log"
)
var logger = log.DefaultLogger
func main() {
defer logger.Close()
logger.Debug("loadbalancer started")
portPtr := flag.Int("port", 8081, "ListenPort")
nodesPtr := flag.String("nodes", "", "NodeList")
flag.Parse()
port := *portPtr
nodes := splitNodes(*nodesPtr)
if port <= 0x0 || port >= 0xFFFF {
flag.Usage()
return
}
srv, err := server.NewServer(":"+strconv.Itoa(port), nodes)
if err != nil {
logger.Debugf("create server failed, %v", err)
return
}
{
err := srv.Start()
if err != nil {
logger.Debugf("start server failed, %v", err)
return
}
}
ch := make(chan os.Signal)
signal.Notify(ch, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)
for {
sig := <-ch
logger.Debugf("receive signal %v", sig)
err := srv.Stop()
if err != nil {
logger.Debugf("stop server failed, %v", err)
return
}
logger.Debug("loadbalancer finished")
return
}
}
func splitNodes(content string) []string {
return strings.Split(content, ",")
}