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
participantsorsubscribedmore than 20 times in total counts once against the newgraphql_participant_fieldsrate 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 thegraphql_participant_fields_rate_limitflag is disabled, requests over the limit are only recorded inrate_limit_state, and nothing is blocked. - New beta application setting
graphql_participant_fields_request_limit, default100requests a minute. Set to0to 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 withkey:andthreshold:options. A smallRequestCounterkeeps 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 forparticipantsandsubscribedonIssue,MergeRequest, andEpic,WorkItemWidgetParticipants.participants, andWorkItemWidgetNotifications.subscribed.WorkItemFeaturesandBoardEpicreuse these types.
The list resolvers no longer preload notes when these fields are selected:
Issuable.participant_includespreloaded 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_participantsapplies.limitto 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_associationsbatches 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
- https://gitlab.com/gitlab-org/gitlab/-/work_items/635894
- Rollout: #641028
- Precedent for a staged key keyed by user or IP: !268216 (merged)
Screenshots or screen recordings
Not applicable, no UI change.
How to set up and validate locally
- In the Rails console, lower the limit:
ApplicationSetting.current.update!(graphql_participant_fields_request_limit: 1) - In GraphiQL (
http://127.0.0.1:3000/-/graphql-explorer), run a query that lists more than 20 issues withparticipantstwice, for example:query { project(fullPath: "<project path>") { issues(first: 25) { nodes { id participants { nodes { id } } } } } } - With the flag disabled, both runs succeed, and the second request's line in
log/development_json.loghasrate_limit_statewithapplimiter_graphql_participant_fields:limit_graphql_participant_field_requests_by_user_or_ip_log:log. - Enable the flag with
Feature.enable(:graphql_participant_fields_rate_limit), then run the query twice more. The second run returns429 Too Many Requests. - 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.