{"id":15679806,"url":"https://github.com/nsweeting/throttle","last_synced_at":"2025-05-07T10:29:33.279Z","repository":{"id":62430406,"uuid":"91509100","full_name":"nsweeting/throttle","owner":"nsweeting","description":"A general purpose Elixir throttle utility.","archived":false,"fork":false,"pushed_at":"2020-03-04T19:36:00.000Z","size":17,"stargazers_count":11,"open_issues_count":3,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-10-30T13:57:36.950Z","etag":null,"topics":["api","elixir","pheonix","phoenix-framework","plug","throttle"],"latest_commit_sha":null,"homepage":null,"language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nsweeting.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-05-16T22:13:27.000Z","updated_at":"2023-09-01T10:45:53.000Z","dependencies_parsed_at":"2022-11-01T20:20:33.650Z","dependency_job_id":null,"html_url":"https://github.com/nsweeting/throttle","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nsweeting%2Fthrottle","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nsweeting%2Fthrottle/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nsweeting%2Fthrottle/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nsweeting%2Fthrottle/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nsweeting","download_url":"https://codeload.github.com/nsweeting/throttle/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223806487,"owners_count":17205982,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","elixir","pheonix","phoenix-framework","plug","throttle"],"created_at":"2024-10-03T16:36:48.904Z","updated_at":"2024-11-09T09:05:36.020Z","avatar_url":"https://github.com/nsweeting.png","language":"Elixir","readme":"# Throttle\n\nA general throttle utility. Mainly used to throttle inbound or outbound requests.\n\n## Installation\n\nAdd the following to your list of dependencies in mix.exs:\n\n```elixir\ndef deps do\n  [{:throttle, \"~\u003e 0.1.2\"}]\nend\n```\n\n## Setup\n\nAs of now, Throttle only provides a Redis-backed cache. To configure your Redis connection, please add the following to `config/config.exs`.\n\n```elixir\nconfig :redis_connection_pool, [\n  host: \"127.0.0.1\",\n  port: 6379,\n  password: \"\",\n  db: 0,\n  reconnect: :no_reconnect,\n  pool_name: :\"Redis.Pool\",\n  pool_size: 10,\n  pool_max_overflow: 1\n]\n```\n\nOr use with the full_url option.\n\n```elixir\nconfig :redis_connection_pool, [\n  full_url: \"redis://127.0.0.1:6379\",\n  reconnect: :no_reconnect,\n  pool_name: :\"Redis.Pool\",\n  pool_size: 10,\n  pool_max_overflow: 1\n]\n```\n\nYou can also add throttle contexts to your config. This will allow us to access them by atom-only in the future.\n\n```elixir\nconfig :throttle, [\n  contexts: [\n    # This would create a throttle context keyed under \"example1\" that allows 3 requests per second.\n    example1: {\"example1\", :rps, 3}\n    # This would create a throttle context keyed under \"example2\" that allows 30 requests per minute.\n    example2: {\"example2\", :rpm, 30}\n    # This would create a throttle context keyed under \"example3\" that allows 300 requests per hour.\n    example2: {\"example3\", :rph, 300}\n    # This would create a throttle context keyed under \"example4\" that allows 1 request every 3 seconds.\n    example2: {\"example3\", :interval, 3}\n    # This would create a throttle context keyed under \"example5\" using a leaky bucket that adds 1 token every second (rate), \n    # to a maximum of 40 (max), with each request costing 2 tokens (cost).\n    example2: {\"example3\", :bucket, [rate: 1, max: 40, cost: 2]}\n  ]\n]\n```\n\n## Usage\n\nThrottle provides a number of ways in which to control requests. They are:\n\n- Requests per second (rps)\n- Requests per minute (rpm)\n- Requests per hour (rph)\n- Seconds Between Request (interval)\n- Leaky Bucket (bucket)\n\nAssuming we use one of our above contexts, we can now start using our throttles:\n\n```elixir\ncase Throttle.allow?(:example2) do\n  {:ok, result} -\u003e IO.puts \"OK\"\n  {:error, result} -\u003e IO.puts \"Error\"\nend\n```\n\nUsing config contexts means that each request is made under the same key. In the example above, this would be \"example2\".\n\nAlternatively, we can simply pass a context directly into the `allow?` function.\n\n```elixir\nkey = \"1\" # This could be a user id\n\ncase Throttle.allow?({key, :rps, 3}) do\n  {:ok, result} -\u003e IO.puts \"OK\"\n  {:error, result} -\u003e IO.puts \"Error\"\nend\n```\n\nIn all cases, a context must be structured as follows:\n\n```elixir\n{key, type, value} = {\"key\", :rps, 3}\n```\n\n### Using With Plug\n\nThrottle provides a plug that can be used for API throttling. It can be used as follows:\n\n```elixir\ndefmodule MyRouter do\n  use Plug.Router\n\n  plug Throttle.Plug, {\"mysite\", :bucket, [rate: 1, max: 40, cost: 2]}\n  plug :match\n  plug :dispatch\n\n  match _ do\n    send_resp(conn, 200, \"hello\")\n  end\nend\n```\n\nThe above would end up creating a leaky bucket throttle. The key would be a join between \"mysite\" and the IP address of the incoming request.\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnsweeting%2Fthrottle","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnsweeting%2Fthrottle","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnsweeting%2Fthrottle/lists"}