Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more registry functions #2

Merged
merged 4 commits into from
Feb 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Ragistry

[![Documentation badge](https://img.shields.io/badge/Hexdocs-ff6900)](https://hexdocs.pm/ragistry/)
[![Hex.pm badge](https://img.shields.io/badge/hex.pm-informational)](https://hex.pm/packages/ragistry)

A work-in-progress distributed process registry system built on top of the [Ra](https://github.com/rabbitmq/ra) implementation of Raft.

## Usage
Expand Down
39 changes: 14 additions & 25 deletions lib/ragistry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,24 @@ defmodule Ragistry do
processes terminate, maintaining consistency across the cluster.
"""

@doc """
Starts a new registry instance.

## Options
* `:name` - Required. The name to register the registry under.
"""
defdelegate start_link(opts), to: Ragistry.Cluster

@doc """
Registers the current process in the registry under the given key with associated value.

Returns `:ok` if successful, or `{:error, :already_registered}` if the key is already taken.
"""
defdelegate child_spec(opts), to: Ragistry.Cluster
defdelegate register(name, key, value), to: Ragistry.Machine

@doc """
Looks up processes registered under the given key.

Returns a list of tuples containing `{pid, value}` for all matching processes,
or an empty list if no process is registered under the given key.
"""
defdelegate lookup(name, key), to: Ragistry.Machine

@doc """
Unregisters the current process for the given key.

Returns `:ok` if successful.
"""
defdelegate unregister(name, key), to: Ragistry.Machine
defdelegate count(name), to: Ragistry.Machine
defdelegate count_match(name, key, pattern, guards \\ []), to: Ragistry.Machine
defdelegate count_select(name, spec), to: Ragistry.Machine
defdelegate dispatch(name, key, mfa), to: Ragistry.Machine
defdelegate keys(name, pid), to: Ragistry.Machine
defdelegate match(name, key, pattern, guards \\ []), to: Ragistry.Machine
defdelegate meta(name, key), to: Ragistry.Machine
defdelegate put_meta(name, key, value), to: Ragistry.Machine
defdelegate delete_meta(name, key), to: Ragistry.Machine
defdelegate select(name, spec), to: Ragistry.Machine
defdelegate unregister_match(name, key, pattern, guards \\ []), to: Ragistry.Machine
defdelegate update_value(name, key, value), to: Ragistry.Machine
defdelegate values(name, key, pid), to: Ragistry.Machine

@doc false
defdelegate whereis_name(via), to: Ragistry.Machine
Expand Down
11 changes: 8 additions & 3 deletions lib/ragistry/cluster.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
defmodule Ragistry.Cluster do
@moduledoc false

use GenServer
require Logger

Expand All @@ -10,6 +8,14 @@ defmodule Ragistry.Cluster do
GenServer.start_link(__MODULE__, opts, name: __MODULE__)
end

def child_spec(opts) do
%{
id: opts[:name] || __MODULE__,
start: {Ragistry, :start_link, [opts]},
type: :supervisor
}
end

def init(opts) do
name = opts[:name]
type = Keyword.get(opts, :type, :unique)
Expand All @@ -33,7 +39,6 @@ defmodule Ragistry.Cluster do
members ->
# Join existing cluster through any member
[existing_member | _] = members -- [self()]
# TODO Rename from primary
existing_node = node(existing_member)
Logger.debug("Joining existing cluster through #{inspect(existing_member)}")

Expand Down
Loading