In my websocket server developed with Erlang, I would like to use a timer (start_timer/3
), for each connection, to terminate the connection if the timeout elapses without receiving a "ping" from the client.
Do Erlang timers scale well, assuming I will have a large number of client connestions?
call sleep(Time) -> ok to suspend the process calling this function for Time amount of milliseconds. Save this answer.
The Timer module allows you to wait for something to happen, and to timeout (which throws an exception) if that something doesn't happen quickly enough.
What is a large number of connections? Erlangs VM uses a timer wheel internally to handle the timers so it scales pretty well up to some thousand connections. Then you might run into trouble.
Usually the trick is to group pids together on timers. This is also what kernels tend to do. If for instance you have a timer that has to awake in 200ms you schedule yourself ahead of time not on the next, but the next 200ms timer again. This means you will wait at least 200ms and perhaps 400ms, 300ms being typical. By approximating timers like this, you are able to run many more since you can have a single timer wake up large numbers of processes in one go. But depending on the timer frequency and amounts of timers a standard send_after/3
may be enough.
In any case, I would start by assuming it can scale and then handle the problem if it can't by doing approximate timing like envisioned above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With