-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from bzzt/develop
Connection Pooling and Refactor
- Loading branch information
Showing
35 changed files
with
536 additions
and
468 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
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
defmodule Bigtable.Admin.Modification do | ||
def create(id) do | ||
@moduledoc false | ||
def create(_id) do | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
defmodule Bigtable.Connection.Auth do | ||
defmodule Bigtable.Auth do | ||
@moduledoc false | ||
|
||
@scopes [ | ||
|
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,77 @@ | ||
defmodule Bigtable.Connection do | ||
@moduledoc false | ||
|
||
use GenServer | ||
@default_endpoint "bigtable.googleapis.com:443" | ||
|
||
## Client API | ||
def start_link(_opts) do | ||
GenServer.start_link(__MODULE__, :ok, name: __MODULE__) | ||
end | ||
|
||
@doc """ | ||
Connects to Bigtable and returns a `GRPC.Channel`. | ||
""" | ||
@spec connect() :: GRPC.Channel.t() | ||
def connect do | ||
GenServer.call(__MODULE__, :connect) | ||
end | ||
|
||
@doc """ | ||
Disconnects from the provided `GRPC.Channel`. | ||
""" | ||
@spec disconnect(GRPC.Channel.t()) :: :ok | ||
def disconnect(channel) do | ||
GenServer.cast(__MODULE__, {:disconnect, channel}) | ||
end | ||
|
||
# Server Callbacks | ||
@spec init(:ok) :: {:ok, map()} | ||
def init(:ok) do | ||
{:ok, %{endpoint: get_endpoint(), opts: build_opts()}} | ||
end | ||
|
||
def handle_call(:connect, _from, %{endpoint: endpoint, opts: opts} = state) do | ||
{:ok, channel} = | ||
GRPC.Stub.connect( | ||
endpoint, | ||
opts | ||
) | ||
|
||
{:reply, channel, state} | ||
end | ||
|
||
def handle_cast({:disconnect, channel}, state) do | ||
GRPC.Stub.disconnect(channel) | ||
{:noreply, state} | ||
end | ||
|
||
def handle_info(_msg, state) do | ||
{:noreply, state} | ||
end | ||
|
||
@spec build_opts() :: list() | ||
defp build_opts do | ||
if Application.get_env(:bigtable, :ssl, true) do | ||
[ | ||
cred: %GRPC.Credential{ | ||
ssl: [] | ||
} | ||
] | ||
else | ||
[] | ||
end | ||
end | ||
|
||
@spec get_endpoint() :: binary() | ||
def get_endpoint do | ||
emulator = System.get_env("BIGTABLE_EMULATOR_HOST") | ||
endpoint = Application.get_env(:bigtable, :endpoint, @default_endpoint) | ||
|
||
if emulator != nil do | ||
emulator | ||
else | ||
endpoint | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
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,40 @@ | ||
defmodule Bigtable.Connection.Worker do | ||
@moduledoc false | ||
alias Bigtable.Connection | ||
use GenServer | ||
|
||
def start_link(_) do | ||
GenServer.start_link(__MODULE__, nil, []) | ||
end | ||
|
||
def get_connection(pid) do | ||
GenServer.call(pid, :get_connection) | ||
end | ||
|
||
def init(_) do | ||
Process.flag(:trap_exit, true) | ||
{:ok, Connection.connect()} | ||
end | ||
|
||
def handle_call(:get_connection, _from, state) do | ||
{:reply, state, state} | ||
end | ||
|
||
def handle_info({:EXIT, _from, reason}, state) do | ||
disconnect(state) | ||
{:stop, reason, state} | ||
end | ||
|
||
def handle_info(_msg, state) do | ||
{:noreply, state} | ||
end | ||
|
||
def terminate(_reason, state) do | ||
disconnect(state) | ||
state | ||
end | ||
|
||
defp disconnect(connection) do | ||
Connection.disconnect(connection) | ||
end | ||
end |
Oops, something went wrong.