Named connection manager for InfluxDB instances.
Stores and retrieves initialized connections keyed by name. Values are stored in a persistent term for fast, lock-free reads from any process.
Important
Stored values are initialized connections (the result of
Client.init_connection/1), not raw keyword configs. The type
depends on the configured client implementation:
Client.HTTP— keyword list (host, token, etc.)Client.Local— map with ETS table reference (%{table: _, database: _, profile: _})
Usage
Connections are automatically registered by
InfluxElixir.ConnectionSupervisor during startup. The supervisor
calls Client.init_connection/1 to convert raw config into a
usable connection before storing it here. The facade module
(InfluxElixir) resolves atom names transparently, so most consumer
code simply passes the connection name:
InfluxElixir.health(:trading)
InfluxElixir.write(:trading, "cpu value=1.0", database: "prices")For direct registry access:
{:ok, conn} = InfluxElixir.Connection.get(:trading)
conn = InfluxElixir.Connection.fetch!(:trading)Storage
Uses :persistent_term for near-zero-cost reads. Writes happen only
during connection setup/teardown (rare), so the copy-on-write cost
of :persistent_term is acceptable.
A connection supervisor registers its connection when it starts and
erases it when it stops, whether InfluxElixir.remove_connection/1
stopped it or it gave up after too many restarts: a name never resolves
to a connection that is not running.
Summary
Functions
The server generation a connection writes to: :v2 or :v3.
Removes a connection config by name.
Retrieves the connection config for the given name, raising an
ArgumentError that names the missing connection.
Returns the Finch pool name for a given connection name.
Retrieves the connection config for the given name.
Stores a validated connection config under the given name.
Functions
@spec api_version(term()) :: :v2 | :v3
The server generation a connection writes to: :v2 or :v3.
Takes a connection (a keyword list with :api_version, a Client.Local connection, whose
:v2 profile is InfluxDB 2) or the name of a registered one. Anything else, or a
connection that does not say, is :v3, the default.
Examples
iex> InfluxElixir.Connection.api_version(host: "h", api_version: :v2)
:v2
iex> InfluxElixir.Connection.api_version(host: "h")
:v3
@spec delete(atom()) :: :ok
Removes a connection config by name.
Returns :ok regardless of whether the name existed.
Parameters
name- atom identifying the connection
Retrieves the connection config for the given name, raising an
ArgumentError that names the missing connection.
Parameters
name- atom identifying the connection
Examples
iex> InfluxElixir.Connection.put(:test, host: "localhost", token: "t")
iex> conn = InfluxElixir.Connection.fetch!(:test)
iex> conn[:host]
"localhost"
Returns the Finch pool name for a given connection name.
The pool a registered connection was given with :finch_name, else the one its
supervisor starts (:"influx_elixir_<name>_finch").
Parameters
name- atom identifying the connection
Retrieves the connection config for the given name.
Returns {:ok, connection} or {:error, :not_found}.
Parameters
name- atom identifying the connection
Examples
iex> InfluxElixir.Connection.put(:test, host: "localhost", token: "t")
iex> {:ok, conn} = InfluxElixir.Connection.get(:test)
iex> conn[:host]
"localhost"
Stores a validated connection config under the given name.
Parameters
name- atom identifying the connectionconnection- initialized connection value (fromClient.init_connection/1)
Examples
iex> InfluxElixir.Connection.put(:test, host: "localhost", token: "t")
:ok