mirror of
https://github.com/benbusby/farside.git
synced 2025-04-19 06:45:55 -04: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:
parent
4f60a39d7f
commit
7f26ab3bbf
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user