From de70439f3ee9b2a82b750cd0b4393f56e6c8917c Mon Sep 17 00:00:00 2001 From: cynic Date: Wed, 24 Aug 2022 01:31:30 -0400 Subject: [PATCH] ratelimiter --- lib/tir_inna_noc/imageboard.ex | 16 ++++++++++++---- lib/tir_inna_noc/rate.ex | 23 +++++++++++++++++++++++ lib/tir_inna_noc/sup.ex | 1 + 3 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 lib/tir_inna_noc/rate.ex diff --git a/lib/tir_inna_noc/imageboard.ex b/lib/tir_inna_noc/imageboard.ex index 516ed9d..3cfe26c 100644 --- a/lib/tir_inna_noc/imageboard.ex +++ b/lib/tir_inna_noc/imageboard.ex @@ -5,16 +5,24 @@ defmodule TirInnaNoc.Imageboard do #plug Tesla.Middleware.Headers, [{"authorization", "token xyz"}] plug Tesla.Middleware.JSON + def wget(uri) do + if GenServer.call(:ratelimiter, :activate) == :goahead do + get(uri) + else + Process.sleep(1000) + wget(uri) + end + end def boards() do - get("/boards.json") + wget("/boards.json") end def threads(board) do - get("/"<>board<>"/threads.json") + wget("/"<>board<>"/threads.json") end def thread(board, id) do - get("/"<>board<>"/thread/"<>to_string(id)<>".json") + wget("/"<>board<>"/thread/"<>to_string(id)<>".json") end def catalog(board) do - get("/"<>board<>"/catalog.json") + wget("/"<>board<>"/catalog.json") end end diff --git a/lib/tir_inna_noc/rate.ex b/lib/tir_inna_noc/rate.ex new file mode 100644 index 0000000..8a96398 --- /dev/null +++ b/lib/tir_inna_noc/rate.ex @@ -0,0 +1,23 @@ +defmodule TirInnaNoc.Rate do + use GenServer + + def start_link(last_activity) do + GenServer.start_link(__MODULE__, last_activity, name: :ratelimiter) + end + + @impl true + def init(last_activity) do + last_activity = :os.system_time(:millisecond) + {:ok, last_activity} + end + + @impl true + def handle_call(:activate, _, last_activity) do + if :os.system_time(:millisecond) - last_activity > 10_000 do + last_activity = :os.system_time(:millisecond) + {:reply, :goahead, last_activity} + else + {:reply, :defer, last_activity} + end + end +end diff --git a/lib/tir_inna_noc/sup.ex b/lib/tir_inna_noc/sup.ex index 104f97e..6db2cd5 100644 --- a/lib/tir_inna_noc/sup.ex +++ b/lib/tir_inna_noc/sup.ex @@ -9,6 +9,7 @@ defmodule TirInnaNoc.Sup do def init(:ok) do children = [ {TirInnaNoc.Db, [nil]}, + {TirInnaNoc.Rate, [nil]}, {DynamicSupervisor, strategy: :one_for_one, name: MerlinSupervisor}, {TirInnaNoc.Merlin, [nil]} ]