Skip to content

Commit 6e20a76

Browse files
committed
Update README
1 parent f25d612 commit 6e20a76

File tree

3 files changed

+135
-1
lines changed

3 files changed

+135
-1
lines changed

README.md

Lines changed: 135 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,137 @@
1+
<p align="center">
2+
<img src="gopher.png">
3+
</p>
4+
5+
![Go](https://img.shields.io/github/workflow/status/globocom/go-buffer/Go?style=flat-square)
6+
[![License](https://img.shields.io/github/license/globocom/go-buffer?color=blue&style=flat-square)](https://github.com/globocom/go-buffer/blob/master/LICENSE)
7+
![Go](https://img.shields.io/github/go-mod/go-version/globocom/go-buffer?style=flat-square)
8+
[![PkgGoDev](https://img.shields.io/badge/Go-reference-blue?style=flat-square)](https://pkg.go.dev/github.com/globocom/go-buffer)
9+
110
# go-buffer
211

3-
![Go](https://github.com/globocom/go-buffer/workflows/Go/badge.svg)
12+
`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.
13+
14+
## Installation
15+
16+
go get github.com/globocom/go-buffer
17+
18+
## Examples
19+
20+
### Size-based flush
21+
22+
```golang
23+
package main
24+
25+
import (
26+
"time"
27+
28+
"github.com/globocom/go-buffer"
29+
)
30+
31+
func main() {
32+
buff := buffer.New(
33+
// buffer can hold up to 5 items
34+
buffer.WithSize(5),
35+
// call this function when the buffer needs flushing
36+
buffer.WithFlusher(func(items []interface{}) {
37+
for _, item := range items {
38+
println(item.(string))
39+
}
40+
}),
41+
)
42+
defer buff.Close()
43+
44+
buff.Push("item 1")
45+
buff.Push("item 2")
46+
buff.Push("item 3")
47+
buff.Push("item 4")
48+
buff.Push("item 5")
49+
50+
// block the current goroutine
51+
time.Sleep(3*time.Second)
52+
53+
println("done")
54+
}
55+
```
56+
57+
### Interval-based flush
58+
59+
```golang
60+
package main
61+
62+
import (
63+
"time"
64+
65+
"github.com/globocom/go-buffer"
66+
)
67+
68+
func main() {
69+
buff := buffer.New(
70+
// buffer can hold up to 5 items
71+
buffer.WithSize(5),
72+
// buffer will be flushed every second, regardless of
73+
// how many items were pushed
74+
buffer.WithFlushInterval(time.Second),
75+
// call this function when the buffer needs flushing
76+
buffer.WithFlusher(func(items []interface{}) {
77+
for _, item := range items {
78+
println(item.(string))
79+
}
80+
}),
81+
)
82+
defer buff.Close()
83+
84+
buff.Push("item 1")
85+
buff.Push("item 2")
86+
buff.Push("item 3")
87+
88+
// block the current goroutine
89+
time.Sleep(3*time.Second)
90+
91+
println("done")
92+
}
93+
```
94+
95+
### Manual flush
96+
97+
```golang
98+
package main
99+
100+
import (
101+
"time"
102+
103+
"github.com/globocom/go-buffer"
104+
)
105+
106+
func main() {
107+
buff := buffer.New(
108+
// buffer can hold up to 5 items
109+
buffer.WithSize(5),
110+
// call this function when the buffer needs flushing
111+
buffer.WithFlusher(func(items []interface{}) {
112+
for _, item := range items {
113+
println(item.(string))
114+
}
115+
}),
116+
)
117+
defer buff.Close()
118+
119+
buff.Push("item 1")
120+
buff.Push("item 2")
121+
buff.Push("item 3")
122+
123+
// block the current goroutine
124+
time.Sleep(3*time.Second)
125+
126+
buff.Flush()
127+
println("done")
128+
}
129+
```
130+
131+
## Documentation
132+
133+
Visit [Pkg.go.dev](https://pkg.go.dev/github.com/globocom/go-buffer) for full documentation.
134+
135+
## License
136+
137+
[MIT License](https://github.com/globocom/go-buffer/blob/master/LICENSE)

gopher.png

105 KB
Loading

gopher.xcf

183 KB
Binary file not shown.

0 commit comments

Comments
 (0)