buffer

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 28, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

README

go-buffer

go-buffer represents a Generic buffer that asynchronously flushes its contents.

Motivation

Provide generic buffer for user, which is useful for applications that need to aggregate data / write to an external storage before flush it. Compatible with various data types (Container), providing error handling and secure Close.

Features

  • Periodic automatic flush
  • Manually flush(async/sync)
  • Safely Close
  • Error Channel for error handling
  • Generic Support

Install

go get github.com/Kevinello/go-buffer@latest
⚠️ Retracted versions

Versions released erroneously, Please do not install these versions(retracted versions)

  • v0.0.1
  • v0.0.2
  • v0.1.0

Usage

Life Cycle

Project Structure

Project Structure

TODO

  • add test and benchmark

Contribution & Support

Feel free to send a pull request if you consider there's something which can be improved. Also, please open up an issue if you run into a problem when using benchvisual or just have a question.

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

View Source
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

type Buffer[T any] struct {
	Config
	// contains filtered or unexported fields
}

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

func (buffer *Buffer[T]) Close() error

Close would gracefully shut down the buffer.

@param buffer *Buffer[T]
@return Close
@author kevineluo
@update 2023-03-16 11:12:33

func (*Buffer[T]) Flush

func (buffer *Buffer[T]) Flush(async bool) error

Flush manually flush the buffer

@receiver buffer *Buffer
@param async bool
@return error
@author kevineluo
@update 2023-03-27 02:00:56

func (*Buffer[T]) Put

func (buffer *Buffer[T]) Put(data T) error

Put put data into buffer asynchronously

@param buffer *Buffer[T]
@return Put
@author kevineluo
@update 2023-03-15 11:09:25

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

func (*Config) Validate added in v0.1.1

func (config *Config) Validate() (err error)

Validate check config and set default value

@receiver config *Config
@return err error
@author kevineluo
@update 2023-03-15 10:11:28

Directories

Path Synopsis
Package container implementation of some Container interfaces
Package container implementation of some Container interfaces

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL