Skip to content
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

Update code for loadtests #764

Merged
merged 7 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/loadtest-brazil-server-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ jobs:
RELEASE: arena
PHX_SERVER: ${{ vars.PHX_SERVER }}
PHX_HOST: ${{ vars.LOADTEST_SERVER_HOST }}
OVERRIDE_JWT: ${{ vars.OVERRIDE_JWT }}
PORT: ${{ vars.ARENA_PORT }}
GATEWAY_URL: ${{ vars.GATEWAY_URL }}
BOT_MANAGER_PORT: ${{ vars.BOT_MANAGER_PORT }}
Expand All @@ -65,6 +66,7 @@ jobs:
RELEASE=${RELEASE} \
PHX_SERVER=${PHX_SERVER} \
PHX_HOST=${PHX_HOST} \
OVERRIDE_JWT=${OVERRIDE_JWT} \
PORT=${PORT} \
GATEWAY_URL=${GATEWAY_URL} \
BOT_MANAGER_PORT=${BOT_MANAGER_PORT} \
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/loadtest-europe-server-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ jobs:
RELEASE: arena
PHX_SERVER: ${{ vars.PHX_SERVER }}
PHX_HOST: ${{ vars.LOADTEST_SERVER_HOST }}
OVERRIDE_JWT: ${{ vars.OVERRIDE_JWT }}
PORT: ${{ vars.ARENA_PORT }}
GATEWAY_URL: ${{ vars.GATEWAY_URL }}
BOT_MANAGER_PORT: ${{ vars.BOT_MANAGER_PORT }}
Expand All @@ -64,6 +65,7 @@ jobs:
RELEASE=${RELEASE} \
PHX_SERVER=${PHX_SERVER} \
PHX_HOST=${PHX_HOST} \
OVERRIDE_JWT=${OVERRIDE_JWT} \
PORT=${PORT} \
GATEWAY_URL=${GATEWAY_URL} \
BOT_MANAGER_PORT=${BOT_MANAGER_PORT} \
Expand Down
2 changes: 1 addition & 1 deletion apps/arena/lib/arena/game_socket_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule Arena.GameSocketHandler do
@impl true
def init(req, _opts) do
## TODO: The only reason we need this is because bots are broken, we should fix bots in a way that
## we don't need to pass a real user_id (or none at all). Ideally we could have JWT that says "Bot Sever".
## we don't need to pass a real user_id (or none at all). Ideally we could have JWT that says "Bot Server".
client_id =
case :cowboy_req.parse_qs(req) do
[{"gateway_jwt", jwt}] ->
Expand Down
16 changes: 13 additions & 3 deletions apps/arena/lib/arena/socket_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,19 @@ defmodule Arena.SocketHandler do

@impl true
def init(req, _opts) do
[{"gateway_jwt", jwt}] = :cowboy_req.parse_qs(req)
signer = GatewaySigner.get_signer()
{:ok, %{"sub" => user_id}} = GatewayTokenManager.verify_and_validate(jwt, signer)
# TODO: We need to mock user_id validation for bots (loadtests are broken).
# Ideally we could have JWT that says "Bot Server".
# https://github.com/lambdaclass/mirra_backend/issues/765
user_id =
if System.get_env("OVERRIDE_JWT") == "true" do
:cowboy_req.binding(:client_id, req)
else
[{"gateway_jwt", jwt}] = :cowboy_req.parse_qs(req)
signer = GatewaySigner.get_signer()
{:ok, %{"sub" => user_id}} = GatewayTokenManager.verify_and_validate(jwt, signer)
user_id
end

character_name = :cowboy_req.binding(:character_name, req)
player_name = :cowboy_req.binding(:player_name, req)
{:cowboy_websocket, req, %{client_id: user_id, character_name: character_name, player_name: player_name}}
Expand Down
43 changes: 26 additions & 17 deletions apps/arena_load_test/lib/arena_load_test/socket_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,26 @@ defmodule ArenaLoadTest.SocketHandler do

@impl true
def handle_frame({:binary, game_state}, state) do
game_id = Serialization.GameState.decode(game_state).game_id
game_msg = Serialization.LobbyEvent.decode(game_state)

case :ets.lookup(:clients, state.client_id) do
[{client_id, _}] ->
:ets.delete(:clients, client_id)

[] ->
raise KeyError, message: "Client with ID #{state.client_id} doesn't exist."
end

{:ok, pid} =
SocketSupervisor.add_new_player(
state.client_id,
game_id
)
case game_msg.event do
{:game, game_state} ->
game_id = game_state.game_id
remove_client_from_lobby_ets_table(state.client_id)

true = :ets.insert(:players, {state.client_id, game_id})
{:ok, pid} =
SocketSupervisor.add_new_player(
state.client_id,
game_id
)

Process.send(pid, :send_action, [])
true = :ets.insert(:players, {state.client_id, game_id})
Process.send(pid, :send_action, [])
{:ok, state}

{:ok, state}
{_event, _event_state} ->
{:ok, state}
end
end

# Private
Expand All @@ -75,4 +74,14 @@ defmodule ArenaLoadTest.SocketHandler do
["muflus", "h4ck", "uma"]
|> Enum.random()
end

defp remove_client_from_lobby_ets_table(client_id) do
case :ets.lookup(:clients, client_id) do
[{client_id, _}] ->
:ets.delete(:clients, client_id)

[] ->
raise KeyError, message: "Client with ID #{client_id} doesn't exist."
end
end
end
Loading