/
agent.go
304 lines (251 loc) · 6.37 KB
/
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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package main
import (
"io"
"log"
"net"
"net/http"
"strconv"
"time"
"github.com/armon/consul-api"
)
const (
MANIPT_KEY = "service/manipt/leader"
)
var (
AgentWaitTime = 50 * time.Millisecond
)
type Agent struct {
Client *consulapi.Client
Leader *consulapi.Node
Bind *net.TCPAddr
PubListener *net.TCPListener
WebAppListener *net.TCPListener
Server *http.Server
Session string
leaderchan chan *consulapi.Node
connchan chan *net.TCPConn
proxychan chan *net.TCPConn
quitupdater chan struct{}
quitproxy chan struct{}
quitupresp chan struct{}
}
func NewAgent(c *consulapi.Client, bind string, port int, server *http.Server) *Agent {
tcpaddr, err := net.ResolveTCPAddr("tcp", bind+":"+strconv.Itoa(port))
if err != nil {
panic(err)
}
return &Agent{
Client: c,
Leader: nil,
Bind: tcpaddr,
Server: server,
leaderchan: make(chan *consulapi.Node),
connchan: make(chan *net.TCPConn, 1024),
proxychan: make(chan *net.TCPConn, 1024),
quitupdater: make(chan struct{}),
quitproxy: make(chan struct{}),
quitupresp: make(chan struct{}),
}
}
func (a *Agent) Quit() {
log.Printf("[info] agent called to quit")
a.quitupdater <- struct{}{}
a.quitproxy <- struct{}{}
a.PubListener.Close()
a.WebAppListener.Close()
<-a.quitupresp
log.Printf("[info] agent quit successfully")
}
func (a *Agent) proxyConns() {
for {
c, err := a.PubListener.AcceptTCP()
if err != nil {
select {
case <-a.quitproxy:
log.Printf("[info] proxy called to quit")
return
default:
log.Printf("[err] failed to accept conn: %s", err)
continue
}
}
a.connchan <- c
}
}
func (a *Agent) setup() {
publisten, err := net.ListenTCP("tcp", a.Bind)
if err != nil {
log.Fatalf("[fatal] failed to setup public listener")
}
a.PubListener = publisten
webapp, err := net.ListenTCP("tcp", &net.TCPAddr{
IP: net.IPv4(127, 0, 0, 1),
Port: 0,
})
if err != nil {
log.Fatalf("[fatal] failed to setup webapp listener")
}
a.WebAppListener = webapp
go a.Server.Serve(a.WebAppListener)
}
func (a *Agent) Run() {
// set our Listener
a.setup()
// get the current status first
a.Leader = <-a.leaderchan
if a.Leader == nil {
log.Printf("[info] starting as leader")
} else {
log.Printf("[info] starting as proxy to %s @ %s", a.Leader.Node, a.Leader.Address)
}
// start proxying connections
go a.proxyConns()
for {
select {
case node, ok := <-a.leaderchan:
if !ok {
return
}
log.Printf("[info] received leader node: %s", node)
if node == nil && a.Leader != nil {
log.Printf("[info] becoming leader")
} else if node != nil {
log.Printf("[info] becoming proxy to %s @ %s", node.Node, node.Address)
}
a.Leader = node
case incoming := <-a.connchan:
if a.Leader != nil {
log.Printf("[info] proxying %s", incoming.RemoteAddr())
a.proxyTo(a.Leader, incoming)
} else {
a.proxyWebApp(incoming)
}
}
}
}
func (a *Agent) proxyWebApp(conn *net.TCPConn) {
waddr := a.WebAppListener.Addr().(*net.TCPAddr)
proxy, err := net.DialTCP("tcp", nil, waddr)
if err != nil {
log.Printf("[err] failed to send to webapp, dropping connection: %s", err)
conn.Close()
return
}
go a.proxyStream(conn, proxy)
go a.proxyStream(proxy, conn)
}
func (a *Agent) proxyTo(to *consulapi.Node, conn *net.TCPConn) {
proxy, err := net.Dial("tcp", to.Address+":"+strconv.Itoa(a.Bind.Port))
if err != nil {
log.Printf("[err] failed to proxy to leader, will drop connection: %s", err)
conn.Close()
return
}
tcpproxy, ok := proxy.(*net.TCPConn)
if !ok {
log.Fatalf("[fatal] assert failed - tcpproxy should be a *net.TCPConn")
}
go a.proxyStream(conn, tcpproxy)
go a.proxyStream(tcpproxy, conn)
}
func (a *Agent) proxyStream(from, to *net.TCPConn) {
for {
buf := make([]byte, 1024)
_, err := from.Read(buf)
if err != nil {
if err != io.EOF {
log.Printf("[err] error reading from %s to %s: %s", from, to, err)
}
from.CloseRead()
to.CloseWrite()
return
}
_, err = to.Write(buf)
if err != nil {
log.Printf("[err] error writing from %s to %s: %s", from, to, err)
return
}
}
}
func (a *Agent) LeaderUpdater() {
nodename, err := a.Client.Agent().NodeName()
if err != nil {
log.Fatalf("[fatal] can't get our own node name: %s", err)
}
if a.Session == "" {
str, _, err := a.Client.Session().Create(&consulapi.SessionEntry{
LockDelay: 1000 * time.Nanosecond, // minimum value accepted by consul
}, nil)
if err != nil {
log.Fatalf("[fatal] failed to create session in consul: %s", err)
}
a.Session = str
}
var lastindex uint64
kv := a.Client.KV()
for {
res, _, err := kv.Acquire(&consulapi.KVPair{
Key: MANIPT_KEY + "/" + strconv.Itoa(a.Bind.Port),
Value: []byte(nodename),
Session: a.Session,
}, nil)
if err != nil {
log.Fatalf("[fatal] couldn't contact consul for acquiring lock: %s", err)
}
if res {
// we're leader!
a.leaderchan <- nil
}
for {
// who's leader? let's check
pair, meta, err := kv.Get(MANIPT_KEY+"/"+strconv.Itoa(a.Bind.Port), &consulapi.QueryOptions{
WaitIndex: lastindex,
WaitTime: AgentWaitTime,
})
if err != nil {
log.Printf("[err] failed to get kv for current leader node: %s", err)
break
}
select {
case <-a.quitupdater:
log.Printf("[info] called to quit")
if string(pair.Value) == nodename && pair.Session != "" { // give up leadership
log.Printf("[info] giving up leadership")
kv.Release(pair, nil)
log.Printf("[info] released leadership successfully")
}
close(a.leaderchan)
a.quitupresp <- struct{}{}
log.Printf("[info] updater shutdown complete")
return
default:
}
if pair.Session == "" {
// whoever was no longer is
// handle until we get a new leader
a.leaderchan <- nil
// set the last index
lastindex = meta.LastIndex
// wait for a moment (lockdelays)
time.Sleep(1 * time.Second)
// so we're going to try getting it
break
}
if lastindex == meta.LastIndex {
// nothing's changed, continue
continue
}
if string(pair.Value) != nodename { // non-leaders
// find this person
catalog := a.Client.Catalog()
node, _, err := catalog.Node(string(pair.Value), nil)
if err != nil {
log.Printf("[err] failed to get current leader node: %s", err)
continue
}
a.leaderchan <- node.Node // send new leader off
}
lastindex = meta.LastIndex
}
}
}