-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Improve batch state checker performance and add RequireFeaturesSimpleBatchStateChecker #25276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
729b4de
Improve batch state checker performance by sharing a single DI scope
maliming b255437
Resolve global state checkers from evaluation scope for consistency
maliming 3c52892
Add early-exit guard for states with no non-batch checkers in batch path
maliming c432374
Add batch feature checking support with RequireFeaturesSimpleBatchSta…
maliming 6505dc0
Optimize batch state checker model lookup from O(N²) to O(N)
maliming File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
framework/src/Volo.Abp.Features/Volo/Abp/Features/RequireFeaturesSimpleBatchStateChecker.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Volo.Abp.SimpleStateChecking; | ||
|
|
||
| namespace Volo.Abp.Features; | ||
|
|
||
| public class RequireFeaturesSimpleBatchStateChecker<TState> : SimpleBatchStateCheckerBase<TState> | ||
| where TState : IHasSimpleStateCheckers<TState> | ||
| { | ||
| public static RequireFeaturesSimpleBatchStateChecker<TState> Current => _current.Value!; | ||
| private static readonly AsyncLocal<RequireFeaturesSimpleBatchStateChecker<TState>> _current = new(); | ||
|
|
||
| private readonly List<RequireFeaturesSimpleBatchStateCheckerModel<TState>> _models; | ||
|
|
||
| static RequireFeaturesSimpleBatchStateChecker() | ||
| { | ||
| _current.Value = new RequireFeaturesSimpleBatchStateChecker<TState>(); | ||
| } | ||
|
|
||
| public RequireFeaturesSimpleBatchStateChecker() | ||
| { | ||
| _models = new List<RequireFeaturesSimpleBatchStateCheckerModel<TState>>(); | ||
| } | ||
|
|
||
| public RequireFeaturesSimpleBatchStateChecker<TState> AddCheckModels( | ||
| params RequireFeaturesSimpleBatchStateCheckerModel<TState>[] models) | ||
| { | ||
| Check.NotNullOrEmpty(models, nameof(models)); | ||
|
|
||
| _models.AddRange(models); | ||
| return this; | ||
| } | ||
|
|
||
| public static IDisposable Use(RequireFeaturesSimpleBatchStateChecker<TState> checker) | ||
| { | ||
| var previousValue = Current; | ||
| _current.Value = checker; | ||
| return new DisposeAction(() => _current.Value = previousValue); | ||
| } | ||
|
|
||
| public override async Task<SimpleStateCheckerResult<TState>> IsEnabledAsync( | ||
| SimpleBatchStateCheckerContext<TState> context) | ||
| { | ||
| var featureChecker = context.ServiceProvider.GetRequiredService<IFeatureChecker>(); | ||
|
|
||
| var result = new SimpleStateCheckerResult<TState>(context.States); | ||
|
|
||
| var relevantModels = _models | ||
| .Where(x => context.States.Any(s => s.Equals(x.State))) | ||
| .ToList(); | ||
|
|
||
| var features = relevantModels.SelectMany(x => x.FeatureNames).Distinct().ToArray(); | ||
| var featureValues = await featureChecker.IsEnabledAsync(features); | ||
|
|
||
| foreach (var state in context.States) | ||
| { | ||
| var model = relevantModels.FirstOrDefault(x => x.State.Equals(state)); | ||
| if (model != null) | ||
|
maliming marked this conversation as resolved.
Outdated
|
||
| { | ||
| if (model.RequiresAll) | ||
| { | ||
| result[state] = model.FeatureNames.All(x => featureValues.TryGetValue(x, out var v) && v); | ||
| } | ||
| else | ||
| { | ||
| result[state] = model.FeatureNames.Any(x => featureValues.TryGetValue(x, out var v) && v); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
| } | ||
23 changes: 23 additions & 0 deletions
23
...rk/src/Volo.Abp.Features/Volo/Abp/Features/RequireFeaturesSimpleBatchStateCheckerModel.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| using Volo.Abp.SimpleStateChecking; | ||
|
|
||
| namespace Volo.Abp.Features; | ||
|
|
||
| public class RequireFeaturesSimpleBatchStateCheckerModel<TState> | ||
| where TState : IHasSimpleStateCheckers<TState> | ||
| { | ||
| public TState State { get; } | ||
|
|
||
| public string[] FeatureNames { get; } | ||
|
|
||
| public bool RequiresAll { get; } | ||
|
|
||
| public RequireFeaturesSimpleBatchStateCheckerModel(TState state, string[] featureNames, bool requiresAll = true) | ||
| { | ||
| Check.NotNull(state, nameof(state)); | ||
| Check.NotNullOrEmpty(featureNames, nameof(featureNames)); | ||
|
|
||
| State = state; | ||
| FeatureNames = featureNames; | ||
| RequiresAll = requiresAll; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.