gobwas/ws-examples

Why do we use netpoll event descriptor instead of just a reader goroutine?

Closed this issue · 2 comments

I'm trying to understand the example, specifically why do we need to create a netpoll even descriptor for the purpose of reading messages from the connection?

Comparing to most other websocket library examples, I usually see just a dedicated goroutine for reading messages, and usually another separate goroutine for writing messages. Why the seemingly unnecessary complication here?

Would creating a separate reader goroutine for every connection achieve the same thing?

// Create netpoll event descriptor for conn.
// We want to handle only read events of it.
desc := netpoll.Must(netpoll.HandleRead(conn))

It's simply about optimization. If you run 2 go routines per connection, it's fine if you have 10. But when you have 5000 or 100 000 it starts getting problematic.

Of course when you're using workers and similar to reduce the number of go routines you'll end up having to solve other kinds of problems, such as slow connections leading to starvation.

Pick your poison :)

Thanks for explaining. I also read this article from the author which provided a lot of context as well.