Skip to content

Commit ec863fa

Browse files
Add rate limiter to helm agent fetches
Pretty sure these can get overwhelmed at large scale, and a simple rate limit should improve the load shedding here.
1 parent 81db3cf commit ec863fa

2 files changed

Lines changed: 11 additions & 5 deletions

File tree

lib/console/deployments/git/agent.ex

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ defmodule Console.Deployments.Git.Agent do
1818

1919
@poll :timer.seconds(120)
2020
@timeout :timer.seconds(10)
21-
@limit 50
21+
@limit 100
2222
@limit_interval :timer.seconds(1)
2323

2424
defmodule State, do: defstruct [:git, :cache, :last_pull, :url]
@@ -269,12 +269,15 @@ defmodule Console.Deployments.Git.Agent do
269269

270270
defp touch(pid, line), do: send(pid, {:touch, line})
271271

272-
defp rate_limited(key, fun) when is_function(fun, 0) do
272+
@doc false
273+
def rate_limited(key, fun) when is_function(fun, 0) do
273274
:erlang.term_to_binary(key)
274275
|> Hammer.check_rate(@limit_interval, @limit)
275276
|> case do
276277
{:allow, _} -> fun.()
277-
{:deny, _} -> {:error, :rate_limited}
278+
{:deny, _} ->
279+
Logger.warn "rate limiting git/helm agent fetch"
280+
{:error, :rate_limited}
278281
end
279282
end
280283

lib/console/deployments/helm/agent.ex

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
defmodule Console.Deployments.Helm.Agent do
22
use GenServer, restart: :temporary
3+
import Console.Deployments.Git.Agent, only: [rate_limited: 2]
34
alias Console.Repo
45
alias Console.Deployments.Git
56
alias Console.Deployments.Helm.{AgentCache, Discovery, Supervisor}
@@ -21,7 +22,8 @@ defmodule Console.Deployments.Helm.Agent do
2122
_ <- touch(pid, line) do
2223
{:ok, opener(pid, f), d, i}
2324
else
24-
_ -> GenServer.call(pid, {:fetch, chart, vsn}, @timeout)
25+
_ ->
26+
rate_limited({:helm, pid}, fn -> GenServer.call(pid, {:fetch, chart, vsn}, @timeout) end)
2527
end
2628
end
2729

@@ -32,7 +34,8 @@ defmodule Console.Deployments.Helm.Agent do
3234
_ <- touch(pid, line) do
3335
{:ok, d}
3436
else
35-
_ -> GenServer.call(pid, {:digest, chart, vsn}, @timeout)
37+
_ ->
38+
rate_limited({:helm, pid}, fn -> GenServer.call(pid, {:digest, chart, vsn}, @timeout) end)
3639
end
3740
end
3841

0 commit comments

Comments
 (0)