diff options
| author | Ben Busby <contact@benbusby.com> | 2024-01-08 11:49:18 -0700 |
|---|---|---|
| committer | Ben Busby <contact@benbusby.com> | 2024-01-08 11:49:18 -0700 |
| commit | 7f26ab3bbf3743e5f6924b51e08986b58641ba49 (patch) | |
| tree | c2c904b8f11044de568a0379f8467f8c101ccabe /lib | |
| parent | 4f60a39d7f6aded5a7ccd5f003f77ed634415a90 (diff) | |
| download | farside-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
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/farside/router.ex | 28 |
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 |
