https://github.com/iamjinlei/goringbuffer
Ringbuffer with concurrent insertion and Iteration support
https://github.com/iamjinlei/goringbuffer
golang lockless ringbuffer
Last synced: about 1 month ago
JSON representation
Ringbuffer with concurrent insertion and Iteration support
- Host: GitHub
- URL: https://github.com/iamjinlei/goringbuffer
- Owner: iamjinlei
- License: gpl-3.0
- Created: 2019-02-14T05:25:13.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2020-03-26T08:07:07.000Z (almost 6 years ago)
- Last Synced: 2025-08-15T06:46:17.634Z (7 months ago)
- Topics: golang, lockless, ringbuffer
- Language: Go
- Homepage:
- Size: 69.3 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.com/iamjinlei/goringbuffer)
# Ring Buffer
A simple thread safe ring buffer implementation that supports add and iterate operation. The position in ring buffer does not necessarily indicate data freshness at the tiem when data are added concurrently, but eventually would. An example of such scenario:
Assume we have following ring buffer
```
+----+----+----+----+----+----
| p0 | p1 | p2 | p3 | p4 |
+----+----+----+----+----+----
```
And the code is
```
r := goringbuffer.New(10)
go r.Add(100) // to p0 with old value 10
go r.Add(200) // to p1 with old value 20
sum := []int{0}
go r.Do(func(e interface{}){
sum[0] += e.(int)
})
```
Even though the above code may settle 100 into p0 and 200 into p1 eventually, sum[0] may be 30 (10 + 20), 300 (100 + 200), 210 (10 + 200) or 120 (100 + 20).
# Perf
Intel Core i5 2.3GHz
#### size = 16
```
BenchmarkAdd-8 50000000 36.1 ns/op
BenchmarkDo-8 20000000 104 ns/op
```
#### size = 64
```
BenchmarkAdd-8 30000000 36.7 ns/op
BenchmarkDo-8 5000000 322 ns/op
```