Skip to content

Commit

Permalink
Added a grace method to start a table without linking
Browse files Browse the repository at this point in the history
  • Loading branch information
whitfin committed Sep 8, 2016
1 parent c334d71 commit ab3c954
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
29 changes: 26 additions & 3 deletions lib/eternal.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ defmodule Eternal do
## Examples
iex> Eternal.new(:table1)
iex> Eternal.start_link(:table1)
{ :ok, _pid1 }
iex> Eternal.new(:table2, [ :compressed ])
iex> Eternal.start_link(:table2, [ :compressed ])
{ :ok, _pid2 }
iex> Eternal.new(:table3, [ ], [ quiet: true ])
iex> Eternal.start_link(:table3, [ ], [ quiet: true ])
{ :ok, _pid3 }
"""
Expand All @@ -65,6 +65,29 @@ defmodule Eternal do
end
end

@doc """
Functionally equivalent to `start_link/3`, except that the link to the starting
process is removed after the table is started.
## Examples
iex> Eternal.start(:table1)
{ :ok, _pid1 }
iex> Eternal.start(:table2, [ :compressed ])
{ :ok, _pid2 }
iex> Eternal.start(:table3, [ ], [ quiet: true ])
{ :ok, _pid3 }
"""
@spec start(name :: atom, ets_opts :: Keyword.t, opts :: Keyword.t) :: on_start
def start(name, ets_opts \\ [], opts \\ []) when is_opts(name, ets_opts, opts) do
with { :ok, pid } = v <- start_link(name, ets_opts, opts) do
:erlang.unlink(pid) && v
end
end

@doc false
# As of v1.1, this function is deprecated and you should use `start_link/3` or
# `start/3`. It still exists only to support semantic versioning.
Expand Down
10 changes: 10 additions & 0 deletions test/eternal_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ defmodule EternalTest do
assert(:ets.info(:table_with_options, :compressed) == true)
end

test "starting a table with no link" do
spawn(fn ->
Eternal.start(:unlinked, [], [ quiet: true ])
end)

:timer.sleep(25)

assert(:unlinked in :ets.all())
end

test "recovering from a stopd owner" do
tab = create(:recover_stopd_owner)

Expand Down

0 comments on commit ab3c954

Please sign in to comment.