https://github.com/globocom/go-buffer
Asynchronous data buffer for Go applications
https://github.com/globocom/go-buffer
async buffer go
Last synced: 4 months ago
JSON representation
Asynchronous data buffer for Go applications
- Host: GitHub
- URL: https://github.com/globocom/go-buffer
- Owner: globocom
- License: mit
- Created: 2020-03-23T16:15:51.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2023-10-21T02:54:47.000Z (about 2 years ago)
- Last Synced: 2024-06-20T06:44:44.206Z (over 1 year ago)
- Topics: async, buffer, go
- Language: Go
- Homepage:
- Size: 313 KB
- Stars: 22
- Watchers: 14
- Forks: 6
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
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
```golang
package main
import (
"time"
"github.com/globocom/go-buffer/v2"
)
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(buffer.FlusherFunc(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
```golang
package main
import (
"time"
"github.com/globocom/go-buffer/v2"
)
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(buffer.FlusherFunc(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
```golang
package main
import (
"time"
"github.com/globocom/go-buffer/v2"
)
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(buffer.FlusherFunc(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](https://pkg.go.dev/github.com/globocom/go-buffer) for full documentation.
## License
[MIT License](https://github.com/globocom/go-buffer/blob/master/LICENSE)