Skip to content

Rate limits: limit public routes by visitor and by network

Describes Web Analytics 0.10.0.

Rate limits stop one visitor, or one network, from sending more requests to a public route than you allow. Each request a rule covers is counted twice: for its visitor and for its network, and both must have room. A client that changes its browser string with every request is a new visitor each time, so the network count is what holds it.

Where to find it

Web Analytics → Rate limits (/admin/web-analytics/rate-limits). You need the Owner role or the Web Analytics permission. The switch and the blocked networks are in Settings, under Rate limits.

The Rate limits page of Web Analytics: rate limits are on, the Rules table and Latest refusals, both empty The Rate limits page of Web Analytics: rate limits are on, the Rules table and Latest refusals, both empty

The Rate limits page, before any rule or refusal.

Turning limits on

Nothing is limited until a developer adds the rate-limit plug with rules to the site’s routes (see below). The rules live in the site’s code, next to the routes they protect.

In Settings, Rate limits then chooses what the rules do:

  • On (the default): requests over a limit are refused.
  • Log only: requests over a limit are recorded on the Rate limits page and let through. Use it to see what a limit would do before it does it.
  • Off: nothing is counted or refused, blocked networks included.

Blocked networks takes addresses or networks to refuse with a 403, one per line, nothing wider than /8 (IPv4) or /16 (IPv6). It applies on every path the plug covers while rate limits aren’t off, log only included. Only what you enter is blocked; nothing is added by itself.

Who is never limited

  • The site’s internal networks: those listed under Internal networks in Settings, and any set in the app’s config.
  • Staff, by the same staff roles as the analytics, when the plug runs after the user is loaded.
  • Local development: a request from the same machine to localhost.
  • A request carrying the bypass token set in the app’s config.

The Rate limits page

The bar at the top says whether limits are on, log only or off, with Settings and Refresh. Everything on the page is what this server has seen since it started; nothing about a refusal is stored.

Rules lists each rule with its Rule name, Paths, Methods, the limits Per visitor and Per network, the Window, and how many requests were Allowed, Refused and Logged. A rule with no paths shows charged from code: it is only counted where the site’s code charges it. A rule appears once its plug has seen a request; before that the card says No rate limits are installed.

Latest refusals lists the most recent requests over a limit or from a blocked network, newest first: Time, Rule, Limit hit, What happened, Network, Visitor and Request. The network is shown whole only when Settings keep whole addresses, otherwise as its /24 or /48. Paths never include a query string. Until anything is refused, it says Nothing has been refused.

What a refused visitor gets

A 429 answer with a retry-after header saying how many seconds to wait, as JSON for API requests and a small HTML page otherwise. A developer can render the site’s own page instead.

What it doesn’t do

  • Counters are kept per server, in memory. With several servers, each counts on its own, and a restart starts the counts again.
  • It doesn’t block anyone by itself: blocked networks are only what you enter.
  • If the counters aren’t running, the page warns that nothing is limited and every request is let through.

For developers

Add the plug to a pipeline, after the plugs that load the user if staff are to be left alone:

plug PhoenixKitWebAnalytics.RateLimit.Plug,
  rules: [
    [name: :search, paths: ["/search"], methods: ["GET"], limit: 60, window_ms: 60_000],
    [name: :all, paths: ["/catalog*"], limit: 300, network_limit: 1_200]
  ]

limit is per visitor per window (60 seconds by default) and network_limit per network, four times limit by default. For work that is expensive only sometimes, call PhoenixKitWebAnalytics.RateLimit.charge(conn, :rule_name) and answer {:error, denial} with RateLimit.deny(conn, denial). The bypass token is config :phoenix_kit_web_analytics, :rate_limit, bypass_token: …, sent in the x-rate-limit-bypass header. PhoenixKitWebAnalytics.visitor/1 gives your own code the visitor the module counts.