-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.go
184 lines (150 loc) · 3.51 KB
/
queue.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
package dlqdump
import (
"sync"
"sync/atomic"
"github.com/koykov/bitset"
"github.com/koykov/queue"
)
const (
flagTimer = 0
)
// Queue represents dumping queue.
type Queue struct {
bitset.Bitset
// Config instance.
config *Config
// Actual queue status.
status queue.Status
once sync.Once
// Internal timer. Triggers flush operation according Config.FlushInterval.
timer *timer
mux sync.Mutex
buf []byte
Err error
}
// NewQueue makes new dumping queue instance and initialize it according config params.
func NewQueue(config *Config) (*Queue, error) {
q := &Queue{
config: config.Copy(),
}
q.once.Do(q.init)
return q, q.Err
}
// Enqueue puts x to the queue.
func (q *Queue) Enqueue(x any) (err error) {
q.once.Do(q.init)
if status := q.getStatus(); status == queue.StatusClose || status == queue.StatusFail {
return queue.ErrQueueClosed
}
q.mux.Lock()
defer q.mux.Unlock()
// Check job incoming and extract payload.
switch x.(type) {
case queue.Job:
x = x.(queue.Job).Payload
case *queue.Job:
x = x.(*queue.Job).Payload
}
// Encode item to bytes.
q.buf, err = q.c().Encoder.Encode(q.buf[:0], x)
if err != nil {
return
}
// Start timer on first incoming item.
// Timer will trigger flush operation after Config.FlushInterval since current time.
if !q.CheckBit(flagTimer) {
q.SetBit(flagTimer, true)
go q.timer.wait(q)
}
// Forward encoded item to writer.
if _, err = q.c().Writer.Write(q.c().Version, q.buf); err != nil {
q.m().Fail("write fail")
return
}
q.m().Dump(len(q.buf))
q.buf = q.buf[:0]
// Check if Config.Capacity reached.
if q.c().Writer.Size() >= q.c().Capacity {
// Reset timer and flush with corresponding reason.
q.timer.reset()
err = q.flushLF(flushReasonSize)
}
return
}
// Size returns actual size in bytes of all queued items (since start or last flush).
func (q *Queue) Size() int {
if q.c().Writer == nil {
return 0
}
return int(q.c().Writer.Size())
}
// Capacity returns maximum queue capacity.
func (q *Queue) Capacity() int {
return int(q.c().Capacity)
}
// Rate returns size to capacity ratio.
func (q *Queue) Rate() float32 {
return 0
}
// Close gracefully stops the queue.
func (q *Queue) Close() error {
if q.getStatus() == queue.StatusClose {
return queue.ErrQueueClosed
}
if l := q.l(); l != nil {
msg := "caught close signal"
l.Printf(msg)
}
q.mux.Lock()
defer q.mux.Unlock()
q.timer.stop()
return q.flushLF(flushReasonForce)
}
// Init the queue.
func (q *Queue) init() {
c := q.c()
// Check mandatory params.
if c.Capacity == 0 {
q.Err = queue.ErrNoCapacity
q.setStatus(queue.StatusFail)
return
}
if c.Encoder == nil {
q.Err = ErrNoEncoder
q.setStatus(queue.StatusFail)
return
}
if c.Writer == nil {
q.Err = ErrNoWriter
q.setStatus(queue.StatusFail)
return
}
// Check non-mandatory params and set default values if needed.
if c.FlushInterval == 0 {
c.FlushInterval = defaultFlushInterval
}
q.timer = newTimer()
if c.MetricsWriter == nil {
// Use dummy MW.
c.MetricsWriter = DummyMetrics{}
}
// Queue is ready!
q.setStatus(queue.StatusActive)
}
// Set status of the queue.
func (q *Queue) setStatus(status queue.Status) {
atomic.StoreUint32((*uint32)(&q.status), uint32(status))
}
// Get status of the queue.
func (q *Queue) getStatus() queue.Status {
return queue.Status(atomic.LoadUint32((*uint32)(&q.status)))
}
func (q *Queue) c() *Config {
return q.config
}
func (q *Queue) m() MetricsWriter {
return q.config.MetricsWriter
}
func (q *Queue) l() queue.Logger {
return q.config.Logger
}