Maximum buffer size for a channel

5,988 views
Skip to first unread message

Anh Hai Trinh

unread,
Feb 1, 2010, 11:11:22 AM2/1/10
to golang-nuts
I have a few questions:

What is the maximum buffer size for a channel? sizeof(int)?

And would it be possible (and sensible) to have channels with
non-blocking write, i.e. infinite buffer?

Thanks,

--
// aht
http://blog.onideas.ws

Russ Cox

unread,
Feb 1, 2010, 11:35:57 AM2/1/10
to Anh Hai Trinh, golang-nuts
On Mon, Feb 1, 2010 at 08:11, Anh Hai Trinh <anh.ha...@gmail.com> wrote:
I have a few questions:

What is the maximum buffer size for a channel? sizeof(int)?

In the current implementation, the maximum buffer size
you can ask for is the maximum int, but you're not likely
to get it since that would need to allocate 2G items worth
of buffer.
 
And would it be possible (and sensible) to have channels with
non-blocking write, i.e. infinite buffer?

It's not possible in the language now.  It might be convenient
in some cases, but I think equally often it would lead to
programs that used all of memory for buffering.

Russ

Anh Hai Trinh

unread,
Feb 1, 2010, 12:29:48 PM2/1/10
to r...@golang.org, golang-nuts
On Mon, Feb 1, 2010 at 11:35 PM, Russ Cox <r...@golang.org> wrote:
>
> In the current implementation, the maximum buffer size
> you can ask for is the maximum int, but you're not likely
> to get it since that would need to allocate 2G items worth
> of buffer.

I am not too familiar with channel implementation, but it looks like
instead of pre-allocating a circular buffer ring, perhaps you could
use a normal linked list and allocate memory when values are sent to
the channel?


> It's not possible in the language now.  It might be convenient
> in some cases, but I think equally often it would lead to
> programs that used all of memory for buffering.
> Russ

You are probably right generally. But for this toy program of mine
<http://github.com/aht/gosieve/blob/master/sieve2.go#L145> having
non-blocking write is not just a convenience: the program will
deadlock when the buffer is full as there is a feedback loop in the
system that is consumed rather slowly. That program still runs since
it only works with primes in the int range so I can set a big enough
buffer. But theoretically, there are programs whose lives depend on
infinite buffer channels :)

Ian Lance Taylor

unread,
Feb 1, 2010, 12:40:40 PM2/1/10
to Anh Hai Trinh, r...@golang.org, golang-nuts
Anh Hai Trinh <anh.ha...@gmail.com> writes:

> But theoretically, there are programs whose lives depend on
> infinite buffer channels :)

Theoretically, perhaps, but clearly impossible on real computers. I
can see that in some cases a feedback loop might require a program to
use a top-level select loop, and that that could require the program
to have an awkward structure. But that structure would provide a real
benefit: the program would avoid slowly consuming all available
resources before deadlocking.

Ian

roger peppe

unread,
Feb 1, 2010, 12:46:36 PM2/1/10
to Anh Hai Trinh, r...@golang.org, golang-nuts
if you want an inifinite buffer, it's easily programmed with an extra
goroutine.

eap...@gmail.com

unread,
Jan 13, 2014, 10:07:17 AM1/13/14
to golan...@googlegroups.com
I ended up writing an entire package that implements a bunch of related ideas in this area:

https://github.com/eapache/channels
https://godoc.org/github.com/eapache/channels

It includes channels with "infinite" buffers channels with finite-but-resizable buffers and a bunch of other useful types and functions.

Reply all
Reply to author
Forward
0 new messages