-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce internal connection pool for clusters
- Loading branch information
1 parent
6dd035b
commit c47ff39
Showing
9 changed files
with
140 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
defmodule Xandra.Cluster.ConnectionPool do | ||
@moduledoc false | ||
|
||
use Supervisor | ||
|
||
@spec start_link(keyword()) :: Supervisor.on_start() | ||
def start_link(opts) when is_list(opts) do | ||
connection_opts = Keyword.fetch!(opts, :connection_options) | ||
pool_size = Keyword.fetch!(opts, :pool_size) | ||
Supervisor.start_link(__MODULE__, {connection_opts, pool_size}) | ||
end | ||
|
||
@spec checkout(pid()) :: pid() | ||
def checkout(sup_pid) when is_pid(sup_pid) do | ||
pids = | ||
for {_id, pid, _type, _modules} when is_pid(pid) <- Supervisor.which_children(sup_pid) do | ||
pid | ||
end | ||
|
||
Enum.random(pids) | ||
end | ||
|
||
## Callbacks | ||
|
||
@impl true | ||
def init({connection_opts, pool_size}) do | ||
children = | ||
for index <- 1..pool_size do | ||
Supervisor.child_spec({Xandra, connection_opts}, id: {Xandra, index}) | ||
end | ||
|
||
Supervisor.init(children, strategy: :one_for_one) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
defmodule Xandra.Cluster.ConnectionPoolTest do | ||
use ExUnit.Case, async: true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.