-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
149 lines (123 loc) · 2.52 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
143
144
145
146
147
148
149
package main
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"os/signal"
"syscall"
)
var PROC_DIR string
const (
DEFAULT_LISTEN string = "0.0.0.0:5743"
)
func showHelpMessage() int {
fmt.Println("NAME")
fmt.Println(" salsa - TCP load balancer")
fmt.Println()
fmt.Println("SYNOPSIS")
fmt.Println(" salsa [-l <listening-address>] <backends...>")
fmt.Println()
fmt.Println("OPTIONS")
flag.PrintDefaults()
fmt.Println("EXAMPLES")
fmt.Println(" salsa -l :4000 127.0.0.1:3000 127.0.0.1:3001 127.0.0.1:3002")
return 0
}
func showManPage() int {
cmd := exec.Command("man", "salsa")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
fmt.Printf("error opening man page: %s\n", err.Error())
return 1
}
return 0
}
func getBackends(backends []string) []Backend {
var ret []Backend
for _, backend := range backends {
path := fmt.Sprintf(
PROC_DIR+"/backends/%s", backend,
)
ret = append(ret, NewBackend(backend, path))
}
return ret
}
func initProcDir(l *TCPListener) error {
err := os.MkdirAll(PROC_DIR, 0755)
if err != nil {
return err
}
err = os.Mkdir(PROC_DIR+"/backends", 0755)
if err != nil {
return err
}
for i := range l.Backends {
backend := &l.Backends[i]
os.Mkdir(backend.dir, 0755)
err = os.WriteFile(
backend.dir+"/score",
([]byte)(fmt.Sprintf("%d\n", backend.GetScore())),
0644,
)
if err != nil {
return err
}
}
return nil
}
func cleanup(l *TCPListener) {
l.Shutdown()
os.RemoveAll(PROC_DIR)
os.Exit(0)
}
func run() int {
if uid := syscall.Getuid(); uid == 0 {
PROC_DIR = fmt.Sprintf("/var/run/salsa/%d", os.Getpid())
} else {
PROC_DIR = fmt.Sprintf("/run/user/%d/salsa/%d", uid, os.Getpid())
}
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
listen := flag.String("l", DEFAULT_LISTEN, "listening address")
help := flag.Bool("h", false, "show help message")
manPage := flag.Bool("help", false, "show man page")
flag.Parse()
backends := flag.Args()
if *manPage == true {
return showManPage()
}
if *help == true {
return showHelpMessage()
}
listener := NewTCPListener(
*listen,
getBackends(backends),
)
defer cleanup(&listener)
err := initProcDir(&listener)
if err != nil {
log.Println(err)
return 2
}
tcpServerStopped := make(chan struct{})
go func() {
err := listener.Listen()
if err != nil {
log.Println(err.Error())
}
close(tcpServerStopped)
}()
select {
case <-sigs:
return 0
case <-tcpServerStopped:
return 3
}
}
func main() {
os.Exit(run())
}