-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- separate the genserver from the behavior - add unit testing for token sweeper - ran mix format - fix hex.pm files - add extra configs for docs
- Loading branch information
Showing
13 changed files
with
216 additions
and
151 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[ | ||
inputs: ["mix.exs", "{config,lib,test,priv}/**/*.{ex,exs}"] | ||
] |
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 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
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,44 @@ | ||
defmodule Guardian.DB.Token.Sweeper do | ||
@moduledoc """ | ||
Purges and schedule work for cleaning up expired tokens. | ||
""" | ||
|
||
alias Guardian.DB.Token | ||
|
||
@doc """ | ||
Purges the expired tokens and schedule the next purge. | ||
""" | ||
def sweep(pid, state) do | ||
Token.purge_expired_tokens() | ||
schedule_work(pid, state) | ||
end | ||
|
||
@doc """ | ||
Schedule the next purge. | ||
""" | ||
def schedule_work(pid, state) do | ||
if state[:timer] do | ||
Process.cancel_timer(state.timer) | ||
end | ||
|
||
timer = Process.send_after(pid, :sweep, state[:interval]) | ||
Map.merge(state, %{timer: timer}) | ||
end | ||
|
||
@doc false | ||
def get_interval do | ||
:guardian | ||
|> Application.get_env(Guardian.DB) | ||
|> Keyword.get(:sweep_interval, 60) | ||
|> minute_to_ms() | ||
end | ||
|
||
defp minute_to_ms(value) when is_binary(value) do | ||
value | ||
|> String.to_integer() | ||
|> minute_to_ms() | ||
end | ||
|
||
defp minute_to_ms(value) when value < 1, do: 1000 | ||
defp minute_to_ms(value), do: round(value * 60 * 1000) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
defmodule Guardian.DB.Token.SweeperServer do | ||
@moduledoc """ | ||
Periocially purges expired tokens from the DB. | ||
## Example | ||
config :guardian, Guardian.DB, | ||
sweep_interval: 60 # 1 hour | ||
# in your supervisor | ||
worker(Guardian.DB.Token.SweeperServer, []) | ||
""" | ||
|
||
use GenServer | ||
alias Guardian.DB.Token.Sweeper | ||
|
||
def start_link(opts \\ []) do | ||
defaults = %{ | ||
interval: Sweeper.get_interval() | ||
} | ||
|
||
state = Enum.into(opts, defaults) | ||
|
||
GenServer.start_link(__MODULE__, state, name: __MODULE__) | ||
end | ||
|
||
@doc """ | ||
Reset the purge timer. | ||
""" | ||
def reset_timer do | ||
GenServer.call(__MODULE__, :reset_timer) | ||
end | ||
|
||
@doc """ | ||
Manually trigger a database purge of expired tokens. Also resets the current | ||
scheduled work. | ||
""" | ||
def purge do | ||
GenServer.call(__MODULE__, :sweep) | ||
end | ||
|
||
def init(state) do | ||
{:ok, Sweeper.schedule_work(self(), state)} | ||
end | ||
|
||
def handle_call(:reset_timer, _from, state) do | ||
{:reply, :ok, Sweeper.schedule_work(self(), state)} | ||
end | ||
|
||
def handle_call(:sweep, _from, state) do | ||
{:reply, :ok, Sweeper.sweep(self(), state)} | ||
end | ||
|
||
def handle_info(:sweep, state) do | ||
{:noreply, Sweeper.sweep(self(), state)} | ||
end | ||
|
||
def handle_info(_, state), do: {:noreply, state} | ||
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.