mirror of
https://github.com/mgerb/classic-wow-forums
synced 2026-01-10 17:12:48 +00:00
server - adjust rate limiter - reply/thread mod update
This commit is contained in:
@@ -4,14 +4,19 @@ defmodule MyApp.RateLimiter do
|
||||
def new_reply_key, do: 1
|
||||
def new_thread_key, do: 2
|
||||
|
||||
@spec limit(String.t, integer, integer) :: {:ok, String.t} | {:error, String.t}
|
||||
def limit(end_point, user_id, seconds) do
|
||||
@spec set_limit(String.t, integer, integer) :: {:ok, String.t}
|
||||
def set_limit(end_point, user_id, seconds) do
|
||||
key = "rl#{end_point}:#{user_id}"
|
||||
Cachex.set(:myapp, key, true, ttl: :timer.seconds(seconds))
|
||||
{:ok, "ok"}
|
||||
end
|
||||
|
||||
@spec check_limit(String.t, integer) :: {:ok, String.t} | {:error, String.t}
|
||||
def check_limit(end_point, user_id) do
|
||||
key = "rl#{end_point}:#{user_id}"
|
||||
case Cachex.get(:myapp, key) do
|
||||
{:missing, _} ->
|
||||
Cachex.set(:myapp, key, true, ttl: :timer.seconds(seconds))
|
||||
{:ok, "ok"}
|
||||
{:ok, _} -> {:error, "limit reached"}
|
||||
{:missing, _} -> {:ok, "ok"}
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user