Add rate limit log and feature flag to billable user change preview
What does this MR do and why?
- Add a per-user rate limit to the
gitlabSubscriptionsPreviewBillableUserChangeGraphQL field. The limit comes from thepreview_billable_user_change_limitapplication setting, which defaults to0and disables the limit. - Rate limit is behind the
preview_billable_user_change_rate_limitfeature flag.- When disabled, requests over the limit are only logged.
- When enabled, requests over the limit return a
RateLimitedErrorwithextensions.error_type: rate_limited.
gitlabSubscriptionsPreviewBillableUserChangeis called in the following frontend components. A follow-up MR will update all frontend callers to show the error in the UI:
Follow-up
After this MR is merged and deployed, a production change request sets preview_billable_user_change_limit to 60 on GitLab.com. Until then, nothing is logged, because the limiter skips a limit of 0. The change request is tracked in the rollout issue.
The threshold is an initial value that will be adjusted as we receive log data before globally enabling the feature flag.
Expected rate_limit_state when the setting is above 0:
| Scenario | Feature Flag | Rule and result following applimiter_gitlab_subscriptions_preview_billable_user_change: |
|---|---|---|
| Under the limit | Off | limit_subscriptions_preview_billable_user_change_by_user_log:allow |
| Over the limit, request succeeds | Off | limit_subscriptions_preview_billable_user_change_by_user_log:log |
| Under the limit | On | limit_subscriptions_preview_billable_user_change_by_user:allow |
| Over the limit, request rejected | On | limit_subscriptions_preview_billable_user_change_by_user:block |
Stacked MRs
| Order | MR | What it does |
|---|---|---|
| 1 | This MR | backend - per-user rate limit on the field, behind preview_billable_user_change_rate_limit |
| 2 | !272001 | frontend - show the rate limit error in the three callers |
References
- Related to #637060
- Rollout issue #640528
GroupfieldsProjectfields
Screenshots or screen recordings
| Before | After |
|---|---|
![]() |
![]() |
How to set up and validate locally
-
In rails console, set the application setting limit to
2:ApplicationSetting.current.update!(preview_billable_user_change_limit: 2) -
As an owner of a top-level group, run this query in GraphiQL 3 times:
query { group(fullPath: "<group_path>") { gitlabSubscriptionsPreviewBillableUserChange(addUserIds: [<user_id>], role: DEVELOPER) { newBillableUserCount } } }All 3 return data. In
log/development_json.log, the third request'srate_limit_stateincludesapplimiter_gitlab_subscriptions_preview_billable_user_change:limit_subscriptions_preview_billable_user_change_by_user_log:log. -
In the Rails console, enable the flag for your user:
Feature.enable(:preview_billable_user_change_rate_limit, User.find_by_username('<username>')) -
Run the query 3 more times. The third returns
nullwith the errorThis endpoint has been requested too many times. Try again later.andextensions.error_typeset torate_limited.In
log/development_json.log, the third request'srate_limit_stateincludesapplimiter_gitlab_subscriptions_preview_billable_user_change:limit_subscriptions_preview_billable_user_change_by_user:block.
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.

