Rate limit deleting large milestones
What does this MR do and why?
Deleting a milestone had no rate limit. Milestones::DestroyService runs an Issues::UpdateService for every issue and a MergeRequests::UpdateService for every merge request on the milestone, inside one transaction, so the cost of a delete grows with the size of the milestone.
All four delete paths go through the service: the project and group milestone destroy actions and the project and group milestone DELETE API endpoints. This MR checks a new milestone_destroy rule there, before the transaction, keyed on the user. The limit only applies to milestones with more than 10 issues and merge requests, so deleting empty or small milestones is not limited. The existing RateLimitedError handlers in ApplicationController and API::API return the 429 responses, the same way as the CSV export and import limits.
The limit comes from the new milestone_destroy_limit application setting, 10 deletes per minute by default. Admins can change it through the application settings API, and 0 disables it. The setting lives in the rate_limits JSONB column, so no migration is needed.
The limit is behind the milestone_destroy_rate_limit feature flag, disabled by default.
Database
New read queries introduced by Milestones::DestroyService#rate_limited_size? (run once per milestone delete, only when the milestone_destroy_rate_limit flag is enabled). Both count at most 11 rows, so the cost does not grow with the size of the milestone.
1. Issues on the milestone (always runs)
SELECT COUNT(*) FROM (SELECT 1 AS one FROM "issues" WHERE "issues"."milestone_id" = <milestone_id> LIMIT 11) subquery_for_countPlan: https://console.postgres.ai/gitlab/projects/gitlab-production-main/sessions/79096/commands/187158
(Index Only Scan on bigint_idx_a4617e3cfb76f2301d74, 11 rows, 11.0 ms cold, buffers: 2 hit / 14 read)
2. Merge requests on the milestone (only when the milestone has 10 or fewer issues)
The LIMIT is 11 minus the issue count, so 11 is the worst case.
SELECT COUNT(*) FROM (SELECT 1 AS one FROM "merge_requests" WHERE "merge_requests"."milestone_id" = <milestone_id> LIMIT 11) subquery_for_countPlan: https://console.postgres.ai/gitlab/projects/gitlab-production-main/sessions/79096/commands/187159
(Index Only Scan on index_merge_requests_on_milestone_id, 11 rows, 5.9 ms cold, buffers: 3 hit / 7 read)
References
None.
Screenshots or screen recordings
Not applicable, no UI changes. Over the limit, the existing 429 response is shown.
How to set up and validate locally
-
Enable the feature flag in the Rails console:
Feature.enable(:milestone_destroy_rate_limit) -
Optional. Change the limit with
ApplicationSetting.current.update!(milestone_destroy_limit: 2). -
Create 11 project milestones (or one more than the limit you set), each with at least 11 issues.
-
Delete them within one minute, in the UI or with
DELETE /projects/:id/milestones/:milestone_id. -
The delete over the limit returns
This endpoint has been requested too many times. Try again later.and the milestone is kept. -
Deleting a milestone with 10 or fewer issues and merge requests is not limited.
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.