olahol/melody

Websocket connections get dropped when a long-running operation is executed inside a handler

mikolysz opened this issue · 3 comments

Currently, all message handlers run in the same goroutine responsible for reading messages from the websocket connection. That goroutine is also responsible for handling pong messages and extending the connection's read deadline when they come in. When our handler executes for longer than a minute (with the default configuration), this causes the deadline to expire, dropping the connection the moment we try writing to the socket. This behavior is not documented.

This issue could be solved either by documenting the current behavior or by running each handler in its own goroutine, just like http.Server does for incoming http requests. I'm willing to do a PR for this if the maintainer(s) decide which solution is appropriate.

The current workaround is to surround all code for your long-running handlers with a go func() { ... } block. This lets your handler exit immediately, freeing its goroutine to handle other messages. AN entirely separate goroutine is created for your long-running code to run in.

I have made a branch https://github.com/olahol/melody/tree/concurrent-message-handling and added a config option ConcurrentMessageHandling. Can you check if this solves your issue?

@olahol can you tag a new release for this change ?

@olahol can you tag a new release for this change ?

Is now released as v1.1.5