auto_process_registry v0.1.0 AutoProcessRegistry behaviour
AutoProcessRegistry is a process registry that is setup with a starter function to start new processes on demand.
Processes aren’t supervised. The idea is that if they die, for whichever reason, they can be restarted on demand when necessary. It is not hard, though, to make the starter function register a process under a supervisor.
The registry implements the whereis
message that is needed to
make it usable for via naming lookups in the style of
{:via, YourRegistryModule, key}
.
Here is a quick example using agents, lifted from the test suite:
defmodule SampleModule do
use AutoProcessRegistry
def start_new(config, key) do
Agent.start_link(fn -> config * key end)
end
end
SampleModule.start_link(10)
With this module defined, it’s now possible to launch processes (in
this case, simple agents, but it should be hard to see how to extend
this for GenServer
, etcetera:
Agent.get({:via, SampleModule, 42}, fn(key) -> key end) # --> 420
Agent.get({:via, SampleModule, 666}, fn(key) -> key end) # --> 6660
A potential use case for this library is for example genservers that have
their state persisted in a database. start_new
can be setup to read
data for a primary key, start a process with the state from the database,
and from then reading from the database basically is just done on the fly
by starting to talk to an instance with a certain primary key. What would
be neat in that case, of course, is to extend this with an LRU cache and a
maximum number of live processes, maybe for a later iteration.
Summary
Callbacks
Start the GenServer process implementing the registry. The GenServer
is registered with the module name, so keeping the pid around is not
necessary. The config argument is passed on to start_new
Start a new process with the config argument as the first argument and the key as the second argument
Callbacks
Start the GenServer process implementing the registry. The GenServer
is registered with the module name, so keeping the pid around is not
necessary. The config argument is passed on to start_new
Start a new process with the config argument as the first argument and the key as the second argument.