Skip to content

globocom/go-buffer

Repository files navigation

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 

Go < 1.18:

go get github.com/globocom/go-buffer@v2 

Examples

Note

For v2, see Examples v2.

Size-triggered flush

package main import ( "time""github.com/globocom/go-buffer/v3" ) funcmain(){buff:=buffer.New( // call this function when the buffer needs flushingfunc(items []string){for_, item:=rangeitems{println(string) } }, // buffer can hold up to 5 itemsbuffer.WithSize(5), ) // ensure the bufferdeferbuff.Close() buff.Push("item 1") buff.Push("item 2") buff.Push("item 3") buff.Push("item 4") buff.Push("item 5") // block the current goroutinetime.Sleep(3*time.Second) println("done") }

Interval-triggered flush

package main import ( "time""github.com/globocom/go-buffer/v3" ) funcmain(){buff:=buffer.New( // call this function when the buffer needs flushingfunc(items []string){for_, item:=rangeitems{println(item) } }, // buffer can hold up to 5 itemsbuffer.WithSize(5), // buffer will be flushed every second, regardless of// how many items were pushedbuffer.WithFlushInterval(time.Second), ) deferbuff.Close() buff.Push("item 1") buff.Push("item 2") buff.Push("item 3") // block the current goroutinetime.Sleep(3*time.Second) println("done") }

Manual flush

package main import ( "time""github.com/globocom/go-buffer/v3" ) funcmain(){buff:=buffer.New( // call this function when the buffer needs flushingfunc(items []string){for_, item:=rangeitems{println(item) } }, // buffer can hold up to 5 itemsbuffer.WithSize(5), ) deferbuff.Close() buff.Push("item 1") buff.Push("item 2") buff.Push("item 3") // block the current goroutinetime.Sleep(3*time.Second) buff.Flush() println("done") }

Examples v2

Size-triggered flush

package main import ( "time""github.com/globocom/go-buffer/v2" ) funcmain(){buff:=buffer.New( // buffer can hold up to 5 itemsbuffer.WithSize(5), // call this function when the buffer needs flushingbuffer.WithFlusher(buffer.FlusherFunc(func(items []interface{}){for_, item:=rangeitems{println(item.(string)) } })), ) // ensure the bufferdeferbuff.Close() buff.Push("item 1") buff.Push("item 2") buff.Push("item 3") buff.Push("item 4") buff.Push("item 5") // block the current goroutinetime.Sleep(3*time.Second) println("done") }

Interval-triggered flush

package main import ( "time""github.com/globocom/go-buffer/v2" ) funcmain(){buff:=buffer.New( // buffer can hold up to 5 itemsbuffer.WithSize(5), // buffer will be flushed every second, regardless of// how many items were pushedbuffer.WithFlushInterval(time.Second), // call this function when the buffer needs flushingbuffer.WithFlusher(buffer.FlusherFunc(func(items []interface{}){for_, item:=rangeitems{println(item.(string)) } })), ) deferbuff.Close() buff.Push("item 1") buff.Push("item 2") buff.Push("item 3") // block the current goroutinetime.Sleep(3*time.Second) println("done") }

Manual flush

package main import ( "time""github.com/globocom/go-buffer/v2" ) funcmain(){buff:=buffer.New( // buffer can hold up to 5 itemsbuffer.WithSize(5), // call this function when the buffer needs flushingbuffer.WithFlusher(buffer.FlusherFunc(func(items []interface{}){for_, item:=rangeitems{println(item.(string)) } })), ) deferbuff.Close() buff.Push("item 1") buff.Push("item 2") buff.Push("item 3") // block the current goroutinetime.Sleep(3*time.Second) buff.Flush() println("done") }

Documentation

Visit Pkg.go.dev for full documentation.

License

MIT License

About

Asynchronous data buffer for Go applications

Topics

Resources

License

Stars

Watchers

Forks

Contributors 4

  •  
  •  
  •  
  •