https://github.com/floatdrop/ringchan
Ring buffer channel for Go
https://github.com/floatdrop/ringchan
Last synced: 9 months ago
JSON representation
Ring buffer channel for Go
- Host: GitHub
- URL: https://github.com/floatdrop/ringchan
- Owner: floatdrop
- License: mit
- Created: 2025-06-19T17:11:57.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-06-19T19:05:45.000Z (about 1 year ago)
- Last Synced: 2025-06-19T19:44:00.740Z (about 1 year ago)
- Language: Go
- Size: 8.79 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# RingChan
[](https://github.com/floatdrop/ringchan/actions/workflows/ci.yaml)

[](https://goreportcard.com/report/github.com/floatdrop/ringchan)
[](https://pkg.go.dev/github.com/floatdrop/ringchan)
[](https://opensource.org/licenses/MIT)
**RingChan** is a thread-safe, fixed-capacity ring buffer implemented as a channel in Go. It mimics Go's channel behavior while providing ring-buffer semantics — meaning **new items overwrite the oldest when full**.
## Features
- Fixed-size buffer with overwrite behavior
- Range-friendly: can be iterated using `for ... range`
- Safe for concurrent producers and consumers
## Installation
```bash
go get github.com/floatdrop/ringchan
```
## Usage
```go
package main
import (
"fmt"
"time"
"github.com/floatdrop/ringchan"
)
func main() {
input := make(chan string, 5)
ring := ringchan.New(input, 3)
go func() {
inputs := []string{"A", "B", "C", "D", "E"}
for _, v := range inputs {
input <- v
}
close(input)
}()
time.Sleep(50 * time.Millisecond)
for v := range ring.C {
fmt.Println("Got:", v)
}
// Output:
// Got: C
// Got: D
// Got: E
}
```
## Benchmarks
```bash
go test -bench=. -benchmem
```
```
goos: darwin
goarch: arm64
pkg: github.com/floatdrop/ringchan
cpu: Apple M1 Pro
BenchmarkSingleSender-10 7097070 167.3 ns/op 0 B/op 0 allocs/op
BenchmarkParallelSenders-10 4145682 295.0 ns/op 0 B/op 0 allocs/op
PASS
coverage: 90.9% of statements
ok github.com/floatdrop/ringchan 3.050s
```
For high-throughput write-heavy workloads it is better to use preallocated ringbuffer, like https://github.com/peterbourgon/rb
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.