generated from devnw/oss-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
plex.go
342 lines (296 loc) · 8.54 KB
/
plex.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
340
341
342
// plex is a library for managing net.Conn connection types with minimal effort
// this library utilizes the options pattern to allow for easy configuration and
// customization of the multiplexer. This library provides multiple types which
// are used as wrappers to the configured net.Conn types which allow for general
// streaming of data through channels of bytes as an alternative to the
// more go traditional io.Reader/io.Writer interfaces.
//
// One important caveat is that the Plex library makes no assumptions of which
// net.Conn object is returned from either Reader or Writer. This means that
// messages must be routed by the consumer based on the data in the message
// rather than assuming that a response to a received message will be sent to
// the same connection which initiated the message round trip transmission.
//
// The goal of this library is to abstract the complexity of managing multiple
// connections and their associated channels of data into a single API which
// consumers can request Reader and Writer types from. These types implement
// the io.Closer method which will re-queue the Reader or Writer to the correct
// internal buffer within the multiplexer for another consumer down the line.
package plex
import (
"context"
"net"
"sync"
"time"
)
// TODO: Add support for auto-scaling and self-healing of failed connections
// New creates a net.Conn Multiplexer with the provided options.
//
// CRITICAL: A Multiplexer must be closed by calling the Close method when it
// is no longer needed. Failure to do so may result in goroutines leaking.
//
// NOTE: A single instance of the Multiplexer can ONLY be used to manage a
// set of connections for a SINGLE host.
//
// NOTE: It is important to read the documentation for the options which you
// wish to use to ensure proper configuration of the multiplexer.
func New(ctx context.Context, opts ...Option) (*Multiplexer, error) {
ctx, cancel := _ctx(ctx)
m := &Multiplexer{
ctx: ctx,
cancel: cancel,
}
// Apply mutliplexer options
for _, opt := range opts {
err := opt(m)
if err != nil {
return nil, err
}
}
// Ensure that the capacity for this multiplexer is correct
// for the number of streams it will be handling.
// DEFAULT: 1
capacity := m.capacity
if capacity == 0 && len(m.initconns) == 0 {
capacity = 1
} else if capacity == 0 {
capacity = len(m.initconns)
} else if capacity < len(m.initconns) {
return nil, errTooManyConns
}
// Initialize the internal channels
m.readers = make(chan Conn, capacity)
m.writers = make(chan Conn, capacity)
// Add the valid connections to the multiplexer
// and clean up the init
_, err := m.Add(m.ctx, m.initconns...)
if err != nil {
return nil, err
}
// Ensure proper configuration for auto-scaling
if m.scaleTimeout != nil && m.connector == nil {
return nil, errImproperAutoScalingNilConnector
}
// nil out the init connections
// so that they are not accidentally
// added to the multiplexer again
m.initconns = nil
return m, nil
}
// Multiplexer is the type which manages the multiplexing of net.Conn
// types through the use of channels and goroutines.
type Multiplexer struct {
ctx context.Context
cancel context.CancelFunc
addr net.Addr
readers chan Conn
writers chan Conn
connector Connect
capacity int
scaleTimeout *time.Duration
initconns []Conn
}
// Addr returns the net.Addr of the connections in this instance of the
// multiplexer.
func (m *Multiplexer) Addr() net.Addr {
return m.addr
}
// Close closes the multiplexer and all of the connections it manages.
func (m *Multiplexer) Close() (err error) {
defer func() {
err = recoverErr(err, recover())
}()
m.cancel()
<-m.ctx.Done()
// Kill the streams
m.kill(m.readers)
m.kill(m.writers)
return err
}
// kill drains the internal multiplexer channels and closes the underlying
// connections that it contains
func (m *Multiplexer) kill(conns chan Conn) {
if conns == nil {
return
}
defer func() { _ = recover() }() // ignore any panics
for {
select {
case <-m.ctx.Done():
return
case s, ok := <-conns:
if !ok {
return
}
if s == nil {
continue
}
// TODO: handle error here eventually
_ = s.Close()
}
}
}
// Add adds the provided net.Conn instances to the multiplexer.
//
// NOTE: If the Multiplexer does not have the capacity to add all of the
// provided connections, then the connections that were unable to be added
// will be returned. It is possible the the connection was able to be added
// for a reader or writer, or both but will not indicate which.
//
// TODO: Correct this in the future, we should be able to know if it was
// added to one of them or both.
func (m *Multiplexer) Add(
ctx context.Context,
conns ...Conn,
) (left []Conn, err error) {
defer func() {
err = recoverErr(err, recover())
}()
ctx = merge(m.ctx, ctx)
var i int
for i = 0; i < len(conns); i++ {
c := conns[i]
select {
case <-m.ctx.Done():
return nil, m.ctx.Err()
case <-ctx.Done():
return nil, ctx.Err()
default:
if c == nil {
continue
}
if m.addr == nil {
m.addr = c.RemoteAddr()
} else if c.RemoteAddr().Network() != m.Addr().Network() ||
c.RemoteAddr().String() != m.Addr().String() {
return conns[i:], errAddrMismatch{
Expected: m.addr,
Actual: c.RemoteAddr(),
}
}
left = m.add(ctx, m.readers, c)
left = append(left, m.add(ctx, m.writers, c)...)
}
}
return dedup(left), err
}
// dedup removes any duplicate connections from the provided slice of
// connections and returns the result.
func dedup(in []Conn) (out []Conn) {
m := make(map[Conn]struct{})
for _, c := range in {
m[c] = struct{}{}
}
for c := range m {
out = append(out, c)
}
return out
}
// add adds the provided connection to the provided channel.
// this is separated out so that it can correctly handle channels
// passed to it rather than having extra duplicate code. This also
// adheres to both contexts and breaks down the code a bit.
func (m *Multiplexer) add(
ctx context.Context,
streams chan Conn,
conns ...Conn,
) []Conn {
ctx = merge(m.ctx, ctx)
var i int
// Add as many as possible to the stream
connloop:
for i = 0; i < len(conns) && cap(streams) > len(streams); i++ {
select {
case <-m.ctx.Done():
break connloop
case <-ctx.Done():
break connloop
case streams <- conns[i]:
}
}
return conns[i:]
}
// Reader returns a wrapped net.Conn object as a Reader which provides
// stream capabilities to the net.Conn as well as cuts down on the available
// methods on the net.Conn. This is done on purpose to ensure that the
// consumer is less likely to misuse the Reader when requesting one for the
// purpose of reading.
// nolint:dupl
func (m *Multiplexer) Reader(
ctx context.Context,
timeout *time.Duration,
) (Reader, error) {
ctx = merge(m.ctx, ctx)
var tchan <-chan time.Time
if timeout != nil {
timer := time.NewTimer(*timeout)
defer timer.Stop()
tchan = timer.C
}
select {
case <-m.ctx.Done():
return nil, m.ctx.Err()
case <-ctx.Done():
return nil, ctx.Err()
case <-tchan:
return nil, errTimeout
case conn, ok := <-m.readers:
if !ok {
return nil, errClosed
}
rctx, rcancel := _ctx(ctx)
return &readStream{
conn, // Conn
rctx, // context
rcancel, // cancel
sync.WaitGroup{}, // wg
sync.Mutex{}, // mutex
// cleanup
func() { // TODO: this may need to take the conn for self-heal
_ = m.add(m.ctx, m.readers, conn)
},
}, nil
}
}
// Writer returns a wrapped net.Conn object as a Writer which provides
// stream capabilities to the net.Conn as well as cuts down on the available
// methods on the net.Conn. This is done on purpose to ensure that the
// consumer is less likely to misuse the Writer when requesting one for the
// purpose of writing.
// nolint:dupl
func (m *Multiplexer) Writer(
ctx context.Context,
timeout *time.Duration,
) (Writer, error) {
ctx = merge(m.ctx, ctx)
var tchan <-chan time.Time
if timeout != nil {
timer := time.NewTimer(*timeout)
defer timer.Stop()
tchan = timer.C
}
select {
case <-m.ctx.Done():
return nil, m.ctx.Err()
case <-ctx.Done():
return nil, ctx.Err()
case <-tchan:
return nil, errTimeout
case conn, ok := <-m.writers:
if !ok {
return nil, errClosed
}
wctx, wcancel := _ctx(ctx)
return &writeStream{
conn, // Conn
wctx, // context
wcancel, // cancel
sync.WaitGroup{}, // wg
sync.Mutex{}, // mutex
// cleanup
func() { // TODO: this may need to take the conn for self-heal
_ = m.add(m.ctx, m.writers, conn)
},
}, nil
}
}