mirror of
https://github.com/mgerb/classic-wow-forums
synced 2026-01-11 17:42:48 +00:00
server/client - rate limiting for threads/replies
This commit is contained in:
18
lib/myapp/rate_limiter.ex
Normal file
18
lib/myapp/rate_limiter.ex
Normal 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
|
||||
Reference in New Issue
Block a user