-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
61 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
defmodule BullionWeb.RateLimit do | ||
import Phoenix.Controller | ||
import Plug.Conn | ||
require Logger | ||
|
||
@global_bucket_name "any_request" | ||
@bucket_duration 1_000 # 1 second | ||
@request_limit 5 | ||
|
||
def init(_opts) do end | ||
|
||
def call(conn, _opts) do | ||
rate_limit(conn) | ||
end | ||
|
||
def rate_limit(conn, _options \\ []) do | ||
case check_rate(conn) do | ||
{:allow, _count} -> conn | ||
{:deny, _limit} -> render_error(conn) | ||
{:error, _reason} -> render_error(conn) | ||
end | ||
end | ||
|
||
defp check_rate(conn) do | ||
bucket_name(conn) | ||
|> Hammer.check_rate(@bucket_duration, @request_limit) | ||
end | ||
|
||
defp bucket_name(conn) do | ||
principal = case conn.remote_ip do | ||
nil -> "unidentified" | ||
addr -> addr |> Tuple.to_list |> Enum.join(".") | ||
end | ||
@global_bucket_name <> "_principal=" <> principal | ||
end | ||
|
||
defp render_error(conn) do | ||
conn | ||
|> put_status(429) | ||
|> halt | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
<div class="btn white darken-4 col s10 m4"> | ||
<a href="/game/new" style="text-transform:none"> | ||
<a href="<%= Routes.game_path(@conn, :new_game) %>" style="text-transform:none"> | ||
<button>Start a Game</button> | ||
</a> | ||
<br><br><br> | ||
<%= form_for @conn, Routes.game_path(@conn, :view_game), [method: :get], fn f -> %> | ||
<%= text_input f, :shortcode %> | ||
<%= submit "Find Game" %> | ||
<% end %> | ||
</div> | ||
</div> |
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