Skip to content

A simple implementation of a multiple producers, single consumer, messages queue

License

Notifications You must be signed in to change notification settings

maxgio92/go-mpsc-queue

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 channels. Producers are synchronized through sync.WaitGroup. Synchronization with the consumer happens through a dedicated channel.

Usage

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:

About

A simple implementation of a multiple producers, single consumer, messages queue

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages