-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmercury.go
207 lines (174 loc) · 4.6 KB
/
mercury.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
package mercury
import (
"bufio"
"io"
"sync"
"sync/atomic"
"time"
)
var initiated uint64
var executed uint64
var extended uint64
var cancelled uint64
// Stats holds runtime statistics.
type Stats struct {
Initiated uint64
Executed uint64
Extended uint64
Cancelled uint64
}
// Sub will return the difference of the two stats objects.
func (s Stats) Sub(ss Stats) Stats {
return Stats{
Initiated: s.Initiated - ss.Initiated,
Executed: s.Executed - ss.Executed,
Extended: s.Extended - ss.Extended,
Cancelled: s.Cancelled - ss.Cancelled,
}
}
// GetStats returns runtime statistics.
func GetStats() Stats {
return Stats{
Initiated: atomic.LoadUint64(&initiated),
Executed: atomic.LoadUint64(&executed),
Extended: atomic.LoadUint64(&extended),
Cancelled: atomic.LoadUint64(&cancelled),
}
}
// Writer extends a buffered writer that flushes itself asynchronously. It uses
// a timer to flush the buffered writer if it gets stale. Errors that occur
// during and asynchronous flush are returned on the next call to Write, Flush
// or WriteAndFlush.
type Writer struct {
delay int64
queue int64
writer *bufio.Writer
timer *time.Timer
armed bool
err error
mutex sync.Mutex
}
// NewWriter wraps the provided writer and enables buffering and asynchronous
// flushing using the specified maximum delay.
//
// Note: The delay should not be below 1ms to prevent flushing every write
// asynchronously.
func NewWriter(w io.Writer, maxDelay time.Duration) *Writer {
return newWriter(bufio.NewWriter(w), maxDelay)
}
// NewWriterSize wraps the provided writer and enables buffering and asynchronous
// flushing using the specified maximum delay. This method allows configuration
// of the initial buffer size.
//
// Note: The delay should not be below 1ms to prevent flushing every write
// asynchronously.
func NewWriterSize(w io.Writer, maxDelay time.Duration, size int) *Writer {
return newWriter(bufio.NewWriterSize(w, size), maxDelay)
}
func newWriter(w *bufio.Writer, maxDelay time.Duration) *Writer {
// create writer
writer := &Writer{
writer: w,
delay: int64(maxDelay),
}
// create stopped timer
writer.timer = time.AfterFunc(time.Second, writer.flush)
writer.timer.Stop()
return writer
}
// Write implements the io.Writer interface and writes data to the underlying
// buffered writer and flushes it asynchronously.
func (w *Writer) Write(p []byte) (int, error) {
return w.write(p, false)
}
// Flush flushes the buffered writer immediately.
func (w *Writer) Flush() error {
_, err := w.write(nil, true)
return err
}
// WriteAndFlush writes data to the underlying buffered writer and flushes it
// immediately after writing.
func (w *Writer) WriteAndFlush(p []byte) (int, error) {
return w.write(p, true)
}
// SetMaxDelay can be used to adjust the maximum delay of asynchronous flushes.
//
// Note: The delay should not be below 1ms to prevent flushing every write
// asynchronously.
func (w *Writer) SetMaxDelay(delay time.Duration) {
atomic.StoreInt64(&w.delay, int64(delay))
}
func (w *Writer) write(p []byte, flush bool) (n int, err error) {
// acquire mutex
w.mutex.Lock()
defer w.mutex.Unlock()
// clear and return any error from flush
if w.err != nil {
err = w.err
w.err = nil
return 0, err
}
// write data if available
var flushed bool
if len(p) > 0 {
// get available bytes
a := w.writer.Available()
// write data
n, err = w.writer.Write(p)
if err != nil {
return n, err
}
// a flush happened during the write if more than the available bytes
// have been written
flushed = n > a
}
// get delay
delay := time.Duration(atomic.LoadInt64(&w.delay))
// flush immediately if requested or delay is zero
if flush || delay == 0 {
err = w.writer.Flush()
if err != nil {
return n, err
}
flushed = true
}
// get buffered
buffered := w.writer.Buffered()
// arm timer if data is buffered
if buffered > 0 && !w.armed {
atomic.AddUint64(&initiated, 1)
w.timer.Reset(delay)
w.armed = true
return n, nil
}
// clear timer if no data is buffered and the timer has not yet fired
if buffered == 0 && w.armed {
if w.timer.Stop() {
atomic.AddUint64(&cancelled, 1)
w.armed = false
}
return n, nil
}
// reset timer if data has been flushed and the timer has not yet fired
if flushed && w.armed {
if w.timer.Stop() {
atomic.AddUint64(&extended, 1)
w.timer.Reset(delay)
}
}
return n, nil
}
func (w *Writer) flush() {
// count flush
atomic.AddUint64(&executed, 1)
// acquire mutex
w.mutex.Lock()
defer w.mutex.Unlock()
// set flag
w.armed = false
// flush buffer
err := w.writer.Flush()
if err != nil && w.err == nil {
w.err = err
}
}