Documentation ¶
Overview ¶
Package buffer represents a Generic buffer that asynchronously flushes its contents. It is useful for applications that need to aggregate data before writing it to an external storage. A buffer can be flushed manually, or automatically when it becomes full or after an interval has elapsed, whichever comes first.
@update 2023-03-15 10:40:31
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrClosed indicates the buffer is closed and can no longer be used. ErrClosed = errors.New("buffer is closed") )
Functions ¶
This section is empty.
Types ¶
type Buffer ¶
Buffer is a lock-free buffer It would start a background goroutine continuously consume data from channel and write to container When container is "full", it would asynchronously flush data on container by default
@author kevineluo @update 2023-03-15 09:40:02
func NewBuffer ¶
func NewBuffer[T any](container container.Container[T], config Config) (buffer *Buffer[T], errChan <-chan error, err error)
NewBuffer creates a buffer in type `T`, and start handling data return the buffer and a error channel for user to handle error from putting data and flushing data
@param container Container[T] @param config Config @return buffer *Buffer[T] @return errChan <-chan error @return err error @author kevineluo @update 2023-03-15 10:38:29
func (*Buffer[T]) Close ¶
Close would gracefully shut down the buffer.
@param buffer *Buffer[T] @return Close @author kevineluo @update 2023-03-16 11:12:33
type Config ¶
type Config struct { ID string // buffer identify ID ChanBufSize int // lock free channel size(when data in channel reach chanBufSize, it will block in Buffer.Put) DisableAutoFlush bool // whether disable automate flush FlushInterval time.Duration // automate flush data every [flushInterval] duration SyncAutoFlush bool // determine the buffer will automate flush asynchronously or synchronously, default is false -- async flush Logger *logr.Logger // third-part logger implement logr.LogSinker, default using zapr.Logger LogLevel int // used when Config.logger is nil, follow the zap style level(https://pkg.go.dev/go.uber.org/zap@v1.24.0/zapcore#Level), setting the log level for zapr.Logger(config.logLevel should be in range[-1, 5], default is 0 -- InfoLevel) }
Config Buffer Config
@author kevineluo @update 2023-03-15 09:31:54