/
TimedContentConsumer.go
305 lines (275 loc) · 7.92 KB
/
TimedContentConsumer.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
//Package contentconsumer - Consumes content from channel. (e.g.) TimedContentConsumer
package contentconsumer
import (
"container/ring"
"context"
"errors"
"fmt"
"runtime"
"sync"
"time"
)
//TimedContent - Content with duration
// duration - content duration
// data - actual content
type TimedContent struct {
duration time.Duration
data interface{} //any data
}
//TimedContentPtr - Ptr to the TimedContext
type TimedContentPtr *TimedContent
//NewTimedContent - Create new TimedContent
func NewTimedContent(duration time.Duration, data interface{}) TimedContentPtr {
ret := new(TimedContent)
ret.duration = duration
ret.data = data
return ret
}
//ErrConsumerDrainedEmpty - Empty Consumer Buffer
var ErrConsumerDrainedEmpty error = errors.New("EmptyBuffer")
// ErrConsumerChannelClosed - AddChunk to closed channel
var ErrConsumerChannelClosed error = errors.New("ChannelClosed")
//TimedContentConsumer -
// * Maintains a Cache of given ConsumptionInterval
// * Channel may have more
// * Drains the Cache in fixed intervals
// * While Draining can forward data using PostDataFunc(duration, data)
// * If CacheBuffer below Min Level - calls BelowMinLevelFunc()
type TimedContentConsumer struct {
//EXTERNAL:
//id - User name for the TimedContentConsumer
// used in the logging
ID string
//Interval to drain buffer for content worth the interval
ConsumptionInterval time.Duration
//downstream Consumer of content drained
Downstream chan<- interface{}
//downstream Consumer of events
EventDownstream chan<- ConsumerEventPtr
//INTERNAL:
//inputChan - Channel where Timed Content is fed to the consumer
inputChan chan TimedContentPtr
//inChannelOpen - holds tate of if inputChan is open, for safety
inChannelOpen bool
//chanMutex - mutex to stop channel close race
inputChanMutex sync.Mutex
//buffer - Ring buffer to hold the content worth
buffer *ring.Ring
//Mutex gaurding the buffer
buffersLock sync.Mutex
//Reference to the buffer for writing -> Place to write next item
writer *ring.Ring
//Reference to the buffer for reading -> Place to read next item
reader *ring.Ring
//Amount of content in buffer
bufferAvailPeriod time.Duration
//Time on wall clock, last time buffer was drained
lastTimeEval time.Time
}
//EXTERNAL
//NewTimedContentConsumer - Create
// id - User Name
// interval - Duration of poll
// slots - intial ring buffer size estimate content to fill
// No worries it will be doubled if required
func NewTimedContentConsumer(id string, interval time.Duration, slots int) *TimedContentConsumer {
ret := &TimedContentConsumer{}
ret.ID = id
ret.inputChan = make(chan TimedContentPtr, slots)
ret.inChannelOpen = true
//Minimum 10 slots
if slots < 10 {
slots = 10
}
ret.buffer = ring.New(slots)
ret.writer = ret.buffer //Current write location.. Write() and Next()
ret.reader = ret.buffer.Prev() //Last Read location. Next() and Read()
ret.ConsumptionInterval = interval
ret.Downstream = nil
ret.EventDownstream = nil
return ret
}
//Channel - Returns the channel to write to
func (cc *TimedContentConsumer) Channel() chan<- TimedContentPtr {
cc.inputChanMutex.Lock()
defer cc.inputChanMutex.Unlock()
if cc.inChannelOpen {
return cc.inputChan
}
return nil
}
//CloseChannel - Close the input channel
// This will make the Go Routine Run exit
func (cc *TimedContentConsumer) CloseChannel() {
cc.inputChanMutex.Lock()
defer cc.inputChanMutex.Unlock()
if cc.inChannelOpen {
close(cc.inputChan)
cc.inChannelOpen = false
}
}
//INTERNAL
//addChunk - Add a incoming Chunk
func (cc *TimedContentConsumer) addChunk(c TimedContentPtr) error {
cc.buffersLock.Lock()
defer cc.buffersLock.Unlock()
if c == nil || c.data == nil {
return ErrConsumerChannelClosed
}
//If current writer position is not empty
//and reader is just behind to read
if cc.writer.Value != nil && cc.writer == cc.reader.Next() {
//log.Printf("Extending Ring Start %v %v", cc.writer, cc.writer.Len())
//log.Printf("WRITER VIEW")
//cc.printRingBuf(cc.writer)
//log.Printf("READER VIEW")
//cc.printRingBuf(cc.reader.Next())
//Ring is full.. Need to extend
extendRing := ring.New(cc.buffer.Len())
//log.Printf("EXTENDED VIEW")
//cc.printRingBuf(extendRing)
cc.reader = cc.reader.Link(extendRing).Prev()
cc.writer = extendRing
//log.Printf("WRITER VIEW")
//cc.printRingBuf(cc.writer)
//log.Printf("READER VIEW")
//cc.printRingBuf(cc.reader.Next())
}
cc.writer.Value = c
cc.writer = cc.writer.Next()
cc.bufferAvailPeriod += c.duration
return nil
}
//consumeChunks - Consume Chunks for Elapsed Duration
//elapsed - Actual Time worth of content required
//Return:
// remain elapsed - Amount of content not given
// error - If content is not available
func (cc *TimedContentConsumer) consumeChunks(elapsed time.Duration) (time.Duration, error) {
cc.buffersLock.Lock()
defer cc.buffersLock.Unlock()
for {
if cc.reader.Next() == cc.writer {
//Nothing to read
//log.Printf("Noting READ... ")
return elapsed, ErrConsumerDrainedEmpty
}
//Step forward for reading
cc.reader = cc.reader.Next()
if cc.reader.Value == nil {
//Should not happen... bad types
panic("Bad Data in Ring Buffer.")
}
var chunk TimedContentPtr
chunk = cc.reader.Value.(TimedContentPtr)
if chunk == nil {
//Should not happen... bad type
panic("Bad Data in Ring Buffer.")
}
if chunk.duration < elapsed {
//chunk fully consumed
cc.reader.Value = nil
elapsed = elapsed - chunk.duration
cc.bufferAvailPeriod -= chunk.duration
if cc.Downstream != nil {
cc.Downstream <- chunk.data
//log.Printf("SENT %v %v", chunk.data, chunk.duration)
}
continue //next chunk also required
} else if chunk.duration == elapsed {
//chunk fully consumed
cc.reader.Value = nil
cc.bufferAvailPeriod -= chunk.duration
elapsed = 0
if cc.Downstream != nil {
cc.Downstream <- chunk.data
//log.Printf("SENT %v %v", chunk.data, chunk.duration)
}
break //All data read fully... exit
} else {
//elapsed is < chunkDur
//Don't consume the chunk...
//Retain reading to same buffer
//Step back for re-read
cc.reader = cc.reader.Prev()
break
}
}
return elapsed, nil
}
//DrainChunks - Drain the actual content
func (cc *TimedContentConsumer) DrainChunks(ctx context.Context, wg *sync.WaitGroup) {
if wg != nil {
wg.Add(1)
defer wg.Done()
}
defer func() {
if cc.Downstream != nil {
close(cc.Downstream)
cc.Downstream = nil
//log.Printf("Closed Downstream")
}
if cc.EventDownstream != nil {
close(cc.EventDownstream)
cc.EventDownstream = nil
//log.Printf("Closed EventDownstream")
}
}()
var remain time.Duration
var err error
for {
select {
case <-ctx.Done():
//Context is cancelled ... exit..
return
case <-time.After(cc.ConsumptionInterval):
curTime := time.Now()
elapsed := curTime.Sub(cc.lastTimeEval)
if remain > 0 {
elapsed = elapsed + remain
remain = 0
}
cc.lastTimeEval = curTime
remain, err = cc.consumeChunks(elapsed)
if err != nil {
if cc.EventDownstream != nil {
cc.EventDownstream <- &ConsumerEvent{time.Now(), cc.ID, fmt.Errorf("Elapsed %v: %w", remain, err)}
//log.Printf("ERR SENT %v %v %v", cc.ID, remain, err)
}
}
runtime.Gosched()
}
}
}
//Run - Start Consuming Content
func (cc *TimedContentConsumer) Run(wg *sync.WaitGroup) {
if wg != nil {
defer wg.Done()
}
//Create a cancellable concext for all related stuff to stop at end of this
ctx, cancelfunc := context.WithCancel(context.Background())
defer cancelfunc()
drainChunksStarted := false
for {
select {
case chunk, channelOpen := <-cc.inputChan:
if chunk != nil {
//add chunk
cc.addChunk(chunk)
if !drainChunksStarted {
//On first chunk arrival start draining
cc.lastTimeEval = time.Now()
go cc.DrainChunks(ctx, wg)
drainChunksStarted = true
}
runtime.Gosched()
} else {
//Error in channel
if !channelOpen {
//Channel closed
return
}
}
}
}
}