forked from ha/doozerd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doozerd.go
157 lines (132 loc) · 3.44 KB
/
doozerd.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
150
151
152
153
154
155
156
157
package main
import (
"crypto/tls"
_ "expvar"
"flag"
"fmt"
"github.com/ha/doozer"
"github.com/ha/doozerd/peer"
"log"
"net"
"os"
"strconv"
)
const defWebPort = 8000
type strings []string
func (a *strings) Set(s string) error {
*a = append(*a, s)
return nil
}
func (a *strings) String() string {
return fmt.Sprint(*a)
}
var (
laddr = flag.String("l", "127.0.0.1:8046", "The address to bind to.")
aaddrs = strings{}
buri = flag.String("b", "", "boot cluster uri (tried after -a)")
waddr = flag.String("w", "", "web listen addr (default: see below)")
name = flag.String("c", "local", "The non-empty cluster name.")
showVersion = flag.Bool("v", false, "print doozerd's version string")
pi = flag.Float64("pulse", 1, "how often (in seconds) to set applied key")
fd = flag.Float64("fill", .1, "delay (in seconds) to fill unowned seqns")
kt = flag.Float64("timeout", 60, "timeout (in seconds) to kick inactive nodes")
hi = flag.Int64("hist", 2000, "length of history/revisions to keep")
certFile = flag.String("tlscert", "", "TLS public certificate")
keyFile = flag.String("tlskey", "", "TLS private key")
)
var (
rwsk = os.Getenv("DOOZER_RWSECRET")
rosk = os.Getenv("DOOZER_ROSECRET")
)
func init() {
flag.Var(&aaddrs, "a", "attach address (may be given multiple times)")
}
func Usage() {
fmt.Fprintf(os.Stderr, "Usage: %s [OPTIONS]\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\nOptions:\n")
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, `
The default for -w is to use the addr from -l,
and change the port to 8000. If you give "-w false",
doozerd will not listen for for web connections.
`)
}
func main() {
*buri = os.Getenv("DOOZER_BOOT_URI")
flag.Usage = Usage
flag.Parse()
if *showVersion {
fmt.Println("doozerd", peer.Version)
return
}
if *laddr == "" {
fmt.Fprintln(os.Stderr, "require a listen address")
flag.Usage()
os.Exit(1)
}
log.SetPrefix("DOOZER ")
log.SetFlags(log.Ldate | log.Lmicroseconds)
tsock, err := net.Listen("tcp", *laddr)
if err != nil {
panic(err)
}
if *certFile != "" || *keyFile != "" {
tsock = tlsWrap(tsock, *certFile, *keyFile)
}
uaddr, err := net.ResolveUDPAddr("udp", *laddr)
if err != nil {
panic(err)
}
usock, err := net.ListenUDP("udp", uaddr)
if err != nil {
panic(err)
}
var wsock net.Listener
if *waddr == "" {
wa, err := net.ResolveTCPAddr("tcp", *laddr)
if err != nil {
panic(err)
}
wa.Port = defWebPort
*waddr = wa.String()
}
if b, err := strconv.ParseBool(*waddr); err != nil && !b {
wsock, err = net.Listen("tcp", *waddr)
if err != nil {
panic(err)
}
}
id := randId()
var cl *doozer.Conn
switch {
case len(aaddrs) > 0 && *buri != "":
cl = attach(*name, aaddrs)
if cl == nil {
cl = boot(*name, id, *laddr, *buri)
}
case len(aaddrs) > 0:
cl = attach(*name, aaddrs)
if cl == nil {
panic("failed to attach")
}
case *buri != "":
cl = boot(*name, id, *laddr, *buri)
}
peer.Main(*name, id, *buri, rwsk, rosk, cl, usock, tsock, wsock, ns(*pi), ns(*fd), ns(*kt), *hi)
panic("main exit")
}
func ns(x float64) int64 {
return int64(x * 1e9)
}
func tlsWrap(l net.Listener, cfile, kfile string) net.Listener {
if cfile == "" || kfile == "" {
panic("need both cert file and key file")
}
cert, err := tls.LoadX509KeyPair(cfile, kfile)
if err != nil {
panic(err)
}
tc := new(tls.Config)
tc.Certificates = append(tc.Certificates, cert)
return tls.NewListener(l, tc)
}