https://github.com/maxgio92/go-mpsc-queue
A simple implementation of a multiple producers, single consumer, messages queue
https://github.com/maxgio92/go-mpsc-queue
Last synced: about 1 year ago
JSON representation
A simple implementation of a multiple producers, single consumer, messages queue
- Host: GitHub
- URL: https://github.com/maxgio92/go-mpsc-queue
- Owner: maxgio92
- License: apache-2.0
- Created: 2023-01-15T10:33:43.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-06-05T15:44:12.000Z (almost 3 years ago)
- Last Synced: 2025-01-26T00:16:42.575Z (over 1 year ago)
- Language: Go
- Size: 9.77 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# MPSC queue in Go
This is a simple implementation of a multiple producers, single consumer queue in Go.
It provides a queue for messages and a queue for errors, and simple helpers to manage synchronization.
It leverages `sync.WaitGroup` and `channel`s. Producers are synchronized through `sync.WaitGroup`.
Synchronization with the consumer happens through a dedicated `channel`.
## Usage
```go
package main
import (
"fmt"
gompscqueue "github.com/maxgio92/go-mpsc-queue"
)
func main() {
parallelism := 10
queue := gompscqueue.NewMPSCQueue(parallelism)
for i := 0; i < parallelism; i++ {
go func() {
defer queue.SigProducerCompletion()
// Here you do your work.
queue.SendMessage("Hello world from producer!")
}()
}
go queue.Consume(
func(msg interface{}) {
fmt.Printf("new message: %s\n", msg)
},
func(err error) {
fmt.Errorf("error: %s\n", err.Error())
},
)
queue.WaitAndClose()
}
```
## Similarities
Other packages do something similar, like:
- [run](https://github.com/oklog/run)
- [errgroup](https://pkg.go.dev/golang.org/x/sync/errgroup)
- [tomb](https://pkg.go.dev/gopkg.in/tomb.v2)