An Ash Datalayer backed by individual GenServers.
This package provides an
Ash.DataLayer which
stores resources in emphemeral GenServers. The main use-case for this is two fold:
- Ability to automatically remove resources after an inactivity timeout.
- (Potential) ability to migrate resources across a cluster during deploys to allow access to continue without failure.
- before and after hooks for changesets and queries are run within the server process, making registration, etc, possible.
-
When a resource using this datalayer is created it spawns an instance of
AshGenServer.Serverand performs all operations on the data within it. This means that your actions must pay the price of aGenServer.call/3to read or modify the data. -
When destroying a resource it's process is terminated and it's internal state is lost.
-
If, for some reason, the
AshGenServer.Serverprocess crashes or exits for an abnormal reason the supervisor will restart it with the changeset used by thecreateaction - this means that any updates performed since creation will be lost. -
Any resource using this data source must have at least one primary key field.
-
Retrieving a resource by primary key is an optimised case, but any other queries will pay the price of having to query every
AshGenServer.Serverprocess in sequence.
If available in Hex, the package can be installed
by adding ash_gen_server to your list of dependencies in mix.exs:
def deps do
[
{:ash_gen_server, "~> 0.3.0"}
]
endThis package assumes that you have Ash installed and configured. See the Ash documentation for details.
Once installed you can easily define a resource which is backed by a GenServer:
defmodule MyApp.EphemeralResource do
use Ash.Resource, data_layer: AshGenServer.DataLayer
attributes do
uuid_primary_key :id
attribute :temporary_data, :string
end
endDocumentation for the latest release will be available on
hexdocs and for the main
branch.
- To contribute updates, fixes or new features please fork and open a
pull-request against
main. - Please use conventional commits - this allows us to dynamically generate the changelog.
- Feel free to ask any questions on out GitHub discussions page.