Skip to content

Generic, Asynchronous data buffer for Go applications that supports timeout and capacity flushing

License

Notifications You must be signed in to change notification settings

ivanvanderbyl/buffer

 
 

Repository files navigation

This is a fork of https://github.com/globocom/go-buffer using Generics.

buffer

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/ivanvanderbyl/buffer

Examples

Size-triggered flush

package main

import (
  "time"

  "github.com/ivanvanderbyl/buffer"
)

func main() {
  buff := buffer.New(
  // call this function when the buffer needs flushing
  buffer.Fn(func(items []string) {
   println("flushing", len(items), "items")
   for _, item := range items {
    println(item)
   }
  }),
  // buffer can hold up to 5 items
  buffer.WithSize(5),
 )
 // 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")
 buff.Push("item 6") // This item will be flushed by the Closer.

 println("exiting...")
}

Interval-triggered flush

package main

import (
  "time"

  "github.com/ivanvanderbyl/buffer"
)

func main() {
  buff := buffer.New(
  // call this function when the buffer needs flushing
  buffer.Fn(func(items []string) {
   println("flushing", len(items), "items")
   for _, item := range items {
    println(item)
   }
  }),
  // buffer can hold up to 3 items
  buffer.WithSize(3),
  buffer.WithFlushInterval(time.Second),
 )
 // ensure the buffer
 defer buff.Close()

 buff.Push("item 1") // Flushed on timeout of 1 second
 buff.Push("item 2") // Flushed on timeout of 1 second
 time.Sleep(2 * time.Second)
 buff.Push("item 3")
 buff.Push("item 4")
 buff.Push("item 5")
 buff.Push("item 6") // Flushed on close

 println("exiting...")
}

Manual flush

package main

import (
 "github.com/ivanvanderbyl/buffer"
)

func main() {
 buff := buffer.New(
  buffer.Fn(func(items []string) {
   println("flushing", len(items), "items")
   for _, item := range items {
    println(item)
   }
  }),
  // buffer can hold up to 5 items
  buffer.WithSize(5),
 )
 defer buff.Close()

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

 println("done")
}

Custom Flusher

package main

import (
 "github.com/ivanvanderbyl/buffer"
)

type CustomFlusher struct{}

func (f CustomFlusher) Write(items []string) {
 println("flushing", len(items), "items")
 for _, item := range items {
  println(item)
 }
}

// Verify that CustomFlusher implements the buffer.Flusher interface
var _ buffer.Flusher[string] = (*CustomFlusher)(nil)

func main() {
 flusher := CustomFlusher{}

 buff := buffer.New[string](flusher, buffer.WithSize(5))
 defer buff.Close()

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

Documentation

Visit Pkg.go.dev for full documentation.

License

MIT License

About

Generic, Asynchronous data buffer for Go applications that supports timeout and capacity flushing

Topics

Resources

License

Stars

Watchers

Forks

Languages

  • Go 99.0%
  • Makefile 1.0%