Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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: 23 days 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 (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-06-05T15:44:12.000Z (over 1 year ago)
- Last Synced: 2024-04-21T01:05:23.258Z (7 months ago)
- Language: Go
- Size: 9.77 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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 mainimport (
"fmt"
gompscqueue "github.com/maxgio92/go-mpsc-queue"
)func main() {
parallelism := 10queue := 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)