Skip to content

Commit

Permalink
Add rate limiting configuration (#568)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljguarino authored Dec 27, 2023
1 parent 6af4a71 commit caa985b
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 2 deletions.
7 changes: 6 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ config :console,
cache_adapter: Console.Cache,
local_cache: Console.LocalCache,
version: Mix.Project.config[:version],
kas_dns: "https://kas.example.com"
kas_dns: "https://kas.example.com",
qps: 40

config :logger, :console,
format: "$time $metadata[$level] $message\n",
Expand Down Expand Up @@ -93,4 +94,8 @@ config :console, Console.PartitionedCache,

config :console, :login_link, []

config :hammer,
backend: {Hammer.Backend.ETS, [expiry_ms: 60_000 * 60 * 4,
cleanup_interval_ms: 60_000 * 10]}

import_config "#{Mix.env()}.exs"
2 changes: 2 additions & 0 deletions lib/console.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule Console do
@type error :: {:error, term}

def rate_limit(), do: {"global", :timer.seconds(1), Console.conf(:qps)}

def provider(), do: Console.conf(:provider)

def byok?() do
Expand Down
5 changes: 5 additions & 0 deletions lib/console_web/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,10 @@ defmodule ConsoleWeb.Endpoint do
key: "_watchman_key",
signing_salt: "cyhBqo3d"

plug Hammer.Plug, [
rate_limit: &Console.rate_limit/0,
by: :ip
]

plug ConsoleWeb.Router
end
4 changes: 3 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ defmodule Console.MixProject do
{:phoenix_client, "~> 0.11", git: "https://github.com/michaeljguarino/phoenix_client.git", branch: "mguarino/channel-listen"},
{:botanist, "~> 0.1.0", git: "https://github.com/michaeljguarino/botanist.git", branch: "ecto3"},
{:elixpath, "~> 0.1.1", git: "https://github.com/mtannaan/elixpath.git"},
{:mimic, "~> 1.1", only: :test}
{:mimic, "~> 1.1", only: :test},
{:hammer, "~> 6.0"},
{:hammer_plug, "~> 3.0", git: "https://github.com/pluralsh/hammer-plug.git", branch: "runtime-config"}
]
end

Expand Down
2 changes: 2 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
"gettext": {:hex, :gettext, "0.18.2", "7df3ea191bb56c0309c00a783334b288d08a879f53a7014341284635850a6e55", [:mix], [], "hexpm", "f9f537b13d4fdd30f3039d33cb80144c3aa1f8d9698e47d7bcbcc8df93b1f5c5"},
"guardian": {:hex, :guardian, "1.2.1", "bdc8dd3dbf0fb7216cb6f91c11831faa1a64d39cdaed9a611e37f2413e584983", [:mix], [{:jose, "~> 1.8", [hex: :jose, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.3", [hex: :phoenix, repo: "hexpm", optional: true]}, {:plug, "~> 1.3.3 or ~> 1.4", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "723fc404edfb7bd5cba4cd83329b352037f102aa97468f44e58ac7f47c136a98"},
"hackney": {:hex, :hackney, "1.18.1", "f48bf88f521f2a229fc7bae88cf4f85adc9cd9bcf23b5dc8eb6a1788c662c4f6", [:rebar3], [{:certifi, "~>2.9.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~>6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~>1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.3.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~>1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "a4ecdaff44297e9b5894ae499e9a070ea1888c84afdd1fd9b7b2bc384950128e"},
"hammer": {:hex, :hammer, "6.1.0", "f263e3c3e9946bd410ea0336b2abe0cb6260af4afb3a221e1027540706e76c55", [:make, :mix], [{:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: false]}], "hexpm", "b47e415a562a6d072392deabcd58090d8a41182cf9044cdd6b0d0faaaf68ba57"},
"hammer_plug": {:git, "https://github.com/pluralsh/hammer-plug.git", "4887091221a7addff947a9a437d2816f3ecdfd22", [branch: "runtime-config"]},
"hash_ring": {:hex, :hash_ring, "0.4.2", "65d7e84ee911c2f07e53193711013ec9b9761c5eb180720e31224b776c98f502", [:rebar3], [], "hexpm", "c327d99b1e2af41b50e5c4803a9bee7932db2c0f39ad7854a9fdad7e42444066"},
"horde": {:hex, :horde, "0.8.1", "c35a018d7dc2438e94d1204ef86978d01a4fb16fbd9877aafc4a82e735eeeee3", [:mix], [{:delta_crdt, "~> 0.5.10", [hex: :delta_crdt, repo: "hexpm", optional: false]}, {:libring, "~> 1.4", [hex: :libring, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:telemetry_poller, "~> 0.4.0", [hex: :telemetry_poller, repo: "hexpm", optional: false]}], "hexpm", "2b3679fbb7ae6152fa8a25184da8ab7357b2ff044f9163a33684a5b0d7606758"},
"http_stream": {:hex, :http_stream, "1.0.0", "d40cc69eefb380508c01e004571159c26779902564558bffe1517a57764e019e", [:mix], [{:castore, "~> 0.1.7", [hex: :castore, repo: "hexpm", optional: true]}, {:httpoison, "~> 1.7.0", [hex: :httpoison, repo: "hexpm", optional: true]}, {:mint, "~> 1.1.0", [hex: :mint, repo: "hexpm", optional: true]}], "hexpm", "efa64a8800b7365a6e77b0647b289b9fa41fbbbb44a117c8ddc9b8c6fc69e126"},
Expand Down
4 changes: 4 additions & 0 deletions rel/config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,8 @@ if get_env("GRAFANA_TENANT") do
config :console, :grafana_tenant, get_env("GRAFANA_TENANT")
end

if get_env("CONSOLE_QPS") do
config :console, :qps, String.to_integer(get_env("CONSOLE_QPS"))
end

config :elixir, :ansi_enabled, true

0 comments on commit caa985b

Please sign in to comment.