I have a long running GenServer that makes frequent calls using Ecto.
Now I know you can use Repo.checkout/2 to checkout a connection for the lifetime of a function, but can I do the same for my GenServer process? It seems inefficient to keep re-creating a connection that I know it’s going to be used frequently by a single process.
Your GenServer probably accesses the DB in response to messages send to it. How do you know these messages will be sent back-to-back in a quick succession, forever? IMO you don’t know that so it is much safer to have Repo.checkout wrap the part of the DB-accessing code inside your handle_call function body.
Ecto’s underlying pool manager poolboy is quite fast and efficient and can quickly check-out / check-in the underlying resource (in this case DB connections). Don’t concern yourself with that.
Finally, if you expect bursty workloads where you wouldn’t need GenServer but more likely just directly spawn a Task and such then yes, I’d give unfettered access to that if I expect it will end in the next, say, 3 to 15 seconds (those values depend on your pool config e.g. queue_target and the various timeouts).