Have you ever wondered if you should be using a GenServer
or a Task.Supervisor
? You're not alone! Here, I'll share my experience with making this choice - hopefully it helps you decide ;)
The Premise
I recently ran into a situation where I used a GenServer
to prototype some functionality before implementing it in a larger project. It worked excellently and implementing the server (which handles and stores the state of TCP connections, and accepts GenServer call
s - note on call/cast) went without a hitch. Great!
However, when I went to back integrate this feature, there was a bit of an issue. Instead of
implementing a GenServer, the original code uses a listen loop to spawn TCP socket-handling functions and hands them off to a Task Supervisor
.
At this point, my approach is to just implement call
s in the original code and modify the internals to match the desired behaviour. However, my coworker was bothered by this, as there's a difference between Task Supervisor and GenServer. His idea was that we should implement the manager from scratch using GenServer, but is that the best way to do it? Maybe. I'm not really sure yet. I'll be updating this document to reflect what I learn. You better hope OP delivers!
Call/Cast notes
As far as I have seen, the general consensus is that you should start by using call
and only use cast
after careful algorithmic analysis, and if you're sure it's the right choice. Check out this great Medium article by @adammokan for a better explanation.
(Tags: GenServer, Gen Server, Task.Supervisor, Task Supervisor, Elixir, Erlang)