-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
60 lines (45 loc) · 1.37 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
package main
import (
"flag"
"log"
"net/url"
"strings"
"github.com/carantes/golb/lb"
"github.com/carantes/golb/server"
)
var (
addr = flag.String("addr", ":8080", "http service address")
st = flag.String("type", "node", "server type: node or lb")
// Server specific flags
// lbAddr = flag.String("lbAddr", "http://localhost:8080", "load balancer address")
// Load balancer specific flags
alg = flag.String("alg", "roundrobin", "load balancing algorithm: roundrobin or random")
urls = flag.String("urls", "http://localhost:8081,http://localhost:8082", "Initial urls to distribute the load")
interval = flag.Int("interval", 5, "Heartbeat interval in seconds")
)
// ParseNodeFromString parses a string of nodes into a Node list.
func ParseNodeFromString(nodes string) []*lb.Node {
var parsedNodes []*lb.Node
for _, node := range strings.Split(nodes, ",") {
parsedNode, err := url.Parse(node)
if err != nil {
log.Fatal("Invalid node URL")
}
parsedNodes = append(parsedNodes, lb.NewNode(*parsedNode))
}
return parsedNodes
}
func main() {
flag.Parse()
// Start a server node
if *st == "node" {
server.NewServer().Run(*addr)
}
// Start the load balancer
if *st == "lb" {
algorithm := lb.ParseAlgorithm(*alg)
nodes := ParseNodeFromString(*urls)
lb.NewLoadBalancer(nodes, algorithm, *interval).Run(*addr)
}
panic("Invalid server type")
}