Add `.actor_from_id` to check feature flags with model id
What does this MR do and why?
Internally, flipper checks the model class name and model id in order to
determine if a feature is enabled for an actor. If we already have an
actor's ID, there is no need to make a database query for that actor for
the sole purpose of performing a feature flag check. Here we add the
actor_from_id
method to models, which returns a wrapper around the
class name and model ID that builds a flipper_id
. This lets us perform
one query for a feature flag check instead of two. The only downside of
this approach is that we might perform feature flag checks for actors
which no longer exist. I think this downside is negligible because:
- If an administrator explicitly enabled the flag for a given actor, then people must be using that object and it's very unlikely for it to be deleted.
- In the contexts where we have an ID and only want to perform a feature flag check, we are probably passing that ID downstream to another process (like a sidekiq worker) and that process will perform an existence check on the object.
MR acceptance checklist
Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.
Screenshots or screen recordings
Screenshots are required for UI changes, and strongly recommended for all other merge requests.
Before | After |
---|---|
How to set up and validate locally
Numbered steps to set up and validate the change are strongly suggested.
[2] pry(main)> project = Project.find_by_full_path('gitlab-org/gitlab-test')
=> #<Project id:1 gitlab-org/gitlab-test>>
[6] pry(main)> Feature.enable(:project_path_sort, project)
=> true
[7] pry(main)> Feature.enabled?(:project_path_sort, Project.actor_from_id(1))
=> true
[8] pry(main)> Feature.disable(:project_path_sort, project)
=> true
[9] pry(main)> Feature.enabled?(:project_path_sort, Project.actor_from_id(1))
=> false