Skip to content

Commit

Permalink
Enable descentralized counters (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim authored Sep 29, 2024
1 parent 893d1a6 commit cc08ff8
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 20 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ jobs:
matrix:
include:
- pair:
elixir: 1.8.2
otp: 20.3
elixir: 1.11
otp: 23.0
- pair:
elixir: 1.14.3
elixir: 1.17
otp: 25.3
lint: lint
steps:
Expand Down
4 changes: 1 addition & 3 deletions integration_test/ownership/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
excludes = if Version.match?(System.version(), ">= 1.8.0"), do: [], else: [:requires_callers]

ExUnit.start(
capture_log: true,
assert_receive_timeout: 1000,
exclude: [:idle_time, :idle_interval | excludes]
exclude: [:idle_time, :idle_interval]
)

Code.require_file("../../test/test_support.exs", __DIR__)
Expand Down
2 changes: 1 addition & 1 deletion lib/db_connection/connection_pool.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ defmodule DBConnection.ConnectionPool do
def init({mod, opts}) do
DBConnection.register_as_pool(mod)

queue = :ets.new(__MODULE__.Queue, [:protected, :ordered_set])
queue = :ets.new(__MODULE__.Queue, [:protected, :ordered_set, decentralized_counters: true])
ts = {System.monotonic_time(), 0}
{:ok, _} = DBConnection.ConnectionPool.Pool.start_supervised(queue, mod, opts)
target = Keyword.get(opts, :queue_target, @queue_target)
Expand Down
2 changes: 1 addition & 1 deletion lib/db_connection/holder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule DBConnection.Holder do
@spec new(pid, reference, module, term) :: t
def new(pool, ref, mod, state) do
# Insert before setting heir so that pool can't receive empty table
holder = :ets.new(__MODULE__, [:public, :ordered_set])
holder = :ets.new(__MODULE__, [:public, :ordered_set, decentralized_counters: true])

conn = conn(connection: self(), module: mod, state: state, ts: System.monotonic_time())
true = :ets.insert_new(holder, conn)
Expand Down
8 changes: 7 additions & 1 deletion lib/db_connection/ownership/manager.ex
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,13 @@ defmodule DBConnection.Ownership.Manager do
ets =
case Keyword.fetch(owner_opts, :name) do
{:ok, name} when is_atom(name) ->
:ets.new(name, [:set, :named_table, :protected, read_concurrency: true])
:ets.new(name, [
:set,
:named_table,
:protected,
read_concurrency: true,
decentralized_counters: true
])

_ ->
nil
Expand Down
4 changes: 2 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ defmodule DBConnection.Mixfile do

@source_url "https://github.com/elixir-ecto/db_connection"
@pools [:connection_pool, :ownership]
@version "2.7.0"
@version "2.8.0-dev"

def project do
[
app: :db_connection,
version: @version,
elixir: "~> 1.8",
elixir: "~> 1.11",
deps: deps(),
docs: docs(),
description: description(),
Expand Down
20 changes: 11 additions & 9 deletions test/db_connection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,23 @@ defmodule DBConnectionTest do
end

describe "connection_module/1" do
test "returns the connection module when given a pool pid" do
{:ok, pool} = P.start_link([])
setup do
{:ok, agent} = A.start_link([{:ok, :state}, {:idle, :state}, {:idle, :state}])
[agent: agent]
end

test "returns the connection module when given a pool pid", %{agent: agent} do
{:ok, pool} = P.start_link(agent: agent)
assert {:ok, TestConnection} = DBConnection.connection_module(pool)
end

test "returns the connection module when given a pool name", %{test: name} do
{:ok, _pool} = P.start_link(name: name)
test "returns the connection module when given a pool name", %{test: name, agent: agent} do
{:ok, _pool} = P.start_link(name: name, agent: agent)
assert {:ok, TestConnection} = DBConnection.connection_module(name)
end

test "returns the connection module when given a locked connection reference" do
{:ok, agent} = A.start_link([{:ok, :state}, {:idle, :state}, {:idle, :state}])

opts = [agent: agent]
{:ok, pool} = P.start_link(opts)
test "returns the connection module when given a locked connection reference", %{agent: agent} do
{:ok, pool} = P.start_link(agent: agent)

P.run(pool, fn conn ->
assert {:ok, TestConnection} = DBConnection.connection_module(conn)
Expand Down

0 comments on commit cc08ff8

Please sign in to comment.