buffer

package module
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2020 License: MIT Imports: 4 Imported by: 1

README

go-buffer

go-buffer represents a 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 is flushed manually, or automatically when it becomes full or after an interval has elapsed, whichever comes first.

Installation

go get github.com/globocom/go-buffer

Examples

Size-triggered flush
package main

import (
  "time"

  "github.com/globocom/go-buffer"
)

func main() {
  buff := buffer.New(
    // buffer can hold up to 5 items
    buffer.WithSize(5),
    // call this function when the buffer needs flushing
    buffer.WithFlusher(func(items []interface{}) {
      for _, item := range items {
        println(item.(string))
      }
    }),
  )
  // ensure the buffer 
  defer buff.Close()

  buff.Push("item 1")
  buff.Push("item 2")
  buff.Push("item 3")
  buff.Push("item 4")
  buff.Push("item 5")

  // block the current goroutine
  time.Sleep(3*time.Second)

  println("done")
}
Interval-triggered flush
package main

import (
  "time"

  "github.com/globocom/go-buffer"
)

func main() {
  buff := buffer.New(
    // buffer can hold up to 5 items
    buffer.WithSize(5),
    // buffer will be flushed every second, regardless of
    // how many items were pushed
    buffer.WithFlushInterval(time.Second),
    // call this function when the buffer needs flushing
    buffer.WithFlusher(func(items []interface{}) {
      for _, item := range items {
        println(item.(string))
      }
    }),
  )
  defer buff.Close()

  buff.Push("item 1")
  buff.Push("item 2")
  buff.Push("item 3")

  // block the current goroutine
  time.Sleep(3*time.Second)

  println("done")
}
Manual flush
package main

import (
  "time"

  "github.com/globocom/go-buffer"
)

func main() {
  buff := buffer.New(
    // buffer can hold up to 5 items
    buffer.WithSize(5),
    // call this function when the buffer needs flushing
    buffer.WithFlusher(func(items []interface{}) {
      for _, item := range items {
        println(item.(string))
      }
    }),
  )
  defer buff.Close()

  buff.Push("item 1")
  buff.Push("item 2")
  buff.Push("item 3")

  // block the current goroutine
  time.Sleep(3*time.Second)

  buff.Flush()
  println("done")
}

Documentation

Visit Pkg.go.dev for full documentation.

License

MIT License

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTimeout indicates an operation has timed out.
	ErrTimeout = errors.New("operation timed-out")
)

Functions

This section is empty.

Types

type Buffer added in v1.1.0

type Buffer struct {
	io.Closer
	// contains filtered or unexported fields
}

Buffer represents a data buffer that is asynchronously flushed, either manually or automatically.

func New added in v1.1.0

func New(opts ...Option) *Buffer

New creates a new buffer instance with the provided options.

func (*Buffer) Close added in v1.1.0

func (buffer *Buffer) Close() error

Close flushes the buffer and prevents it from being further used. If it succeeds, the buffer cannot be used after it has been closed as all further operations will panic.

func (*Buffer) Flush added in v1.1.0

func (buffer *Buffer) Flush() error

Flush outputs the buffer to a permanent destination. It times out if it cannot be performed in a timely fashion.

func (*Buffer) Push added in v1.1.0

func (buffer *Buffer) Push(item interface{}) error

Push appends an item to the end of the buffer. It times out if it cannot be performed in a timely fashion.

type Flusher added in v1.2.0

type Flusher interface {
	Write(items []interface{})
}

Flusher represents a destination of buffered data.

type FlusherFunc added in v1.2.0

type FlusherFunc func(items []interface{})

FlusherFunc represents a flush function.

func (FlusherFunc) Write added in v1.2.0

func (fn FlusherFunc) Write(items []interface{})

type Option added in v1.1.0

type Option func(*Options)

Option setter.

func WithCloseTimeout added in v1.1.0

func WithCloseTimeout(timeout time.Duration) Option

WithCloseTimeout sets how long

func WithFlushInterval added in v1.2.0

func WithFlushInterval(interval time.Duration) Option

WithFlushInterval sets the interval between automatic flushes.

func WithFlushTimeout added in v1.1.0

func WithFlushTimeout(timeout time.Duration) Option

WithFlushTimeout sets how long a manual flush should wait before giving up.

func WithFlusher added in v1.2.0

func WithFlusher(flusher Flusher) Option

WithFlusher sets the flusher that should be used to write out the buffer.

func WithPushTimeout added in v1.1.0

func WithPushTimeout(timeout time.Duration) Option

WithPushTimeout sets how long a push should wait before giving up.

func WithSize added in v1.2.0

func WithSize(size uint) Option

WithSize sets the size of the buffer.

type Options added in v1.1.0

type Options struct {
	Size          uint
	Flusher       Flusher
	FlushInterval time.Duration
	PushTimeout   time.Duration
	FlushTimeout  time.Duration
	CloseTimeout  time.Duration
}

Configuration options.

Jump to

Keyboard shortcuts

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