This repository has been archived by the owner on Oct 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
source.go
412 lines (374 loc) · 10.2 KB
/
source.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package marshaled
import (
"fmt"
"io"
"log"
"sort"
"strings"
"sync"
"time"
"github.com/uber-go/gwr/source"
)
// NOTE: This approach is perhaps overfit to the json module's marshalling
// mindset. A better interface (for performance) would work by passing a
// writer to the specific encoder, rather than a []byte-returning Marshal
// function. This would be possible perhaps using something like
// io.MultiWriter.
// DataSource wraps a format-agnostic data source and provides one or
// more formats for it.
//
// DataSource implements:
// - DataSource to satisfy DataSources and low level protocols
// - ItemDataSource so that higher level protocols may add their own framing
// - GenericDataWatcher inwardly to the wrapped GenericDataSource
type DataSource struct {
// TODO: better to have alternate implementations for each combination
// rather than one with these nil checks
source source.GenericDataSource
getSource source.GetableDataSource
watchSource source.WatchableDataSource
watiSource source.WatchInitableDataSource
actiSource source.ActivateWatchableDataSource
formats map[string]source.GenericDataFormat
formatNames []string
maxItems int
maxBatches int
maxWait time.Duration
procs sync.WaitGroup
watchLock sync.RWMutex
watchers map[string]*marshaledWatcher
active bool
itemChan chan interface{}
itemsChan chan []interface{}
}
func stringIt(item interface{}) ([]byte, error) {
var s string
if ss, ok := item.(fmt.Stringer); ok {
s = ss.String()
} else {
s = fmt.Sprintf("%+v", item)
}
return []byte(s), nil
}
// NewDataSource creates a DataSource for a given format-agnostic data source
// and a map of marshalers
func NewDataSource(
src source.GenericDataSource,
formats map[string]source.GenericDataFormat,
) *DataSource {
if formats == nil {
formats = make(map[string]source.GenericDataFormat)
}
// source-defined formats
if fmtsrc, ok := src.(source.GenericDataSourceFormats); ok {
fmts := fmtsrc.Formats()
for name, fmt := range fmts {
formats[name] = fmt
}
}
// standard json protocol
if formats["json"] == nil {
formats["json"] = LDJSONMarshal
}
// convenience templated text protocol
if formats["text"] == nil {
if txtsrc, ok := src.(source.TextTemplatedSource); ok {
if tt := txtsrc.TextTemplate(); tt != nil {
formats["text"] = NewTemplatedMarshal(tt)
}
}
}
// default to just string-ing it
if formats["text"] == nil {
formats["text"] = source.GenericDataFormatFunc(stringIt)
}
ds := &DataSource{
source: src,
formats: formats,
watchers: make(map[string]*marshaledWatcher, len(formats)),
// TODO: tunable
maxItems: 100,
maxBatches: 100,
maxWait: 100 * time.Microsecond,
}
ds.getSource, _ = src.(source.GetableDataSource)
ds.watchSource, _ = src.(source.WatchableDataSource)
ds.watiSource, _ = src.(source.WatchInitableDataSource)
ds.actiSource, _ = src.(source.ActivateWatchableDataSource)
for name, format := range formats {
ds.formatNames = append(ds.formatNames, name)
ds.watchers[name] = newMarshaledWatcher(ds, format)
}
sort.Strings(ds.formatNames)
if ds.watchSource != nil {
ds.watchSource.SetWatcher(ds)
}
return ds
}
// Active returns true if there are any active watchers, false otherwise. If
// Active returns false, so will any calls to HandleItem and HandleItems.
func (mds *DataSource) Active() bool {
mds.watchLock.RLock()
r := mds.active && mds.itemChan != nil && mds.itemsChan != nil
mds.watchLock.RUnlock()
return r
}
// Name passes through the GenericDataSource.Name()
func (mds *DataSource) Name() string {
return mds.source.Name()
}
// Formats returns the list of supported format names.
func (mds *DataSource) Formats() []string {
return mds.formatNames
}
// Attrs returns arbitrary description information about the data source.
func (mds *DataSource) Attrs() map[string]interface{} {
// TODO: support per-format Attrs?
// TODO: any support for per-source Attrs?
return nil
}
// Get marshals data source's Get data to the writer
func (mds *DataSource) Get(formatName string, w io.Writer) error {
if mds.getSource == nil {
return source.ErrNotGetable
}
format, ok := mds.formats[strings.ToLower(formatName)]
if !ok {
return source.ErrUnsupportedFormat
}
data := mds.getSource.Get()
buf, err := format.MarshalGet(data)
if err != nil {
log.Printf("get marshaling error %v", err)
return err
}
_, err = w.Write(buf)
return err
}
// Watch marshals any data source GetInit data to the writer, and then
// retains a reference to the writer so that any future agnostic data source
// Watch(emit)'ed data gets marshaled to it as well
func (mds *DataSource) Watch(formatName string, w io.Writer) error {
if mds.watchSource == nil {
return source.ErrNotWatchable
}
mds.watchLock.Lock()
acted := !mds.active
err := func() error {
defer mds.watchLock.Unlock()
watcher, ok := mds.watchers[strings.ToLower(formatName)]
if !ok {
return source.ErrUnsupportedFormat
}
if err := watcher.init(w); err != nil {
return err
}
if err := mds.startWatching(); err != nil {
return err
}
return nil
}()
if err == nil && acted && mds.actiSource != nil {
mds.actiSource.Activate()
}
return err
}
// WatchItems marshals any data source GetInit data as a single item to the
// ItemWatcher's HandleItem method. The watcher is then retained and future
// items are marshaled to its HandleItem method.
func (mds *DataSource) WatchItems(formatName string, iw source.ItemWatcher) error {
if mds.watchSource == nil {
return source.ErrNotWatchable
}
mds.watchLock.Lock()
acted := !mds.active
err := func() error {
defer mds.watchLock.Unlock()
watcher, ok := mds.watchers[strings.ToLower(formatName)]
if !ok {
return source.ErrUnsupportedFormat
}
if err := watcher.initItems(iw); err != nil {
return err
}
if err := mds.startWatching(); err != nil {
return err
}
return nil
}()
if err == nil && acted && mds.actiSource != nil {
mds.actiSource.Activate()
}
return err
}
// startWatching flips the active bit, creates new item channels, and starts a
// processing go routine; it assumes that the watchLock is being held by the
// caller.
func (mds *DataSource) startWatching() error {
// TODO: we could optimize the only-one-format-being-watched case
if mds.active {
return nil
}
mds.active = true
mds.itemChan = make(chan interface{}, mds.maxItems)
mds.itemsChan = make(chan []interface{}, mds.maxBatches)
mds.procs.Add(1)
go mds.processItemChan(mds.itemChan, mds.itemsChan)
return nil
}
// Drain closes the item channels, and waits for the item processor to finish.
// After drain, any remaining watchers are closed, and the source goes
// inactive.
func (mds *DataSource) Drain() {
mds.watchLock.Lock()
any := false
if mds.itemChan != nil {
close(mds.itemChan)
any = true
mds.itemChan = nil
}
if mds.itemsChan != nil {
close(mds.itemsChan)
any = true
mds.itemsChan = nil
}
if any {
mds.watchLock.Unlock()
mds.procs.Wait()
mds.watchLock.Lock()
}
stop := mds.active
if stop {
mds.active = false
}
mds.watchLock.Unlock()
if stop {
for _, watcher := range mds.watchers {
watcher.Close()
}
}
}
func (mds *DataSource) processItemChan(itemChan chan interface{}, itemsChan chan []interface{}) {
defer mds.procs.Done()
stop := false
for !stop && (itemChan != nil || itemsChan != nil) {
mds.watchLock.RLock()
active := mds.active
watchers := mds.watchers
mds.watchLock.RUnlock()
if !active {
break
}
select {
case item, ok := <-itemChan:
if !ok {
itemChan = nil
continue
}
any := false
for _, watcher := range watchers {
if watcher.emit(item) {
any = true
}
}
if !any {
stop = true
}
case items, ok := <-itemsChan:
if !ok {
itemsChan = nil
continue
}
any := false
for _, watcher := range watchers {
if watcher.emitBatch(items) {
any = true
}
}
if !any {
stop = true
}
}
}
mds.watchLock.Lock()
if mds.itemChan == itemChan {
mds.itemChan = nil
}
if mds.itemsChan == itemsChan {
mds.itemsChan = nil
}
if stop {
mds.active = false
}
mds.watchLock.Unlock()
if stop {
for _, watcher := range mds.watchers {
watcher.Close()
}
}
}
// HandleItem implements GenericDataWatcher.HandleItem by passing the item to
// all current marshaledWatchers.
func (mds *DataSource) HandleItem(item interface{}) bool {
if !mds.Active() {
return false
}
select {
case mds.itemChan <- item:
return true
case <-time.After(mds.maxWait):
mds.watchLock.Lock()
if !mds.active {
mds.watchLock.Unlock()
return false
}
mds.active = false
mds.watchLock.Unlock()
for _, watcher := range mds.watchers {
watcher.Close()
}
return false
}
}
// HandleItems implements GenericDataWatcher.HandleItems by passing the batch
// to all current marshaledWatchers.
func (mds *DataSource) HandleItems(items []interface{}) bool {
if !mds.Active() {
return false
}
select {
case mds.itemsChan <- items:
return true
case <-time.After(mds.maxWait):
mds.watchLock.Lock()
if !mds.active {
mds.watchLock.Unlock()
return false
}
mds.active = false
mds.watchLock.Unlock()
for _, watcher := range mds.watchers {
watcher.Close()
}
return false
}
}