Skip to content

Commit 6a6a271

Browse files
committed
Add :peer_applications
1 parent 6c283f6 commit 6a6a271

5 files changed

Lines changed: 30 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## [0.2.1] - 2026-01-27
4+
5+
### Added
6+
7+
- `:peer_applications` - For custom pools, you can decide which applications will be started on the peer nodes. In the default pool this defaults to only `:safe_nif`, and if not passed in to custom pools it will default to `:safe_nif` as well
8+
39
## [0.2.0] - 2026-01-27
410

511
Pooling `:peer` nodes has been implemented!

lib/safe_nif.ex

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@ defmodule SafeNIF do
3131
* `:name` (**required**) - An `t:atom/0` name to give to the pool.
3232
* `:size` - How many workers to put in the pool. Defaults to `System.schedulers_online/0`.
3333
* `:idle_timeout` - How long to allow the pool and nodes to be idle until we start scaling them down.
34+
* `:peer_applications` - A list of applications to start on the peer node, defaulting to just `:safe_nif`.
35+
If you need a custom list of applications that must start on the peer node, make sure to pass their names into the pool.
3436
"""
35-
@type pool_start_opt() :: {:name, atom()} | {:size, pos_integer()} | {:idle_timeout, timeout()}
37+
@type pool_start_opt() ::
38+
{:name, atom()} | {:size, pos_integer()} | {:idle_timeout, timeout()} | {:peer_applications, [atom()]}
3639

3740
@doc false
3841
defdelegate child_spec(args), to: SafeNIF.Pool
@@ -77,6 +80,8 @@ defmodule SafeNIF do
7780
* `:name` (**required**) - An `t:atom/0` name to give to the pool.
7881
* `:size` - How many workers to put in the pool. Defaults to `System.schedulers_online/0`.
7982
* `:idle_timeout` - How long to allow the pool and nodes to be idle until we start scaling them down.
83+
* `:peer_applications` - A list of applications to start on the peer node, defaulting to just `:safe_nif`.
84+
If you need a custom list of applications that must start on the peer node, make sure to pass their names into the pool.
8085
8186
"""
8287
@spec wrap(runnable(), [wrap_opt()]) :: {:ok, term()} | {:error, term()}

lib/safe_nif/application.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ defmodule SafeNIF.Application do
55

66
@impl true
77
def start(_type, _args) do
8-
children = [{SafeNIF, name: SafeNIF.Pool.default_pool_name()}]
8+
children = [{SafeNIF, name: SafeNIF.Pool.default_pool_name(), peer_applications: [:safe_nif]}]
99
opts = [strategy: :one_for_one, name: SafeNIF.Supervisor]
1010
Supervisor.start_link(children, opts)
1111
end

lib/safe_nif/pool.ex

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ defmodule SafeNIF.Pool do
1010

1111
defmodule State do
1212
@moduledoc false
13-
defstruct [:activity_info, :pool_max_idle_time]
13+
@enforce_keys [:activity_info, :pool_max_idle_time, :peer_applications]
14+
defstruct [:activity_info, :pool_max_idle_time, :peer_applications]
1415
end
1516

1617
def default_pool_name do
@@ -69,14 +70,22 @@ defmodule SafeNIF.Pool do
6970
@impl NimblePool
7071
def init_pool(opts) do
7172
idle_timeout = Keyword.get(opts, :idle_timeout, @default_idle_timeout)
72-
pool_state = %__MODULE__.State{activity_info: init_activity_info(), pool_max_idle_time: idle_timeout}
73+
peer_applications = Keyword.get(opts, :peer_applications, [:safe_nif])
74+
75+
pool_state = %__MODULE__.State{
76+
activity_info: init_activity_info(),
77+
pool_max_idle_time: idle_timeout,
78+
peer_applications: peer_applications
79+
}
80+
7381
{:ok, pool_state}
7482
end
7583

7684
@impl NimblePool
7785
def init_worker(%__MODULE__.State{} = pool_state) do
7886
pool_pid = self()
79-
async = fn -> start_peer(pool_pid) end
87+
peer_applications = pool_state.peer_applications
88+
async = fn -> start_peer(pool_pid, peer_applications) end
8089
{:async, async, pool_state}
8190
end
8291

@@ -212,7 +221,7 @@ defmodule SafeNIF.Pool do
212221
Node.spawn_monitor(peer, WrappedCall.call(caller, runnable))
213222
end
214223

215-
defp start_peer(pool_pid) do
224+
defp start_peer(pool_pid, peer_applications) do
216225
node_name = :peer.random_name()
217226
caller = self()
218227

@@ -232,7 +241,7 @@ defmodule SafeNIF.Pool do
232241

233242
add_code_paths(peer)
234243
transfer_configuration(peer)
235-
ensure_apps_started(peer)
244+
ensure_apps_started(peer, peer_applications)
236245
send(pool_pid, {:link_controller, peer})
237246
%__MODULE__{node: peer, controller_pid: controller_pid, last_checkin: System.monotonic_time()}
238247
end
@@ -255,10 +264,8 @@ defmodule SafeNIF.Pool do
255264
end)
256265
end
257266

258-
defp ensure_apps_started(node) do
259-
started_names = Enum.map(Application.started_applications(), fn {name, _, _} -> name end)
260-
261-
Enum.reduce(started_names, MapSet.new(), fn app, started ->
267+
defp ensure_apps_started(node, peer_applications) do
268+
Enum.reduce(peer_applications, MapSet.new(), fn app, started ->
262269
maybe_start_app(node, app, started)
263270
end)
264271
end

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule SafeNIF.MixProject do
22
use Mix.Project
33

4-
@version "0.2.0"
4+
@version "0.2.1"
55
@source_url "https://github.com/probably-not/safe-nif"
66
@homepage_url @source_url
77

0 commit comments

Comments
 (0)