1
0
Fork 0
mirror of https://github.com/benbusby/farside.git synced 2025-04-20 02:48:42 +00:00

Return 429 for users exceeding 1 req/sec

Farside has been getting used by some to rapidly scrape sites, which
puts increased load and effort on maintainers of instances. Rather than
funneling traffic towards the last selected instance, farside will now
just return a 429 error when this behavior occurs.

Closes #147
This commit is contained in:
Ben Busby 2024-01-08 11:49:18 -07:00
parent 4f60a39d7f
commit 7f26ab3bbf
No known key found for this signature in database
GPG key ID: B9B7231E01D924A1

View file

@ -56,23 +56,21 @@ defmodule Farside.Router do
Enum.join(glob, "/")
end
instance = cond do
cond do
conn.assigns[:throttle] != nil ->
Farside.get_service(service_name)
|> Farside.last_instance
|> Farside.amend_instance(service_name, path)
send_resp(conn, :too_many_requests, "Too many requests - max request rate is 1 per second")
true ->
Farside.get_service(service_name)
|> Farside.pick_instance
|> Farside.amend_instance(service_name, path)
end
instance = Farside.get_service(service_name)
|> Farside.pick_instance
|> Farside.amend_instance(service_name, path)
# Redirect to the available instance
conn
|> Plug.Conn.resp(:found, "")
|> Plug.Conn.put_resp_header(
"location",
"#{instance}/#{path}#{get_query_params(conn)}"
)
# Redirect to the available instance
conn
|> Plug.Conn.resp(:found, "")
|> Plug.Conn.put_resp_header(
"location",
"#{instance}/#{path}#{get_query_params(conn)}"
)
end
end
end