Documentation
¶
Overview ¶
Example ¶
package main import ( "fmt" "time" buffer "github.com/woorui/async-buffer" ) // pp implements Flusher interface type pp struct{} func (p *pp) Flush(strs []string) error { return print(strs) } func print(strs []string) error { for _, s := range strs { fmt.Printf("%s ", s) } return nil } func main() { // can also call buffer.FlushFunc` to adapt a function to Flusher buf := buffer.New[string](&pp{}, buffer.Option[string]{ Threshold: 1, FlushInterval: 3 * time.Second, WriteTimeout: time.Second, FlushTimeout: time.Second, ErrHandler: func(err error, t []string) { fmt.Printf("err: %v, ele: %v", err, t) }, }) // data maybe loss if Close() is not be called defer buf.Close() buf.Write("a", "b", "c", "d", "e", "f") }
Output: a b c d e f
Index ¶
Examples ¶
Constants ¶
View Source
const DefaultDataBackupSize = 128
DefaultDataBackupSize is the default size of buffer data backup
Variables ¶
View Source
var ( // ErrClosed represents a closed buffer ErrClosed = errors.New("async-buffer: buffer is closed") // ErrWriteTimeout returned if write timeout ErrWriteTimeout = errors.New("async-buffer: write timeout") // ErrFlushTimeout returned if flush timeout ErrFlushTimeout = errors.New("async-buffer: flush timeout") )
Functions ¶
func DefaultErrHandler ¶ added in v0.0.3
DefaultErrHandler prints error and the size of elements to stderr.
Types ¶
type Buffer ¶
type Buffer[T any] struct { // contains filtered or unexported fields }
Buffer represents an async buffer.
The Buffer automatically flush data within a cycle flushing is also triggered when the data reaches the specified threshold.
If both Threshold and FlushInterval are setted to zero, Writing is Flushing.
You can also flush data manually by calling `Flush`.
type FlushFunc ¶ added in v0.0.2
The FlushFunc is an adapter to allow the use of ordinary functions as a Flusher. FlushFunc(f) is a Flusher that calls f.
type Option ¶ added in v0.0.3
type Option[T any] struct { // Threshold indicates that the buffer is large enough to trigger flushing, // if Threshold is zero, do not judge threshold. Threshold uint32 // WriteTimeout set write timeout, set to zero if a negative, zero means no timeout. WriteTimeout time.Duration // FlushTimeout flush timeout, set to zero if a negative, zero means no timeout. FlushTimeout time.Duration // FlushInterval indicates the interval between automatic flushes, set to zero if a negative. // There is automatic flushing if zero FlushInterval. FlushInterval time.Duration // ErrHandler handles errors, print error and the size of elements to stderr in default. ErrHandler func(err error, elements []T) }
Option for New the buffer.
If both Threshold and FlushInterval are set to zero, Writing is Flushing.
Click to show internal directories.
Click to hide internal directories.