Rate limit feature flag user list writes by size

What does this MR do and why?

Context

Nothing bounds the size of a feature flag user list or how often one user rewrites it, and the cost of each write grows with the size of the list. The figures and the agreed approach are in https://gitlab.com/gitlab-org/gitlab/-/work_items/638255#note_4182036252.

This MR

  • POST and PUT /projects/:id/feature_flags_user_lists[/:iid], which the user lists UI also calls, count the bytes of the submitted user_xids toward a new per-user rate limit, feature_flag_user_list_write_bytes, before the write. A request without user_xids, such as a rename, counts nothing.
  • The limit is a new rate_limits application setting, feature_flag_user_list_write_bytes_limit_per_user (default 16 MiB per minute, 0 disables), on Admin > Settings > Network and in the application settings API.
  • The new key starts in log mode: requests over the limit are logged until the rate_limit_feature_flag_user_list_write_bytes flag is enabled, and then get 429 Too Many Requests.
  • No size cap on the list itself. With the flag on, a single write larger than the limit gets a 429, so a list over the limit can no longer be saved.
  • Docs: a new rate limit page under doc/rate_limits/api/, a line on the user lists API, and the settings API. The Changelog entry comes with the flag-removal MR.

Feature Flag: rate_limit_feature_flag_user_list_write_bytes

References

Screenshots or screen recordings

The new Feature flag user lists API rate limits block on Admin > Settings > Network. Before (master) and After (this branch) are attached at the end of this description.

How to set up and validate locally

  1. In a Rails console, set a small limit:
    ApplicationSetting.current.update!(feature_flag_user_list_write_bytes_limit_per_user: 20)
  2. With a personal access token (api scope) for a project where you can manage feature flags, create a list twice:
    curl --include --request POST --header "PRIVATE-TOKEN: <your_access_token>" \
      --data "name=list-a&user_xids=user1,user2,user3" "http://gdk.test:3000/api/v4/projects/<id>/feature_flags_user_lists"
    curl --include --request POST --header "PRIVATE-TOKEN: <your_access_token>" \
      --data "name=list-b&user_xids=user1,user2,user3" "http://gdk.test:3000/api/v4/projects/<id>/feature_flags_user_lists"
    Both return 201, and log/api_json.log shows rate_limit_state ending in _by_user_log:log for the second request.
  3. Run Feature.enable(:rate_limit_feature_flag_user_list_write_bytes), wait a minute, and repeat the two requests with new names. The second returns 429 with RateLimit-Name: feature_flag_user_list_write_bytes.
  4. A PUT that sends only name still returns 200.
  5. Clean up: Feature.remove(:rate_limit_feature_flag_user_list_write_bytes) and reset the setting to 16777216.

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

before after

Edited by Leaminn Ma

Merge request reports

Loading
Loading