This week’s post is a 5-minute read.
This is the first of a series of posts on simple OTP idioms, patterns and tricks.
We’ll start with a few posts on handle_info, each week covering some some easy pattern that can be covered in a few minutes.
This week we’ll talk about how to use handle_info to slightly defer the initialization of an expensive to set up server.
You’re writing an application in Elixir and you’ve got a GenServer that is costly to initialize:
The problem with this is that ‘MyServer.start_link’ has to block until ‘init’ finishes.
If ‘init’ is slow, then any caller trying to create an instance of ‘MyServer’ will be temporarily blocked.
The issue is made worse if there are multiple servers set up this way because OTP Supervisors start each of their children serially. So if you have 10 child servers that each take 6 seconds to initialize, then the supervisor will be blocked for a minute while it tries to get each one started.
We’d like to avoid this situation if we can.
Note that the return tuple from init has a 3rd element, which is
0. In this case,
0 simply means “timeout immediately”.
The timeout will trigger the handle_info callback allowing you to do your drawn out setup out of band without making everyone else wait.
I’ve received conflicting information on whether or not the timeout handler will be guaranteed to be the first message handled by your server.
If it’s not, and your server receives a different message first, then your server will most likely crash because its state will not have been fully initialized at that point.
This seems unlikely, although it might be possible.
It’s worth noting that you don’t have to use timeout to achieve similar results:
As you can see from above, the real mechanism for deferred initialization isn’t the timeout - it’s using init to stuff a message into the mailbox that tells it to finish its initialization next.
Triggering a timeout is just one means to that; directly using send to queue up a post initialization function (also via the handle_info callback) is another.
If the state of your server needs to be periodically updated on a schedule (e.g a Cache), then setting things up with timeouts might be the way to go.
For everything else, the second approach is probably best.
You’ll see several variations of these techniques in open source projects if you hunt for them (see the links below).
You don’t have to use them yourself, but it can save you time if you know how to spot them.
Here are some relevant examples of handle_info that you might be interested in:
If you read through the examples, it will be worth your time to checkout how each ‘init’ function is setup. A few are explicitly setting timeouts, while others use different approaches.
Next week’s post will cover using handle_info for looping and polling.
Thanks for following along.