Skip to content

Commit

Permalink
wip: tweak args, add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinfarris committed Mar 9, 2021
1 parent 02b0d3d commit 0bfc5dc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 24 deletions.
12 changes: 10 additions & 2 deletions lib/snowflex/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ defmodule Snowflex.Connection do
When used, the connection expects the `:otp_app` option. You may also define a standard timeout. This will default to 60 seconds.
If `keep_alive?` is set to `true`, each worker in the connection pool will
periodically send a dummy query to Snowflake to keep the authenticated
session from expiring.
```
defmodule SnowflakeConnection do
use Snowflex.Connection,
otp_app: :my_app,
timeout: :timer.seconds(60)
timeout: :timer.seconds(60),
keep_alive?: true
end
```
Expand Down Expand Up @@ -86,6 +91,7 @@ defmodule Snowflex.Connection do
# setup compile time config
otp_app = Keyword.fetch!(opts, :otp_app)
timeout = Keyword.get(opts, :timeout, :timer.seconds(60))
keep_alive? = Keyword.get(opts, :keep_alive?, false)

@otp_app otp_app
@name __MODULE__
Expand All @@ -94,6 +100,8 @@ defmodule Snowflex.Connection do
max: 10,
min: 5
]
@keep_alive? keep_alive?
@heartbeat_interval :timer.hours(3)

def child_spec(_) do
config = Application.get_env(@otp_app, __MODULE__, [])
Expand All @@ -113,7 +121,7 @@ defmodule Snowflex.Connection do
{:max_overflow, min_pool_size}
]

:poolboy.child_spec(@name, opts, connection)
:poolboy.child_spec(@name, opts, {connection, @keep_alive?, @heartbeat_interval})
end

@impl Snowflex.Connection
Expand Down
14 changes: 2 additions & 12 deletions lib/snowflex/worker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,9 @@ defmodule Snowflex.Worker do
## GENSERVER CALL BACKS

@impl GenServer
def init(connection_args) do
{keep_alive?, connection_args} = Keyword.pop(connection_args, :keep_alive?, false)

{heartbeat_interval, connection_args} =
Keyword.pop(connection_args, :heartbeat_interval, :timer.hours(3))

def init({connection_args, keep_alive?, heartbeat_interval}) do
send(self(), {:start, connection_args, keep_alive?, heartbeat_interval})

{:ok,
%{
backoff: :backoff.init(2, 60),
state: :not_connected
}}
{:ok, %{backoff: :backoff.init(2, 60), state: :not_connected}}
end

@impl GenServer
Expand Down
14 changes: 4 additions & 10 deletions test/snowflex/worker_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,29 @@ defmodule Snowflex.WorkerTest do
end

test "does not send a heartbeat if `keep_alive?` is false" do
start_supervised!({Snowflex.Worker, @connection_args})
start_supervised!({Snowflex.Worker, {@connection_args, false, 10}})
Process.sleep(15)

assert :meck.num_calls(:odbc, :sql_query, ["mock pid", 'SELECT 1']) == 0
end

test "sends heartbeat every interval if `keep_alive?` is true" do
connection_args = @connection_args ++ [keep_alive?: true, heartbeat_interval: 10]

assert capture_log(fn ->
start_supervised!({Snowflex.Worker, connection_args})
start_supervised!({Snowflex.Worker, {@connection_args, true, 10}})
Process.sleep(30)
end) =~ "sending heartbeat"

assert :meck.num_calls(:odbc, :sql_query, ["mock pid", 'SELECT 1']) > 1
end

test "postpones heartbeat if any other sql query is sent" do
connection_args = @connection_args ++ [keep_alive?: true, heartbeat_interval: 10]

:meck.expect(:odbc, :sql_query, fn "mock pid", 'SELECT * FROM my_table' ->
{:selected, ['name'], [{'dustin'}]}
end)

refute(
capture_log(fn ->
worker = start_supervised!({Snowflex.Worker, connection_args})
worker = start_supervised!({Snowflex.Worker, {@connection_args, true, 10}})
Process.sleep(7)
Snowflex.Worker.sql_query(worker, "SELECT * FROM my_table")
Process.sleep(7)
Expand All @@ -59,8 +55,6 @@ defmodule Snowflex.WorkerTest do
end

test "postpones heartbeat if any other params query is sent" do
connection_args = @connection_args ++ [keep_alive?: true, heartbeat_interval: 10]

:meck.expect(:odbc, :param_query, fn "mock pid",
'SELECT * FROM my_table WHERE name=?',
[{{:sql_varchar, 250}, ['dustin']}] ->
Expand All @@ -69,7 +63,7 @@ defmodule Snowflex.WorkerTest do

refute(
capture_log(fn ->
worker = start_supervised!({Snowflex.Worker, connection_args})
worker = start_supervised!({Snowflex.Worker, {@connection_args, true, 10}})
Process.sleep(7)

Snowflex.Worker.param_query(worker, "SELECT * FROM my_table WHERE name=?", [
Expand Down

0 comments on commit 0bfc5dc

Please sign in to comment.