Strategy to acquire a machine ID lease from a SQL database (Postgres or MySQL), coordinating uniqueness across nodes through a single table.
Each machine ID (0..511) is a row in the table; a node acquires the lowest available (i.e.
already-expired) row and holds it by keeping its expires_at in the future with a random
token, renewing it periodically.
Requires a table with one pre-populated row per possible machine ID - see the migration below.
Configure the strategy when starting NoNoncense.MachineId and add it after your Repo:
children = [
...
MyApp.Repo,
{NoNoncense.MachineId,
strategy: NoNoncense.MachineId.Strategy.SqlLease,
strategy_opts: [repo: MyApp.Repo],
instances: [[base_key: System.fetch_env!("BASE_KEY")]]}
]Options
:repo(required) - theEcto.Repoto run queries against:table- the table leases are stored in (default"no_noncense_leases")
Migration
A convenience helper migrate/1 has been added to this module to help you write a migration. The table name is overridable using option :table_name.
defmodule MyApp.Repo.Migrations.NoNoncenseLeases do
use Ecto.Migration
def change, do: NoNoncense.MachineId.Strategy.SqlLease.migrate()
end
Summary
Functions
Convenience helper for creating the leases table in an Ecto migration.
Types
Functions
@spec migrate([migrate_opt()]) :: term()
Convenience helper for creating the leases table in an Ecto migration.
Creates table "no_noncense_leases" (overridable) with id, token, expires_at and lock_version columns, and preseeds all 512 IDs.
Options
:table_nameoverride the name of the leases table. Must be passed to the strategy as well if overridden.