Skip to content
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

Cache ResettableServices for DbContext pooling #24138

Merged
merged 2 commits into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 17 additions & 25 deletions src/EFCore/DbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public class DbContext :
private IServiceScope _serviceScope;
private DbContextLease _lease = DbContextLease.InactiveLease;
private DbContextPoolConfigurationSnapshot _configurationSnapshot;
private List<IResettableService> _cachedResettableServices;
private bool _initializing;
private bool _disposed;

Expand Down Expand Up @@ -248,15 +249,13 @@ object IDbSetCache.GetOrAddSet(IDbSetSource source, Type type)
{
CheckDisposed();

if (_sets == null)
{
_sets = new Dictionary<(Type Type, string Name), object>();
}
_sets ??= new Dictionary<(Type Type, string Name), object>();

if (!_sets.TryGetValue((type, null), out var set))
{
set = source.Create(this, type);
_sets[(type, null)] = set;
_cachedResettableServices = null;
}

return set;
Expand All @@ -273,15 +272,13 @@ object IDbSetCache.GetOrAddSet(IDbSetSource source, string entityTypeName, Type
{
CheckDisposed();

if (_sets == null)
{
_sets = new Dictionary<(Type Type, string Name), object>();
}
_sets ??= new Dictionary<(Type Type, string Name), object>();

if (!_sets.TryGetValue((type, entityTypeName), out var set))
{
set = source.Create(this, entityTypeName, type);
_sets[(type, entityTypeName)] = set;
_cachedResettableServices = null;
}

return set;
Expand Down Expand Up @@ -766,7 +763,7 @@ void IDbContextPoolable.SnapshotConfiguration()
[EntityFrameworkInternal]
void IResettableService.ResetState()
{
foreach (var service in GetResettableServices())
foreach (var service in _cachedResettableServices ??= GetResettableServices())
{
service.ResetState();
}
Expand All @@ -785,7 +782,7 @@ void IResettableService.ResetState()
[EntityFrameworkInternal]
async Task IResettableService.ResetStateAsync(CancellationToken cancellationToken)
{
foreach (var service in GetResettableServices())
foreach (var service in _cachedResettableServices ??= GetResettableServices())
{
await service.ResetStateAsync(cancellationToken).ConfigureAwait(false);
}
Expand All @@ -795,30 +792,25 @@ async Task IResettableService.ResetStateAsync(CancellationToken cancellationToke
_disposed = true;
}

private IEnumerable<IResettableService> GetResettableServices()
private List<IResettableService> GetResettableServices()
{
var resettableServices
var resettableServices = new List<IResettableService>();

var services
= _contextServices?.InternalServiceProvider?
.GetService<IEnumerable<IResettableService>>()?.ToList();
.GetService<IEnumerable<IResettableService>>();

if (resettableServices != null)
if (services != null)
{
foreach (var service in resettableServices)
{
yield return service;
}
resettableServices.AddRange(services);
}

if (_sets != null)
{
foreach (var set in _sets.Values)
{
if (set is IResettableService resettable)
{
yield return resettable;
}
}
resettableServices.AddRange((_sets.Values.OfType<IResettableService>()));
}

return resettableServices;
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore/Internal/DbContextLease.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void Release()
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public ValueTask ReleaseAsync()
=> Release(out var pool, out var context) ? pool.ReturnAsync(context) : new ValueTask();
=> Release(out var pool, out var context) ? pool.ReturnAsync(context) : default;

private bool Release(out IDbContextPool pool, out IDbContextPoolable context)
{
Expand Down