Postgres Strategy for libcluster which is used by Supabase on the realtime, supavisor and logflare projects.
You can test it out by running docker compose up
The package can be installed
by adding libcluster_postgres to your list of dependencies in mix.exs:
def deps do
[{:libcluster_postgres, "~> 0.1"}]
endWe connect to a Postgres instance using Postgrex. With the Postgrex.Notifications module we will track for LISTEN events on the configured channel. We'll also use NOTIFY queries to send the node's information.
To use it, set your configuration file with the informations for your database:
config :libcluster,
topologies: [
example: [
strategy: Cluster.Strategy.Postgres,
config: [
hostname: "localhost",
username: "postgres",
password: "postgres",
database: "postgres",
port: 5432,
parameters: [],
channel_name: "cluster"
],
]
]Then add it to your supervision tree:
defmodule MyApp do
use Application
def start(_type, _args) do
topologies = Application.get_env(:libcluster, :topologies)
children = [
# ...
{Cluster.Supervisor, [topologies]}
# ...
]
Supervisor.start_link(children, strategy: :one_for_one)
end
endA special thank you to @gotbones for creating libcluster and @kevinbuch_ for the original inspiration for this strategy.
