Skip to content
A producer-consumer solution for Golang
Go Makefile
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
example close #1. refactor public api Jul 15, 2019
.gitignore
.golangci.yml fix doc format Jul 15, 2019
.travis.yml add code coverage Jul 18, 2019
Makefile add code coverage Jul 18, 2019
README.md add code coverage Jul 18, 2019
go.mod close #1. refactor public api Jul 15, 2019
go.sum close #1. refactor public api Jul 15, 2019
prosumer.go fix nonblocking dequeue Jul 25, 2019
prosumer_test.go fix nonblocking dequeue Jul 25, 2019
queue.go fix nonblocking dequeue Jul 25, 2019

README.md

Prosumer

GoDoc Build Status codecov

A producer-consumer solution for Golang.

Motivation

Go is popular for its simplicity, builtin support for concurrency, light-weight goroutine. However, there are some tricks(here and there) when to coordinate among different goroutines, especially when implements producer-consumer pattern using buffered chan.

I don't want cover details here, guys who interested can check links above. But following cannot be emphasized too much:

Close chan is a sender -> receiver communication, not the reverse. #11344

Feature

  • Inner buffer queue support RejectPolicy
  • Graceful close implemented

If you have any suggestions/questions, open a PR.

Usage

This is just a quick introduction, view the GoDoc for details.

Let's start with a trivial example:

func main() {
	maxLoop := 10
	var wg sync.WaitGroup
	wg.Add(maxLoop)
	defer wg.Wait()
	
	consumer := func(ls []interface{}) error {
		fmt.Printf("get %+v \n", ls)
		wg.Add(-len(ls))
		return nil
	}

	conf := prosumer.DefaultConfig(prosumer.Consumer(consumer))
	c := prosumer.NewCoordinator(conf)
	c.Start()

	for i := 0; i < maxLoop; i++ {
		fmt.Printf("try put %v\n", i)
		discarded, err := c.Put(i)
		if err != nil {
			fmt.Errorf("discarded elements %+v for err %v", discarded, err)
			wg.Add(-len(discarded))
		}
		time.Sleep(time.Second)
	}
	c.Close(true)
}

License

MIT License © Jiacai Liu

You can’t perform that action at this time.