I’m connecting to a physical device that can only accept one incoming connection at a time, but I can have multiple clients trying to access it at the same time. I’d like to serialize these requests so that they don’t conflict with each other. I’m pretty sure that I could do this by just writing my own message loop in a process (basically, receive a message, process it, send a response back and go back to the receive step), but I feel like it would be easier if I could use a GenServer since the client can be blocked until the request is completed. What I’m unclear about, though, is whether the handle_call will block other processes from using the server until it’s completed or if it’s just the one process using GenServer.call that gets blocked. Can someone tell me which one it is? And I’m certainly open to other suggestions as to how to achieve the goal of making sure that only one process can access a resource at a time. Thanks.
That’s pretty much the optimal use-case for the GenServer behavior, you are safe using it.
They block all other processes. By design GenServers can only serve one request at a time (this is how they are able to keep consistent state between requests).
call will always block and wait until the caller request was fulfilled (or if it reaches a timeout).
You can check this thread here for more discussion: GenServer use-cases
Thanks. I’ll continue along those lines, then.