File: README.md

package info (click to toggle)
golang-github-globocom-go-buffer 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 404 kB
  • sloc: makefile: 6
file content (143 lines) | stat: -rw-r--r-- 3,126 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<p align="center">
  <img src="gopher.png">
</p>
<p align="center">
  <img src="https://img.shields.io/github/workflow/status/globocom/go-buffer/Go?style=flat-square">
  <a href="https://github.com/globocom/go-buffer/blob/master/LICENSE">
    <img src="https://img.shields.io/github/license/globocom/go-buffer?color=blue&style=flat-square">
  </a>
  <img src="https://img.shields.io/github/go-mod/go-version/globocom/go-buffer?style=flat-square">
  <a href="https://pkg.go.dev/github.com/globocom/go-buffer">
    <img src="https://img.shields.io/badge/Go-reference-blue?style=flat-square">
  </a>
</p>

# 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(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(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(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)