3 ms·
Why two (or three) go routines per connection? Why not one net socket (for reads) and two channels (one for writes, other for pub/sub) and select() between them
by tankenmate 9y ago
Why two (or three) go routines per connection? Why not one net socket (for reads) and two channels (one for writes, other for pub/sub) and select() between them? It seems like the OP is trying too hard to avoid event loops.
- lenkite 9y agoPlease upvote parent. This is the first thing that struck my mind too. golang offers `select` for non-blocking calls. Deeply skeptical that node.js is the best solution for a chat service. (unless the backend is serving rendered UI's)
- Thetawaves 9y agoYou generally need one thread to read for incoming messages, typical: for { select { case <-finish: done <- true return default: // read from the socket } } You need another thread to listen for messages on a channel and send them out the socket typical: for { select { case <-finish: done <- true return case msg <- msg_out: // write msg to socket } }
- tankenmate 9y agoInstead of? for { select { case <-finish: done <- true return case msg <- msg_out: // write handling case <-pubsub: // handle oob controls default: // read handling } }
- Thetawaves 9y agoYou wont be able to write messages while blocked on the read in this scenario.