Emulate dynamically buffered channels in Golang

Boris Strelnikov
2 min readSep 2, 2020
Moscow channel

It’s known that there are only two types of channels in Go: unbuffered and buffered. There is no way to create from scratch a channel that shrinks in capacity automatically. So let’s implement such functionality via storing elements in the closure slice. Unfortunately, there are no generics to parametrize types of the channel’s elements so we will use interface{} types.

So important things here are the followings:

  • Result channel out closed only after closed in channel that is given as argument
  • After in is closed we need to unwind storage slice before closing out
  • We’ve carefully implemented state machine for different events: new incoming item, the closing of in and outgoing item to out

Example usage

Let’s consider the case when you need to process some tasks in a parallel manner with a pool of workers and the slow task shouldn’t block adding to a queue new task even if the number of tasks in the queue waiting for execution is big enough.

Slow task execution may be simulated via such test:

When we run the test in verbose mode via go test -v ./... and see that adding new items to the queue method isn’t blocked as should be expected.

P.S. All source code may be found in https://github.com/risentveber/dynamicqueue

--

--