Rate limit game creation

This commit is contained in:
Logan Hunt 2023-01-31 12:44:15 -07:00
parent 4394d4721c
commit e5d97870a1
No known key found for this signature in database
GPG Key ID: 8AC6A4B840C0EC49
3 changed files with 65 additions and 33 deletions

View File

@ -12,7 +12,9 @@ config :chessh, RateLimits,
max_concurrent_user_sessions: 5, max_concurrent_user_sessions: 5,
player_session_message_burst_ms: 500, player_session_message_burst_ms: 500,
player_session_message_burst_rate: 8, player_session_message_burst_rate: 8,
player_public_keys: 15 player_public_keys: 15,
create_game_ms: 60 * 1000,
create_game_rate: 2
config :chessh, Web, config :chessh, Web,
github_oauth_login_url: "https://github.com/login/oauth/access_token", github_oauth_login_url: "https://github.com/login/oauth/access_token",

View File

@ -45,17 +45,20 @@ defmodule Chessh.SSH.Client do
screen_state_initials: screen_state_initials screen_state_initials: screen_state_initials
} = state } = state
) do ) do
{:ok, new_screen_pid} = case GenServer.start_link(module, [%{screen_state_initial | client_pid: self()}]) do
GenServer.start_link(module, [%{screen_state_initial | client_pid: self()}]) {:ok, new_screen_pid} ->
send(new_screen_pid, {:render, width, height})
send(new_screen_pid, {:render, width, height}) {:noreply,
%State{
state
| screen_pid: new_screen_pid,
screen_state_initials: [{module, screen_state_initial} | screen_state_initials]
}}
{:noreply, _ ->
%State{ {:noreply, state}
state end
| screen_pid: new_screen_pid,
screen_state_initials: [{module, screen_state_initial} | screen_state_initials]
}}
end end
@impl true @impl true

View File

@ -59,32 +59,59 @@ defmodule Chessh.SSH.Client.Game do
end end
def init([ def init([
%State{player_session: player_session, color: color, game: nil} = state %State{player_session: player_session, color: color, game: nil, client_pid: client_pid} =
state
| tail | tail
]) do ]) do
# Starting a new game [create_game_ms, create_game_rate] =
{:ok, %Game{} = game} = Application.get_env(:chessh, RateLimits)
Game.changeset( |> Keyword.take([:create_game_ms, :create_game_rate])
%Game{}, |> Keyword.values()
Map.merge(
if(color == :light,
do: %{light_player_id: player_session.player_id},
else: %{dark_player_id: player_session.player_id}
),
%{
fen: @default_fen
}
)
)
|> Repo.insert()
init([ case Hammer.check_rate_inc(
%State{ :redis,
state "player-#{state.player_session.id}-create-game-rate",
| game: game create_game_ms,
} create_game_rate,
| tail 1
]) ) do
{:allow, _count} ->
# Starting a new game
{:ok, %Game{} = game} =
Game.changeset(
%Game{},
Map.merge(
if(color == :light,
do: %{light_player_id: player_session.player_id},
else: %{dark_player_id: player_session.player_id}
),
%{
fen: @default_fen
}
)
)
|> Repo.insert()
init([
%State{
state
| game: game
}
| tail
])
{:deny, _limit} ->
send(
client_pid,
{:send_to_ssh,
[
Utils.clear_codes(),
"You are creating too many games, and have been rate limited. Try again later.\n"
]}
)
{:stop, :normal, state}
end
end end
def init([ def init([