Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.


Please 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)


You 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

      }
  }


Instead of?

  for {
    select {
    case <-finish:
      done <- true
      return
    case msg <- msg_out:
      // write handling
    case <-pubsub:
      // handle oob controls
    default:
      // read handling
    }
  }


You wont be able to write messages while blocked on the read in this scenario.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: