Skip to content

Commit

Permalink
Setup TokenFetcher for :arena_load_test app
Browse files Browse the repository at this point in the history
  • Loading branch information
AminArria committed Jun 14, 2024
1 parent 0b6c5a0 commit e1da4c4
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
4 changes: 3 additions & 1 deletion apps/arena_load_test/lib/arena_load_test/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ defmodule ArenaLoadTest.Application do
def start(_type, _args) do
children = [
ArenaLoadTest.SocketSupervisor,
ArenaLoadTest.LoadtestManager
ArenaLoadTest.LoadtestManager,
{Finch, name: ArenaLoadTest.Finch},
ArenaLoadTest.TokenFetcher
# Starts a worker by calling: ArenaLoadTest.Worker.start_link(arg)
# {ArenaLoadTest.Worker, arg}
]
Expand Down
47 changes: 47 additions & 0 deletions apps/arena_load_test/lib/arena_load_test/token_fetcher.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
defmodule ArenaLoadTest.TokenFetcher do
@moduledoc """
GenServer that calls gateway to create and refresh a JWT token used by the bots
"""
use GenServer

def get_auth() do
GenServer.call(__MODULE__, :get_auth)
end

def start_link(_args) do
GenServer.start_link(__MODULE__, [], name: __MODULE__)
end

@impl true
def init(_) do
Process.send_after(self(), :fetch_token, 500)
{:ok, %{}}
end

@impl true
def handle_call(:get_auth, _, state) do
{:reply, state, state}
end

@impl true
def handle_info(:fetch_token, state) do
gateway_url = Application.get_env(:arena_load_test, :gateway_url)
secret = :crypto.strong_rand_bytes(32) |> Base.url_encode64
payload = Jason.encode!(%{"bot_secret" => secret})

result =
Finch.build(:post, "#{gateway_url}/auth/generate-bot-token", [{"content-type", "application/json"}], payload)
|> Finch.request(ArenaLoadTest.Finch)

case result do
{:ok, %Finch.Response{status: 200, body: body}} ->
Process.send_after(self(), :fetch_token, 1_800_000)
%{"token" => token} = Jason.decode!(body)
{:noreply, %{token: token, secret: secret}}

_else_error ->
Process.send_after(self(), :fetch_token, 5_000)
{:noreply, state}
end
end
end
3 changes: 2 additions & 1 deletion apps/arena_load_test/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ defmodule ArenaLoadTest.MixProject do
defp deps do
[
{:websockex, "~> 0.4.3"},
{:protobuf, "~> 0.12.0"}
{:protobuf, "~> 0.12.0"},
{:finch, "~> 0.13"}
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"},
# {:sibling_app_in_umbrella, in_umbrella: true}
Expand Down
7 changes: 7 additions & 0 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -401,4 +401,11 @@ config :bot_manager, :end_point_configuration,
plug: BotManager.Endpoint,
options: [port: bot_manager_port]


###################################
# App configuration: Bot Manager #
###################################

config :arena_load_test, :gateway_url, System.get_env("GATEWAY_URL") || "http://localhost:4001"

###################################

0 comments on commit e1da4c4

Please sign in to comment.