Reputation: 322
One thing I do not like about Go is that channel receive also removes the data from the channel. This only allows two goroutines to communicate with each other even though there are several cases when two or more goroutines should be able to talk to each other.
I know I could make an array of channels and have channel per goroutine, but moving data from one goroutine to all of the other goroutines is much more data on ram than moving one copy of the data to all goroutines.
Think about a case when I have thousand clients connected to server and I want one to send message to only half of them that is five hundred goroutines receiving that message. If the message is 512 bytes this becomes 250 kilobytes of data in the ram moving, even though it's possible to move the same data only once if channels would not remove the data on receive.
So I am asking if there is some simple way to do this or do I have to use mutex from the sync package? Though please tell me if I am calculating wrong and channels do not copy the data, because in that case I can just manage array of channels.
Upvotes: 1
Views: 525
Reputation: 14128
I typically do something like this:
type Message struct {
text string
address string
...
}
type Server {
dropbox chan Message
clients []*Conn
...
}
type Conn {
inbox chan *Message
...
}
Each client, served by a reading/writing go routine, drops "Message" into "dropbox". The Server pulls messages out of "dropbox" and determines which clients to send the message to based on "address".
In Server "clients" can even be a map. It really depends on how you want to route the message: Specific clients, groups, etc.
You can do some clever things with chan chan T
, but if you want to do intelligent routing instead of blind broadcasting, you really need some way to map the message to the client.
In this case you don't need a Mutex. There are cases where Mutexes are best, but in this case a channels are much easier.
Upvotes: 1
Reputation: 24808
Read this article:
It's an analysis on the different ways to channel-broadcast across goroutines, one of them being particularly interesting:
type Broadcaster struct {
Listenc chan chan (chan broadcast);
Sendc chan<- interface{};
}
This approach is called a "linked channel" (analagously to a linked list) by the author.
tell me if I am calculating wrong and channels do not copy the data, because in that case I can just manage array of channels.
You're not wrong. As @Jsor suggested, though - you can just pass pointers around if you're afraid of copy-overhead and the use case allows it.
Upvotes: 2