-
Notifications
You must be signed in to change notification settings - Fork 4
/
tree_seq.go
128 lines (115 loc) · 3 KB
/
tree_seq.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
/*
Copyright NetFoundry Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package sequencer
import (
"github.com/emirpasic/gods/trees/btree"
"github.com/emirpasic/gods/utils"
"github.com/pkg/errors"
"sync/atomic"
"time"
)
func NewSingleWriterSeq(maxOutOfOrder uint32) Sequencer {
return &singleWriterBtreeSeq{
maxOutOfOrder: int(maxOutOfOrder),
ch: make(chan interface{}, 16),
tree: btree.NewWith(4, utils.UInt32Comparator),
nextSeq: 1,
closeNotify: make(chan struct{}),
}
}
// singleWriterBtreeSeq is a single write, multi reader capable sequencer
type singleWriterBtreeSeq struct {
maxOutOfOrder int
ch chan interface{}
tree *btree.Tree
nextSeq uint32
closed atomic.Bool
closeNotify chan struct{}
}
func (seq *singleWriterBtreeSeq) PutSequenced(itemSeq uint32, val interface{}) error {
if seq.closed.Load() {
return ErrClosed
}
if seq.nextSeq == itemSeq {
if err := seq.enqueue(val); err != nil {
return err
}
for !seq.tree.Empty() {
nextKey := seq.tree.LeftKey().(uint32)
if seq.nextSeq != nextKey {
return nil
}
nextVal := seq.tree.LeftValue()
seq.tree.Remove(nextKey)
if err := seq.enqueue(nextVal); err != nil {
return err
}
}
} else if seq.tree.Size() < seq.maxOutOfOrder {
seq.tree.Put(itemSeq, val)
} else {
return errors.Errorf("exceeded max out of order entries: %v", seq.maxOutOfOrder)
}
return nil
}
func (seq *singleWriterBtreeSeq) enqueue(val interface{}) error {
select {
case seq.ch <- val:
seq.nextSeq++
return nil
case <-seq.closeNotify:
return ErrClosed
}
}
func (seq *singleWriterBtreeSeq) GetNext() interface{} {
select {
case val := <-seq.ch:
return val
case <-seq.closeNotify:
// If we're closed, return any buffered values, otherwise return nil
select {
case val := <-seq.ch:
return val
default:
return nil
}
}
}
func (seq *singleWriterBtreeSeq) GetNextWithDeadline(t time.Time) (interface{}, error) {
if t.IsZero() {
result := seq.GetNext()
if result == nil {
return nil, ErrClosed
}
return result, nil
}
select {
case v := <-seq.ch:
return v, nil
case <-seq.closeNotify:
select {
case val := <-seq.ch:
return val, nil
default:
return nil, ErrClosed
}
case <-time.After(time.Until(t)):
return nil, ErrTimedOut
}
}
// Close should be called if a non-producer threads wants to notify the producer that it should stop producing
func (seq *singleWriterBtreeSeq) Close() {
if seq.closed.CompareAndSwap(false, true) {
close(seq.closeNotify)
}
}