1
0
mirror of https://github.com/mgerb/classic-wow-forums synced 2026-01-11 01:22:49 +00:00

server/client - rate limiting for threads/replies

This commit is contained in:
2018-01-22 22:03:22 -06:00
parent adbe3c6857
commit 66dd5d5b9a
4 changed files with 44 additions and 10 deletions

18
lib/myapp/rate_limiter.ex Normal file
View File

@@ -0,0 +1,18 @@
defmodule MyApp.RateLimiter do
# map keys to integers to save memory
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
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"}
end
end
end