Skip to content

Commit b729975

Browse files
committed
Add version tracking to manage cache expiration.
1 parent 23fafe0 commit b729975

4 files changed

Lines changed: 35 additions & 3 deletions

File tree

src/Autofac/Core/Registration/ComponentRegistry.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace Autofac.Core.Registration;
1616
/// is normally used through a <see cref="ContainerBuilder"/>, and not
1717
/// directly by application code.
1818
/// </remarks>
19-
internal class ComponentRegistry : Disposable, IComponentRegistry
19+
internal class ComponentRegistry : Disposable, IComponentRegistry, IRegistrationDiagnostics
2020
{
2121
private readonly IRegisteredServicesTracker _registeredServicesTracker;
2222

@@ -62,6 +62,9 @@ internal ComponentRegistry(IRegisteredServicesTracker registeredServicesTracker,
6262
/// registrations for a new customized scope.</remarks>
6363
public bool HasLocalComponents => true;
6464

65+
/// <inheritdoc/>
66+
int IRegistrationDiagnostics.RegistrationsVersion => _registeredServicesTracker.RegistrationsVersion;
67+
6568
/// <summary>
6669
/// Attempts to find a default registration for the specified service.
6770
/// </summary>

src/Autofac/Core/Registration/DefaultRegisteredServicesTracker.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Reflection;
66
using System.Runtime.CompilerServices;
77
using Autofac.Core.Resolving.Pipeline;
8+
using Autofac.Diagnostics;
89
using Autofac.Util;
910
using Autofac.Util.Cache;
1011

@@ -50,6 +51,7 @@ internal class DefaultRegisteredServicesTracker : Disposable, IRegisteredService
5051

5152
private Dictionary<Service, ServiceRegistrationInfo>? _ephemeralServiceInfo;
5253
private bool _trackerPopulationComplete;
54+
private int _registrationsVersion;
5355

5456
/// <summary>
5557
/// Initializes a new instance of the <see cref="DefaultRegisteredServicesTracker"/> class.
@@ -75,6 +77,9 @@ public DefaultRegisteredServicesTracker()
7577
/// </summary>
7678
public event EventHandler<IRegistrationSource>? RegistrationSourceAdded;
7779

80+
/// <inheritdoc/>
81+
public int RegistrationsVersion => Volatile.Read(ref _registrationsVersion);
82+
7883
/// <inheritdoc />
7984
public IEnumerable<IComponentRegistration> Registrations
8085
{
@@ -138,6 +143,8 @@ public virtual void AddRegistration(IComponentRegistration registration, bool pr
138143
registration.BuildResolvePipeline(this);
139144
}
140145
}
146+
147+
Interlocked.Increment(ref _registrationsVersion);
141148
}
142149

143150
/// <inheritdoc />
@@ -315,6 +322,7 @@ private ServiceRegistrationInfo GetInitializedServiceInfo(Service service)
315322
}
316323

317324
var info = GetServiceInfo(service);
325+
var instrumentationService = AutofacMetrics.Enabled ? service.ToString() : null;
318326
if (info.IsInitialized)
319327
{
320328
return info;
@@ -339,7 +347,13 @@ private ServiceRegistrationInfo GetInitializedServiceInfo(Service service)
339347
var shouldProcessCompletion = false;
340348
try
341349
{
342-
Monitor.Enter(info, ref lockTaken);
350+
Monitor.TryEnter(info, ref lockTaken);
351+
if (!lockTaken)
352+
{
353+
var wait = ValueStopwatch.StartNew();
354+
Monitor.Enter(info, ref lockTaken);
355+
AutofacLockMetrics.RecordContention("Service", instrumentationService, wait.ElapsedTicks);
356+
}
343357

344358
if (info.IsInitialized)
345359
{

src/Autofac/Core/Registration/IRegisteredServicesTracker.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Autofac.Core.Registration;
88
/// <summary>
99
/// Keeps track of the status of registered services.
1010
/// </summary>
11-
internal interface IRegisteredServicesTracker : IDisposable, IAsyncDisposable, IComponentRegistryServices
11+
internal interface IRegisteredServicesTracker : IDisposable, IAsyncDisposable, IComponentRegistryServices, IRegistrationDiagnostics
1212
{
1313
/// <summary>
1414
/// Fired whenever a component is registered - either explicitly or via an <see cref="IRegistrationSource"/>.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) Autofac Project. All rights reserved.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
3+
4+
namespace Autofac.Core.Registration;
5+
6+
/// <summary>
7+
/// Provides additional diagnostic information about the component registrations without expanding the public API surface.
8+
/// </summary>
9+
internal interface IRegistrationDiagnostics
10+
{
11+
/// <summary>
12+
/// Gets the monotonically increasing version of the registrations sequence.
13+
/// </summary>
14+
int RegistrationsVersion { get; }
15+
}

0 commit comments

Comments
 (0)