-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
90 lines (72 loc) · 1.78 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
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"github.com/hashicorp/memberlist"
)
type bootNodes []string
func (bn *bootNodes) String() string {
return strings.Join(*bn, ", ")
}
func (bn *bootNodes) Set(value string) error {
*bn = append(*bn, value)
return nil
}
const (
dirMode = 0755
fileMode = 0644
fileFlags = os.O_CREATE | os.O_RDWR
// NumReplicas default number of replicas.
NumReplicas uint8 = 2 // 32
// NumShards default number of shards.
NumShards uint16 = 8 // 1024
)
var (
nothing struct{}
host string
port uint
boots bootNodes
basePath string
)
func init() {
flag.StringVar(&host, "host", "0.0.0.0", "Hostname or IP address")
flag.UintVar(&port, "port", 50000, "Port number")
flag.Var(&boots, "boot", "Boot node address(es)")
flag.StringVar(&basePath, "base", "./data", "Base path")
flag.Parse()
}
func main() {
mutex := &sync.Mutex{}
fs := NewFileSystem()
name := fmt.Sprintf("%s:%d", host, port)
ring := NewRing(NumReplicas, name)
sharder := NewSharder(NumShards, fs, basePath)
cache := NewCache()
events := NewEvents(mutex, ring, sharder, cache)
_ = NewAPI(mutex, ring, sharder, cache, fs)
cfg := memberlist.DefaultLocalConfig()
cfg.Name = name
cfg.BindAddr = host
cfg.BindPort = int(port)
cfg.Events = events
// cfg.ProtocolVersion = memberlist.ProtocolVersionMax
list, err := memberlist.Create(cfg)
if err != nil {
panic(err)
}
if len(boots) > 0 {
_, err = list.Join(boots)
if err != nil {
panic(err)
}
}
fmt.Printf("SELF: %s\n", list.LocalNode().Name)
sig := make(chan os.Signal)
signal.Notify(sig, syscall.SIGHUP, syscall.SIGINT, syscall.SIGKILL, syscall.SIGQUIT, syscall.SIGTERM)
<-sig
}