-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: provides repeated event protection
- Loading branch information
Showing
4 changed files
with
44 additions
and
13 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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
defmodule Tower.Protection do | ||
use Agent | ||
|
||
@empty_state %{} | ||
|
||
def start_link(_initial_value) do | ||
Agent.start_link(fn -> @empty_state end, name: __MODULE__) | ||
end | ||
|
||
def get(key) do | ||
Agent.get(__MODULE__, fn state -> Map.get(state, key) end) | ||
end | ||
|
||
def seen(key) do | ||
Agent.update( | ||
__MODULE__, | ||
fn state -> | ||
{_, new_state} = | ||
Map.get_and_update(state, key, fn current_value -> | ||
{current_value, (current_value || 0) + 1} | ||
end) | ||
|
||
new_state | ||
end | ||
) | ||
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