-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracker.go
339 lines (287 loc) · 8.22 KB
/
tracker.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package acomm
import (
"errors"
"fmt"
"io/ioutil"
"net"
"net/url"
"os"
"sync"
"time"
log "github.com/Sirupsen/logrus"
)
const (
statusStarted = iota
statusStopping
statusStopped
)
// Tracker keeps track of requests waiting on a response.
type Tracker struct {
status int
responseListener *UnixListener
httpStreamURL *url.URL
defaultTimeout time.Duration
requestsLock sync.Mutex // Protects requests
requests map[string]*Request
dsLock sync.Mutex // Protects dataStreams
dataStreams map[string]*UnixListener
waitgroup sync.WaitGroup
}
// NewTracker creates and initializes a new Tracker. If a socketPath is not
// provided, the response socket will be created in a temporary directory.
func NewTracker(socketPath string, httpStreamURL *url.URL, defaultTimeout time.Duration) (*Tracker, error) {
if socketPath == "" {
var err error
socketPath, err = generateTempSocketPath("", "acommTrackerResponses-")
if err != nil {
return nil, err
}
}
if defaultTimeout <= 0 {
defaultTimeout = time.Minute
}
return &Tracker{
status: statusStopped,
responseListener: NewUnixListener(socketPath, 0),
httpStreamURL: httpStreamURL,
dataStreams: make(map[string]*UnixListener),
defaultTimeout: defaultTimeout,
}, nil
}
func generateTempSocketPath(dir, prefix string) (string, error) {
// Use TempFile to allocate a uniquely named file in either the specified
// dir or the default temp dir. It is then removed so that the unix socket
// can be created with that name.
// TODO: Decide on permissions
if dir != "" {
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
log.WithFields(log.Fields{
"directory": dir,
"perm": os.ModePerm,
"error": err,
}).Error("failed to create directory for socket")
return "", err
}
}
f, err := ioutil.TempFile(dir, prefix)
if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Error("failed to create temp file for response socket")
return "", err
}
_ = f.Close()
_ = os.Remove(f.Name())
return fmt.Sprintf("%s.sock", f.Name()), nil
}
// NumRequests returns the number of tracked requests
func (t *Tracker) NumRequests() int {
t.requestsLock.Lock()
defer t.requestsLock.Unlock()
return len(t.requests)
}
// Addr returns the string representation of the Tracker's response listener socket.
func (t *Tracker) Addr() string {
return t.responseListener.Addr()
}
// URL returns the URL of the Tracker's response listener socket.
func (t *Tracker) URL() *url.URL {
return t.responseListener.URL()
}
// Start activates the tracker. This allows tracking of requests as well as
// listening for and handling responses.
func (t *Tracker) Start() error {
if t.status == statusStarted {
return nil
}
if t.status == statusStopping {
return errors.New("can't start tracker while stopping")
}
t.requests = make(map[string]*Request)
// start the proxy response listener
if err := t.responseListener.Start(); err != nil {
return err
}
go t.listenForResponses()
t.status = statusStarted
return nil
}
// listenForResponse continually accepts new responses on the listener.
func (t *Tracker) listenForResponses() {
for {
conn := t.responseListener.NextConn()
if conn == nil {
return
}
go t.handleConn(conn)
}
}
// handleConn handles the response connection and parses the data.
func (t *Tracker) handleConn(conn net.Conn) {
defer t.responseListener.DoneConn(conn)
resp := &Response{}
if err := UnmarshalConnData(conn, resp); err != nil {
return
}
_ = SendConnData(conn, &Response{})
go t.HandleResponse(resp)
}
// HandleResponse associates a response with a request and either forwards the
// response or calls the request's handler.
func (t *Tracker) HandleResponse(resp *Response) {
req := t.retrieveRequest(resp.ID)
if req == nil {
err := errors.New("response does not have tracked request")
log.WithFields(log.Fields{
"error": err,
"response": resp,
}).Error(err)
return
}
defer t.waitgroup.Done()
// Stop the request timeout. The result doesn't matter.
if req.timeout != nil {
_ = req.timeout.Stop()
}
// If there are handlers, this is the final destination, so handle the
// response. Otherwise, forward the response along.
// Known issue: If this is the final destination and there are
// no handlers, there will be an extra redirects back here. Since the
// request has already been removed from the tracker, it will only happen
// once.
if !req.proxied {
req.HandleResponse(resp)
return
}
if resp.StreamURL != nil {
streamURL, err := t.ProxyStreamHTTPURL(resp.StreamURL) // Replace the StreamURL with a proxy stream url
if err != nil {
streamURL = nil
}
resp.StreamURL = streamURL
}
// Forward the response along
_ = req.Respond(resp)
return
}
// Stop deactivates the tracker. It blocks until all active connections or tracked requests to finish.
func (t *Tracker) Stop() {
// Nothing to do if it's not listening.
if t.responseListener == nil {
return
}
// Prevent new requests from being tracked
t.status = statusStopping
// Handle any requests that are expected
t.waitgroup.Wait()
// Stop listening for responses
t.responseListener.Stop(0)
// Stop any data streamers
var dsWG sync.WaitGroup
t.dsLock.Lock()
for _, ds := range t.dataStreams {
dsWG.Add(1)
go func(ds *UnixListener) {
defer dsWG.Done()
ds.Stop(0)
}(ds)
}
t.dsLock.Unlock()
dsWG.Wait()
t.status = statusStopped
return
}
// TrackRequest tracks a request. This does not need to be called after using
// ProxyUnix.
func (t *Tracker) TrackRequest(req *Request, timeout time.Duration) error {
t.requestsLock.Lock()
defer t.requestsLock.Unlock()
if t.status == statusStarted {
if _, ok := t.requests[req.ID]; ok {
err := errors.New("request id already traacked")
log.WithFields(log.Fields{
"request": req,
"error": err,
}).Error(err)
return err
}
t.waitgroup.Add(1)
t.requests[req.ID] = req
t.setRequestTimeout(req, timeout)
return nil
}
err := errors.New("failed to track request in unstarted tracker")
log.WithFields(log.Fields{
"request": req,
"trackerStatus": t.status,
"error": err,
}).Error(err)
return err
}
// RemoveRequest should be used to remove a tracked request. Use in cases such
// as sending failures, where there is no hope of a response being received.
func (t *Tracker) RemoveRequest(req *Request) bool {
if r := t.retrieveRequest(req.ID); r != nil {
if r.timeout != nil {
_ = r.timeout.Stop()
}
t.waitgroup.Done()
return true
}
return false
}
// retrieveRequest returns a tracked Request based on ID and stops tracking it.
func (t *Tracker) retrieveRequest(id string) *Request {
t.requestsLock.Lock()
defer t.requestsLock.Unlock()
if req, ok := t.requests[id]; ok {
delete(t.requests, id)
return req
}
return nil
}
func (t *Tracker) setRequestTimeout(req *Request, timeout time.Duration) {
// Fallback to default timeout
if timeout <= 0 {
timeout = t.defaultTimeout
}
resp, err := NewResponse(req, nil, nil, errors.New("response timeout"))
if err != nil {
return
}
req.timeout = time.AfterFunc(timeout, func() {
t.HandleResponse(resp)
})
return
}
// ProxyUnix proxies requests that have response hooks of non-unix sockets
// through one that does. If the response hook is already a unix socket, it
// returns the original request. If not, it tracks the original request and
// returns a new request with a unix socket response hook. The purpose of this
// is so that there can be a single entry and exit point for external
// communication, while local services can reply directly to each other.
func (t *Tracker) ProxyUnix(req *Request, timeout time.Duration) (*Request, error) {
if t.responseListener == nil {
err := errors.New("request tracker's response listener not active")
log.WithFields(log.Fields{
"error": err,
}).Error(err)
return nil, err
}
unixReq := req
if req.ResponseHook.Scheme != "unix" {
unixReq = &Request{
ID: req.ID,
Task: req.Task,
ResponseHook: t.responseListener.URL(),
Args: req.Args,
// Success and ErrorHandler are unnecessary here and intentionally
// omitted.
}
if err := t.TrackRequest(req, timeout); err != nil {
return nil, err
}
req.proxied = true
}
return unixReq, nil
}