@@ -18,7 +18,6 @@ defmodule Console.Deployments.Git.Agent do
1818
1919 @ poll :timer . seconds ( 120 )
2020 @ timeout :timer . seconds ( 10 )
21- @ limit 50
2221 @ limit_interval :timer . seconds ( 1 )
2322
2423 defmodule State , do: defstruct [ :git , :cache , :last_pull , :url ]
@@ -269,15 +268,40 @@ defmodule Console.Deployments.Git.Agent do
269268
270269 defp touch ( pid , line ) , do: send ( pid , { :touch , line } )
271270
272- defp rate_limited ( key , fun ) when is_function ( fun , 0 ) do
273- :erlang . term_to_binary ( key )
274- |> Hammer . check_rate ( @ limit_interval , @ limit )
275- |> case do
276- { :allow , _ } -> fun . ( )
277- { :deny , _ } -> { :error , :rate_limited }
271+ @ doc false
272+ def rate_limited ( { _ , pid } = key , fun ) when is_function ( fun , 0 ) and is_pid ( pid ) do
273+ hammer_key = :erlang . term_to_binary ( key )
274+ with { :allow , _ } <- Hammer . check_rate ( hammer_key , @ limit_interval , cache_agent_qps ( ) ) ,
275+ { :q , :ok } <- { :q , queue_limit ( key , pid ) } do
276+ fun . ( )
277+ else
278+ { :deny , _ } ->
279+ Logger . warning "rate limiting git/helm agent fetch"
280+ { :error , :rate_limited }
281+ { :q , _ } ->
282+ Logger . warning "rate limiting git/helm agent fetch due to message queue length"
283+ { :error , :rate_limited }
278284 end
279285 end
280286
287+ defp queue_limit ( key , pid ) when is_pid ( pid ) do
288+ lim = cache_agent_queue_limit ( )
289+ shed = cache_agent_queue_shed ( )
290+ case { :q , Process . info ( pid , :message_queue_len ) } do
291+ { :q , { _ , ^ lim } } ->
292+ case Hammer . check_rate ( :erlang . term_to_binary ( { :shed , key } ) , @ limit_interval , shed ) do
293+ { :allow , _ } -> :ok
294+ { :deny , _ } -> :error
295+ end
296+ { :q , { _ , len } } when len < lim -> :ok
297+ _ -> :error
298+ end
299+ end
300+
301+ defp cache_agent_queue_limit ( ) , do: Console . conf ( :cache_agent_queue_limit )
302+ defp cache_agent_queue_shed ( ) , do: Console . conf ( :cache_agent_queue_shed )
303+ defp cache_agent_qps ( ) , do: Console . conf ( :cache_agent_qps )
304+
281305 defp refresh ( % GitRepository { health: :pullable } = git , cache ) , do: Cache . refresh ( % { cache | git: git } )
282306 defp refresh ( _ , cache ) , do: cache
283307
0 commit comments