non-blocking send to channel?

3,474 views
Skip to first unread message

Kosta Welke

unread,
Jun 10, 2011, 2:54:04 AM6/10/11
to golan...@googlegroups.com
Hi!

For a small example server that listens for messages and then broadcasts them to a number of channels, I was pointed to the following problem:

>Also, your broadcasting of msg to all the connections may block. The connection channels are buffered, but a slow listening connection could easily fill up the buffer, thus blocking the send and the whole server. If you have an inner loop, you always want non-blocking channel sends, regardless of the size of the buffer.

Now, is there a way to do a non-blocking send on a channel or is all I can do to increase the buffer size and close the connection if it still is too slow? I don't want to put the send in a goroutine because I want to preserve the order of messages.

Rob 'Commander' Pike

unread,
Jun 10, 2011, 2:56:21 AM6/10/11
to golan...@googlegroups.com
select {
case ch <- value:
default:
}

-rob

Kosta Welke

unread,
Jun 10, 2011, 3:18:38 AM6/10/11
to golan...@googlegroups.com
Thanks, rob.

This will not send the message when the channel is full. Right?

Rob 'Commander' Pike

unread,
Jun 10, 2011, 3:24:14 AM6/10/11
to golan...@googlegroups.com

On Jun 10, 2011, at 5:18 PM, Kosta Welke wrote:

> Thanks, rob.
>
> This will not send the message when the channel is full. Right?

Correct.

-rob

Reply all
Reply to author
Forward
0 new messages