summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Busby <contact@benbusby.com>2024-01-08 11:49:18 -0700
committerBen Busby <contact@benbusby.com>2024-01-08 11:49:18 -0700
commit7f26ab3bbf3743e5f6924b51e08986b58641ba49 (patch)
treec2c904b8f11044de568a0379f8467f8c101ccabe
parent4f60a39d7f6aded5a7ccd5f003f77ed634415a90 (diff)
downloadfarside-7f26ab3bbf3743e5f6924b51e08986b58641ba49.tar.gz
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
-rw-r--r--lib/farside/router.ex28
1 files changed, 13 insertions, 15 deletions
diff --git a/lib/farside/router.ex b/lib/farside/router.ex
index ce0eb0a..00d98e2 100644
--- a/lib/farside/router.ex
+++ b/lib/farside/router.ex
@@ -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