r/golang•
rouille

Routing TCP (or UDP) packets

Hi, I am working on a problem where I have a single TCP (could be UDP as well) connection and I need to route the packets to one of N sockets on the other side depending on the destination. I don't want to block the flow because one of the destinations is down so dropping the packets is OK, the clients can resend.

A simple solution I see in go would be to have one reader goroutine , N writer goroutines and N buffered channels which each handle their timeouts etc. However I want to be able to have a throughput as close as possible to the network interfaces throughput and sending individual packets trough channels seems inefficient. With a 1 to 1 connection I can just allocate one buffer and reuse it, now I need a bunch of buffers to be passed through channels.

Is it really that inefficient? Is there a way I could do it from a single goroutine?

I also have a similar questions with N readers to one writer. Basically is there a way to make N reader goroutines plus a select statement over N channels close to the efficiency of a select statement over N file descriptors in C.

Posted Aug 31, 2014, 8:56 AM UTC

27
Loading comments…