-
Notifications
You must be signed in to change notification settings - Fork 202
/
Copy pathasync_processor.go
58 lines (47 loc) · 1015 Bytes
/
async_processor.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
package gortsplib
import (
"github.com/bluenviron/gortsplib/v4/pkg/ringbuffer"
)
// this is an asynchronous queue processor
// that allows to detach the routine that is reading a stream
// from the routine that is writing a stream.
type asyncProcessor struct {
bufferSize int
running bool
buffer *ringbuffer.RingBuffer
stopError error
chStopped chan struct{}
}
func (w *asyncProcessor) initialize() {
w.buffer, _ = ringbuffer.New(uint64(w.bufferSize))
}
func (w *asyncProcessor) close() {
if w.running {
w.buffer.Close()
<-w.chStopped
}
}
func (w *asyncProcessor) start() {
w.running = true
w.chStopped = make(chan struct{})
go w.run()
}
func (w *asyncProcessor) run() {
w.stopError = w.runInner()
close(w.chStopped)
}
func (w *asyncProcessor) runInner() error {
for {
tmp, ok := w.buffer.Pull()
if !ok {
return nil
}
err := tmp.(func() error)()
if err != nil {
return err
}
}
}
func (w *asyncProcessor) push(cb func() error) bool {
return w.buffer.Push(cb)
}