Skip to content

Better support for throttling/rate limiting in std.net.http.server.Server #998

Description

@yorickpeterse

Description

An HTTP server is created using std.net.http.server.Server.new in combination with a closure that returns a uni type that implements std.net.http.server.Handle.

Currently this closure isn't passed any arguments, making it difficult to apply some form of rate limiting before accepting the first request.

For example, when throttling by IP some form of shared state is required. Using a process is possible but because of the above you have to wait for the first request to come in before you can determine the IP of the request. This means a client could still try to exhaust the server's resources by establishing many connections but never sending a request.

To prevent this from happening we need to:

  1. Pass the IP address to the handler, so it can be used before establishing the connection state
  2. A way to signal back to the Server to abort setting up the connection, ideally without being forced to always return a Option[uni H: Handle]

Using this approach you could for example stick the IP into a Map along with the connection count, then reject the connection if the number is too large.

There are however some additional challenges with this:

  1. Whatever dataset you store the IP in should have an upper bound, so you don't end up with 200 000 entries consuming more memory than the connections would otherwise consume
  2. It will likely make the API more clunky, which I want to avoid
  3. It doesn't account for residential proxies where some scraper uses 200 different IPs each establishing one connection, instead of one IP establishing 200 connections

Related work

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdChanges related to the standard library

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions