-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
stablerunner.go
105 lines (91 loc) · 2.32 KB
/
stablerunner.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
package threading
import (
"errors"
"runtime"
"sync"
"sync/atomic"
)
const factor = 10
var (
ErrRunnerClosed = errors.New("runner closed")
bufSize = runtime.NumCPU() * factor
)
// StableRunner is a runner that guarantees messages are taken out with the pushed order.
// This runner is typically useful for Kafka consumers with parallel processing.
type StableRunner[I, O any] struct {
handle func(I) O
consumedIndex uint64
writtenIndex uint64
ring []*struct {
value chan O
lock sync.Mutex
}
runner *TaskRunner
done chan struct{}
}
// NewStableRunner returns a new StableRunner with given message processor fn.
func NewStableRunner[I, O any](fn func(I) O) *StableRunner[I, O] {
ring := make([]*struct {
value chan O
lock sync.Mutex
}, bufSize)
for i := 0; i < bufSize; i++ {
ring[i] = &struct {
value chan O
lock sync.Mutex
}{
value: make(chan O, 1),
}
}
return &StableRunner[I, O]{
handle: fn,
ring: ring,
runner: NewTaskRunner(runtime.NumCPU()),
done: make(chan struct{}),
}
}
// Get returns the next processed message in order.
// This method should be called in one goroutine.
func (r *StableRunner[I, O]) Get() (O, error) {
defer atomic.AddUint64(&r.consumedIndex, 1)
index := atomic.LoadUint64(&r.consumedIndex)
offset := index % uint64(bufSize)
holder := r.ring[offset]
select {
case o := <-holder.value:
return o, nil
case <-r.done:
if atomic.LoadUint64(&r.consumedIndex) < atomic.LoadUint64(&r.writtenIndex) {
return <-holder.value, nil
}
var o O
return o, ErrRunnerClosed
}
}
// Push pushes the message v into the runner and to be processed concurrently,
// after processed, it will be cached to let caller take it in pushing order.
func (r *StableRunner[I, O]) Push(v I) error {
select {
case <-r.done:
return ErrRunnerClosed
default:
index := atomic.AddUint64(&r.writtenIndex, 1)
offset := (index - 1) % uint64(bufSize)
holder := r.ring[offset]
holder.lock.Lock()
r.runner.Schedule(func() {
defer holder.lock.Unlock()
o := r.handle(v)
holder.value <- o
})
return nil
}
}
// Wait waits all the messages to be processed and taken from inner buffer.
func (r *StableRunner[I, O]) Wait() {
close(r.done)
r.runner.Wait()
for atomic.LoadUint64(&r.consumedIndex) < atomic.LoadUint64(&r.writtenIndex) {
runtime.Gosched()
}
}