Rate limit GraphQL requests that resolve many participants

What does this MR do and why?

The GraphQL participants and subscribed fields on issues, merge requests, epics, and work items compute the item's participants, which reads and parses every comment on the item. A request that selects them for many items does that work once per item, so its cost grows with the page size and the number of comments. GraphQL complexity doesn't bound it: both fields have a fixed complexity that isn't multiplied by the page size.

Instead of a hard cap on the fields, which would break integrations that page through 100 items, this MR rate limits the requests that resolve these fields many times:

  • A request that resolves participants or subscribed more than 20 times in total counts once against the new graphql_participant_fields rate limit, per user, or per IP address for unauthenticated requests. The fields share one count, and so do the queries of a multiplexed request. Requests that resolve them 20 times or fewer don't count.
  • When the caller is over the limit, the request fails with 429 Too Many Requests, following the GraphQL pattern in the rate limit guide (RateLimitedService::RateLimitedError).
  • The key is a staged_rule, so it starts in log mode: while the graphql_participant_fields_rate_limit flag is disabled, requests over the limit are only recorded in rate_limit_state, and nothing is blocked.
  • New beta application setting graphql_participant_fields_request_limit, default 100 requests a minute. Set to 0 to disable the limit.
  • Subscription updates are not counted, because each update resolves a single object.
  • The throttle bypass header is honored, like in ApplicationRateLimiter.throttled_request?.
  • Requests over the limit count as errors in the GraphQL query error rate, like other GraphQL rate limits today.

Implementation:

  • Gitlab::Graphql::Limit::FieldCallRateLimit: a reusable field extension with key: and threshold: options. A small RequestCounter keeps the count for each key in the request context and runs the rate limit check once per request. It's documented in the rate limit guide and the GraphQL style guide.
  • Gitlab::Graphql::Limit::ParticipantFieldsRateLimit: the shared configuration for participants and subscribed on Issue, MergeRequest, and Epic, WorkItemWidgetParticipants.participants, and WorkItemWidgetNotifications.subscribed. WorkItemFeatures and BoardEpic reuse these types.

The list resolvers no longer preload notes when these fields are selected:

  • Issuable.participant_includes preloaded every note of every item on the page, with no limit, before any field resolved, so the rate limit couldn't avoid that work.
  • The preload was never used: Issuable#notes_for_participants applies .limit to the association, which always runs a new query for each item.
  • Notes now load only for the items whose participants are resolved, so a rejected request loads notes only for the items resolved before the limit check.
  • Because the association is no longer preloaded, notes_with_associations batches the notes' authors and award emoji again, instead of loading the award emoji one note at a time.
  • Each item still runs its own capped notes query, as it already did before this MR.

GitLab's own pages stay within the threshold: boards load 10 epics per list, and the merge request and work item pages load one item at a time. The compliance violations report selects merge request participants with a page size of 20, exactly at the threshold, so raising that page size would make its requests count.

There is no GraphQL schema change. Once the flag is enabled, API clients that resolve these fields more than 20 times in one request can get 429 Too Many Requests when they make many such requests. Mutations count like any other operation, so a mutation payload can't be used to avoid the limit. In a multiplexed request that contains a mutation and a query over the limit, the mutation runs before the request fails with 429, the same as with other GraphQL rate limits today.

References

Screenshots or screen recordings

Not applicable, no UI change.

How to set up and validate locally

  1. In the Rails console, lower the limit:
    ApplicationSetting.current.update!(graphql_participant_fields_request_limit: 1)
  2. In GraphiQL (http://127.0.0.1:3000/-/graphql-explorer), run a query that lists more than 20 issues with participants twice, for example:
    query { project(fullPath: "<project path>") { issues(first: 25) { nodes { id participants { nodes { id } } } } } }
  3. With the flag disabled, both runs succeed, and the second request's line in log/development_json.log has rate_limit_state with applimiter_graphql_participant_fields:limit_graphql_participant_field_requests_by_user_or_ip_log:log.
  4. Enable the flag with Feature.enable(:graphql_participant_fields_rate_limit), then run the query twice more. The second run returns 429 Too Many Requests.
  5. Restore the limit with ApplicationSetting.current.update!(graphql_participant_fields_request_limit: 100), and disable the flag.

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.

Edited by Mario Celi

Merge request reports

Loading
Loading