-
Notifications
You must be signed in to change notification settings - Fork 119
Smarter hibernate #103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Smarter hibernate #103
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
471f190
only hibernate when backoff and timeout
redink 2e065e6
use idle_hibernate as pure options
redink e658003
add test case for idle hibernate
redink dbb2368
update unit test via cr
redink 650be16
add test for hibernate
redink 5b3c7cb
update unit test for hibernate
redink e159049
use tag rather than Mix.env and add hibernate timer
redink 95d0a58
modified via cr
redink 3144264
disable idle hibernate test when sojourn
redink File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -18,9 +18,10 @@ defmodule DBConnection.Connection do | |
| require Logger | ||
| alias DBConnection.Backoff | ||
|
|
||
| @pool_timeout 5_000 | ||
| @timeout 15_000 | ||
| @idle_timeout 1_000 | ||
| @pool_timeout 5_000 | ||
| @timeout 15_000 | ||
| @idle_timeout 1_000 | ||
| @idle_hibernate false | ||
|
|
||
| ## DBConnection.Pool API | ||
|
|
||
|
|
@@ -108,12 +109,14 @@ defmodule DBConnection.Connection do | |
|
|
||
| s = %{mod: mod, opts: opts, state: nil, client: :closed, broker: broker, | ||
| regulator: regulator, lock: nil, queue: queue, timer: nil, | ||
| idle_timer: nil, | ||
| backoff: Backoff.new(opts), | ||
| after_connect: Keyword.get(opts, :after_connect), | ||
| after_connect_timeout: Keyword.get(opts, :after_connect_timeout, | ||
| @timeout), idle: idle, | ||
| idle_timeout: Keyword.get(opts, :idle_timeout, @idle_timeout), | ||
| idle_time: 0, after_timeout: after_timeout} | ||
| idle_time: 0, after_timeout: after_timeout, | ||
| idle_hibernate: Keyword.get(opts, :idle_hibernate, @idle_hibernate)} | ||
| if mode == :connection and Keyword.get(opts, :sync_connect, false) do | ||
| connect(:init, s) | ||
| else | ||
|
|
@@ -130,7 +133,7 @@ defmodule DBConnection.Connection do | |
| def connect(_, s) do | ||
| %{mod: mod, opts: opts, backoff: backoff, after_connect: after_connect, | ||
| idle: idle, idle_timeout: idle_timeout, regulator: regulator, | ||
| lock: lock} = s | ||
| lock: lock, idle_hibernate: idle_hibernate} = s | ||
| case apply(mod, :connect, [opts]) do | ||
| {:ok, state} when after_connect != nil -> | ||
| ref = make_ref() | ||
|
|
@@ -153,7 +156,7 @@ defmodule DBConnection.Connection do | |
| end) | ||
| done_lock(regulator, lock) | ||
| {timeout, backoff} = Backoff.backoff(backoff) | ||
| {:backoff, timeout, %{s | lock: nil, backoff: backoff}} | ||
| maybe_hibernate(idle_hibernate, {:backoff, timeout, %{s | lock: nil, backoff: backoff}}) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should test this part with the other backoff handling |
||
| end | ||
| end | ||
|
|
||
|
|
@@ -335,6 +338,17 @@ defmodule DBConnection.Connection do | |
| end | ||
| end | ||
|
|
||
| def handle_info({:timeout, timer, {__MODULE__, :idle, _}}, s) | ||
| when is_reference(timer) do | ||
| %{mod: mod, state: state} = s | ||
| case apply(mod, :ping, [state]) do | ||
| {:ok, state} -> | ||
| handle_timeout(%{s | state: state, idle_timer: nil}) | ||
| {:disconnect, err, state} -> | ||
| {:disconnect, {:log, err}, %{s | state: state, idle_timer: nil}} | ||
| end | ||
| end | ||
|
|
||
| def handle_info({:timeout, timer, {__MODULE__, pid, timeout}}, | ||
| %{timer: timer} = s) when is_reference(timer) do | ||
| message = "client #{inspect pid} timed out because " <> | ||
|
|
@@ -538,10 +552,10 @@ defmodule DBConnection.Connection do | |
| {:noreply, s} | ||
| end | ||
|
|
||
| defp continue_ping(%{mod: mod, state: state} = s) do | ||
| defp continue_ping(%{mod: mod, state: state, idle_hibernate: idle_hibernate} = s) do | ||
| case apply(mod, :ping, [state]) do | ||
| {:ok, state} -> | ||
| continue_ask(%{s | idle_time: 0, state: state}) | ||
| maybe_hibernate(idle_hibernate, continue_ask(%{s | idle_time: 0, state: state})) | ||
| {:disconnect, err, state} -> | ||
| {:disconnect, {:log, err}, %{s | idle_time: 0, state: state}} | ||
| end | ||
|
|
@@ -563,10 +577,22 @@ defmodule DBConnection.Connection do | |
| end | ||
| end | ||
|
|
||
| defp handle_timeout(%{client: nil, idle_timeout: idle_timeout, | ||
| idle_hibernate: true, | ||
| idle_timer: idle_timer} = s) do | ||
| cancel_timer(idle_timer) | ||
| idle_timer = start_timer(:idle, idle_timeout) | ||
| maybe_hibernate(true, {:noreply, %{s | idle_timer: idle_timer}}) | ||
| end | ||
| defp handle_timeout(%{client: nil, idle_timeout: idle_timeout} = s) do | ||
| {:noreply, s, idle_timeout} | ||
| end | ||
| defp handle_timeout(s), do: {:noreply, s} | ||
| defp handle_timeout(%{idle_hibernate: idle_hibernate} = s) do | ||
| maybe_hibernate(idle_hibernate, {:noreply, s}) | ||
| end | ||
|
|
||
| defp maybe_hibernate(true, res), do: Tuple.append(res, :hibernate) | ||
| defp maybe_hibernate(_, res), do: res | ||
|
|
||
| defp demonitor({_, mon}) when is_reference(mon) do | ||
| Process.demonitor(mon, [:flush]) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If theres enough logic to share a function we could try to simplify the test when hibenating and not share function. We might have overlapping tests.