We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Resource Limits

Rate limiting controls how often a client can call your API. Resource limits control how much work a single request can demand. For example:

  • Request body size – limit the size of incoming JSON or file uploads
  • Processing time – set a maximum time for database queries or third-party API calls
  • Result size – cap the number of items returned in a list or the size of a generated report

I used to do a lot of work with the Facebook and Instagram APIs, and their limits were based on request processing time. One complex query can be 100x more expensive than ten simple ones!

Body and Upload Limits

HTTP servers should enforce a maximum request body size. Go's http.MaxBytesReader stops a handler from reading beyond an explicit limit:

request.Body = http.MaxBytesReader(responseWriter, request.Body, 32*1024)

File uploads need their own limits. A multipart request includes file bytes and encoding overhead, so limit both the request body and the bytes read from each uploaded file. Also reject extra files instead of quietly processing the first one.

Go's io.LimitReader is useful for enforcing the per-file boundary after parsing the multipart form.

Keep in mind that a tiny compressed file can still expand into gigabytes, and a small input to an evil regex can burn through your server's CPU. Limit the resource that can actually be exhausted.

Assignment

Bearly Secure has the limit settings and max-aware APIs, but several request paths don't enforce them yet.

Complete the app's per-request resource limits.

With Bearly Secure still running, run and submit the CLI tests from the project root.