NoNoncense.MachineId.Strategy.SqlLease (NoNoncense v2.0.0)

Copy Markdown View Source

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) - the Ecto.Repo to 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

migrate_opt()

@type migrate_opt() :: {:table_name, String.t()}

opt()

@type opt() :: {:table, String.t()} | {:repo, module()}

Functions

migrate(opts \\ [])

@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_name override the name of the leases table. Must be passed to the strategy as well if overridden.