reset request counts in rate limiter every day at midnight

This commit is contained in:
Owen
2024-12-17 12:15:47 +01:00
parent 70f067f747
commit 322c499725
10 changed files with 34 additions and 3 deletions
+1
View File
@@ -8,6 +8,7 @@ defmodule PlantIdDiscordBot.Application do
children = [
PlantIdDiscordBot.Consumer,
PlantIdDiscordBot.RateLimiter,
PlantIdDiscordBot.Scheduler,
{Plug.Cowboy, scheme: :http, plug: PlantIdDiscordBot.FileServer, options: [port: @port]}
]
+1 -1
View File
@@ -15,7 +15,7 @@ defmodule PlantIdDiscordBot.Cog.PlantNet do
"""
def id(interaction) do
case RateLimiter.check_limit(interaction.guild_id) do
{:limit_exceeded, value} ->
{:limit_exceeded, _} ->
@api.create_interaction_response(interaction, %{
type: 4,
data: %{
+10 -1
View File
@@ -47,6 +47,15 @@ defmodule PlantIdDiscordBot.RateLimiter do
Increase the counter for the number of requests for a guild by one.
"""
def increase_counter(guild_id) do
:ets.update_counter(__MODULE__, guild_id, 1)
case get(guild_id) do
{:ok, _} -> :ets.update_counter(__MODULE__, guild_id, 1)
{:error, _} -> put(guild_id, 1)
end
end
@doc """
Reset all guilds counters to 0.
"""
@spec reset_counters() :: :ok
def reset_counters(), do: :ets.delete_all_objects(__MODULE__)
end
+3
View File
@@ -0,0 +1,3 @@
defmodule PlantIdDiscordBot.Scheduler do
use Quantum, otp_app: :plantid_discord_bot
end