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
150
151
152
153
154
155
156
157
158
159
// design touchstones // configured entirely via environment variables // minimal configurable options // a single global cluster // easy cluster formation // todo // dns resolver for services package main import ( "fmt" "log" "os" "os/signal" "strings" "syscall" "time" "github.com/hashicorp/memberlist" "github.com/hashicorp/serf/serf" ) const ( // gracefulTimeout controls how long we wait before forcefully terminating // note that this value interacts with serfConfig.LeavePropagateDelay gracefulTimeout = 5 * time.Second ) func main() { config := readConfig() agent := setupAgent(config) if err := agent.Start(); err != nil { log.Panic(err) } defer agent.Shutdown() // join any specified startup nodes if err := startupJoin(config, agent); err != nil { log.Panic(err) } if err := handleSignals(config, agent); err != nil { log.Panic(err) } } // handleSignals blocks until we get an exit-causing signal func handleSignals(config *Config, agent *Agent) error { signalCh := make(chan os.Signal, 4) signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM) // Wait for a signal var sig os.Signal select { case s := <-signalCh: sig = s case <-agent.ShutdownCh(): // Agent is already shutdown! return nil } log.Printf("Caught signal: %v", sig) // Check if we should do a graceful leave graceful := false if sig == os.Interrupt || sig == syscall.SIGTERM { graceful = true } // Bail fast if not doing a graceful leave if !graceful { log.Fatal("[WARN] cascade: non-graceful leave detected") } // Attempt a graceful leave gracefulCh := make(chan struct{}) log.Println("Gracefully shutting down agent...") go func() { if err := agent.Leave(); err != nil { log.Printf("Error: %s", err) return } close(gracefulCh) }() // Wait for leave or another signal select { case <-signalCh: return fmt.Errorf("[WARN] cascade: idk") case <-time.After(gracefulTimeout): return fmt.Errorf("[WARN] cascade: leave timed out") case <-gracefulCh: return nil } } func readConfig() *Config { config := DefaultConfig() // CASCADE_BIND=127.0.0.1:1234 if os.Getenv("CASCADE_BIND") != "" { config.BindAddr = os.Getenv("CASCADE_BIND") } // CASCADE_JOIN=127.0.0.1,127.0.0.5 if os.Getenv("CASCADE_JOIN") != "" { config.StartJoin = strings.Split(os.Getenv("CASCADE_JOIN"), ",") } // CASCADE_NAME=nostromo.j3s.sh if os.Getenv("CASCADE_NAME") != "" { config.NodeName = os.Getenv("CASCADE_NAME") } return config } func startupJoin(config *Config, agent *Agent) error { if len(config.StartJoin) == 0 { return nil } log.Printf("Joining cluster...") n, err := agent.Join(config.StartJoin) if err != nil { return err } log.Printf("Join completed. Synced with %d initial agents", n) return nil } func setupAgent(config *Config) *Agent { bindIP, bindPort, err := config.AddrParts(config.BindAddr) if err != nil { log.Panic(err) } serfConfig := serf.DefaultConfig() serfConfig.NodeName = config.NodeName serfConfig.ProtocolVersion = uint8(serf.ProtocolVersionMax) // TODO: how should cascade handle name conflicts? // defaulting to just blowing up for now, but // we _could_ take the tailscale route & append // -1 or whatever to the node. that would be more user friendly. // TODO: some of these serf settings were pulled // from consul[1]. re-examine them eventually. serfConfig.EnableNameConflictResolution = false serfConfig.LeavePropagateDelay = 3 * time.Second serfConfig.MinQueueDepth = 4096 serfConfig.QueueDepthWarning = 1000000 serfConfig.ReconnectTimeout = 3 * 24 * time.Hour serfConfig.MemberlistConfig = memberlist.DefaultWANConfig() serfConfig.MemberlistConfig.DeadNodeReclaimTime = 30 * time.Second serfConfig.MemberlistConfig.BindAddr = bindIP serfConfig.MemberlistConfig.BindPort = bindPort agent := Create(serfConfig) return agent } // [1]: sources for consul serf tweaks // https://github.com/hashicorp/consul/blob/v1.14.4/agent/consul/config.go // https://github.com/hashicorp/consul/blob/v1.14.4/lib/serf/serf.go