agent.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
package main import ( "fmt" "sync" "github.com/hashicorp/serf/serf" "golang.org/x/exp/slog" ) // Agent starts and manages a Serf & adds service discovery (TODO) type Agent struct { serfConf *serf.Config eventCh chan serf.Event // This is the underlying Serf we are wrapping serf *serf.Serf // a lil logger used by the agent, you know how it be logger *slog.Logger // this channel receiving a signal = shutdown shutdown bool shutdownCh chan struct{} shutdownLock sync.Mutex } func Create(config *Config, serfConf *serf.Config, logger *slog.Logger) *Agent { // Create a channel to listen for events from Serf eventCh := make(chan serf.Event, 64) serfConf.EventCh = eventCh // Setup the agent agent := &Agent{ serfConf: serfConf, eventCh: eventCh, logger: logger, shutdownCh: make(chan struct{}), } return agent } func (a *Agent) eventLoop() { serfShutdownCh := a.serf.ShutdownCh() for { select { case e := <-a.eventCh: a.logger.Info("receive event", "event", e.String()) case <-serfShutdownCh: a.logger.Info("detect serf shutdown, turning off event loop") a.Shutdown() return case <-a.shutdownCh: return } } } func (a *Agent) Start() error { a.logger.Info("start serf agent") // Create serf first serf, err := serf.Create(a.serfConf) if err != nil { return err } a.serf = serf // Start event loop go a.eventLoop() return nil } // Shutdown closes this agent and all of its processes. Should be preceded // by a Leave for a graceful shutdown. func (a *Agent) Shutdown() error { a.shutdownLock.Lock() defer a.shutdownLock.Unlock() if a.shutdown { return nil } if a.serf == nil { goto EXIT } a.logger.Info("request serf shutdown") if err := a.serf.Shutdown(); err != nil { return err } EXIT: a.logger.Info("complete serf shutdown") a.shutdown = true close(a.shutdownCh) return nil } // Join asks the Serf instance to join. See the Serf.Join function. func (a *Agent) Join(addrs []string) (n int, err error) { a.logger.Info("issue join request", "addrs", addrs) // we always ignore old events because cascade don't // care about the past n, err = a.serf.Join(addrs, true) if err != nil { return n, fmt.Errorf("Error joining: %v\n", err) } // TODO: when joining fails, we don't get an error here - serf & memberlist // just print to stdout and serf.Join returns without issue. return n, err } // Leave prepares for a graceful shutdown of the agent and its processes func (a *Agent) Leave() error { if a.serf == nil { return nil } a.logger.Info("request graceful serf leave") return a.serf.Leave() } // ShutdownCh returns a channel that can be selected to wait // for the agent to perform a shutdown. func (a *Agent) ShutdownCh() <-chan struct{} { return a.shutdownCh }