20
20
ErrFlushTimeout = errors .New ("async-buffer: flush timeout" )
21
21
)
22
22
23
- // Flusher hold FlushFunc, Flusher tell Buffer how to flush data.
23
+ // Flusher holds FlushFunc, Flusher tell Buffer how to flush data.
24
24
type Flusher [T any ] interface {
25
25
Flush (elements []T ) error
26
26
}
@@ -29,12 +29,12 @@ type Flusher[T any] interface {
29
29
// as a Flusher. FlushFunc(f) is a Flusher that calls f.
30
30
type FlushFunc [T any ] func (elements []T ) error
31
31
32
- // Flush calls f(ctx,m)
32
+ // Flush calls FlushFunc itself.
33
33
func (f FlushFunc [T ]) Flush (elements []T ) error {
34
34
return f (elements )
35
35
}
36
36
37
- // DefaultErrHandler.
37
+ // DefaultErrHandler prints error and the size of elements to stderr .
38
38
func DefaultErrHandler [T any ](err error , flat []T ) {
39
39
fmt .Fprintf (
40
40
os .Stderr ,
@@ -55,7 +55,7 @@ type Option[T any] struct {
55
55
// FlushInterval indicates the interval between automatic flushes, set to zero if a negative.
56
56
// There is automatic flushing if zero FlushInterval.
57
57
FlushInterval time.Duration
58
- // ErrHandler handles errors, print error and elements size to stderr in default.
58
+ // ErrHandler handles errors, print error and the size of elements to stderr in default.
59
59
ErrHandler func (error , []T )
60
60
}
61
61
@@ -64,9 +64,9 @@ type Option[T any] struct {
64
64
// The Buffer automatically flush data within a cycle
65
65
// flushing is also triggered when the data reaches the specified threshold.
66
66
//
67
- // If both Threshold and FlushInterval are set to zero, Writing is Flushing.
67
+ // If both Threshold and FlushInterval are setted to zero, Writing is Flushing.
68
68
//
69
- // You can also flush data manually.
69
+ // You can also flush data manually by calling `Flush` .
70
70
type Buffer [T any ] struct {
71
71
ctx context.Context // ctx controls the lifecycle of Buffer
72
72
cancel context.CancelFunc // cancel is used to stop Buffer flushing
@@ -75,15 +75,11 @@ type Buffer[T any] struct {
75
75
tickerC <- chan time.Time // tickerC flushs datas, when tickerC is nil, Buffer do not timed flushing
76
76
tickerStop func () // tickerStop stop the ticker
77
77
option Option [T ] // options
78
- flusher Flusher [T ] // Flusher is the Flusher that flushes outputs the buffer to a permanent destination.
79
- done chan struct {}
78
+ flusher Flusher [T ] // Flusher is the Flusher that flushes outputs the buffer to a permanent destination
79
+ done chan struct {} // done ensures internal `run` function exit
80
80
}
81
81
82
82
// New returns the async buffer based on option
83
- //
84
- // error returned is an error channel that holds errors generated during the flush process.
85
- // You can subscribe to this channel if you want handle flush errors.
86
- // using `se := new(buffer.ErrFlush[T]); errors.As(err, &se)` to get elements that not be flushed.
87
83
func New [T any ](flusher Flusher [T ], option Option [T ]) * Buffer [T ] {
88
84
ctx , cancel := context .WithCancel (context .Background ())
89
85
@@ -179,7 +175,7 @@ func (b *Buffer[T]) writeDirect(elements []T) (int, error) {
179
175
return n , nil
180
176
}
181
177
182
- // run do flushing in the background and send error to error channel
178
+ // run do flushing in the background.
183
179
func (b * Buffer [T ]) run () {
184
180
flat := make ([]T , 0 , b .option .Threshold )
185
181
0 commit comments