basic admin endgame view and add (#3)

Reviewed-on: #3
This commit was merged in pull request #3.
This commit is contained in:
2026-06-22 18:44:52 +00:00
parent e955e40930
commit b47fcdffcb
82 changed files with 2031 additions and 2 deletions
+55
View File
@@ -0,0 +1,55 @@
defmodule Chesstrainer.Ratelimiters.Lichess do
@moduledoc """
Global rate limiter for 429 response codes from the Lichess API.
"""
@cooldown_seconds 60
@table :lichess_cooldown
def add_cooldown do
ensure_table()
insert_cooldown()
end
def reset_cooldown do
ensure_table()
:ets.insert(@table, {:until, nil})
end
def check_cooldown do
ensure_table()
current_ms = System.system_time(:millisecond)
case :ets.lookup(@table, :until) do
[{:until, nil}] ->
{:ok, 0}
[{:until, until_ms}] ->
remaining = until_ms - current_ms
if remaining <= 0 do
reset_cooldown()
{:ok, 0}
else
{:cooldown, remaining}
end
end
end
defp create do
:ets.new(@table, [:named_table, :public, :set])
:ets.insert(@table, {:until, nil})
end
defp ensure_table do
case :ets.whereis(@table) do
:undefined -> create()
_ -> :ok
end
end
defp insert_cooldown do
until_ms = System.system_time(:millisecond) + @cooldown_seconds * 1000
:ets.insert(@table, {:until, until_ms})
end
end