Fix interceptor memory leaks (#66)
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using System.Reflection;
|
||||
using Castle.DynamicProxy;
|
||||
using Common.Attributes;
|
||||
using Common.Configuration.General;
|
||||
using Microsoft.Extensions.Logging;
|
||||
@@ -7,41 +6,70 @@ using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Infrastructure.Interceptors;
|
||||
|
||||
public class DryRunAsyncInterceptor : AsyncInterceptorBase
|
||||
public class DryRunInterceptor : IDryRunInterceptor
|
||||
{
|
||||
private readonly ILogger<DryRunAsyncInterceptor> _logger;
|
||||
private readonly ILogger<DryRunInterceptor> _logger;
|
||||
private readonly DryRunConfig _config;
|
||||
|
||||
public DryRunAsyncInterceptor(ILogger<DryRunAsyncInterceptor> logger, IOptions<DryRunConfig> config)
|
||||
public DryRunInterceptor(ILogger<DryRunInterceptor> logger, IOptions<DryRunConfig> config)
|
||||
{
|
||||
_logger = logger;
|
||||
_config = config.Value;
|
||||
}
|
||||
|
||||
protected override async Task InterceptAsync(IInvocation invocation, IInvocationProceedInfo proceedInfo, Func<IInvocation, IInvocationProceedInfo, Task> proceed)
|
||||
public void Intercept(Action action)
|
||||
{
|
||||
MethodInfo? method = invocation.MethodInvocationTarget ?? invocation.Method;
|
||||
if (IsDryRun(method))
|
||||
MethodInfo methodInfo = action.Method;
|
||||
|
||||
if (IsDryRun(methodInfo))
|
||||
{
|
||||
_logger.LogInformation("[DRY RUN] skipping method: {name}", method.Name);
|
||||
_logger.LogInformation("[DRY RUN] skipping method: {name}", methodInfo.Name);
|
||||
return;
|
||||
}
|
||||
|
||||
await proceed(invocation, proceedInfo);
|
||||
action();
|
||||
}
|
||||
|
||||
protected override async Task<TResult> InterceptAsync<TResult>(IInvocation invocation, IInvocationProceedInfo proceedInfo, Func<IInvocation, IInvocationProceedInfo, Task<TResult>> proceed)
|
||||
|
||||
public Task InterceptAsync(Delegate action, params object[] parameters)
|
||||
{
|
||||
MethodInfo? method = invocation.MethodInvocationTarget ?? invocation.Method;
|
||||
if (IsDryRun(method))
|
||||
MethodInfo methodInfo = action.Method;
|
||||
|
||||
if (IsDryRun(methodInfo))
|
||||
{
|
||||
_logger.LogInformation("[DRY RUN] skipping method: {name}", method.Name);
|
||||
return default!;
|
||||
_logger.LogInformation("[DRY RUN] skipping method: {name}", methodInfo.Name);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
return await proceed(invocation, proceedInfo);
|
||||
}
|
||||
object? result = action.DynamicInvoke(parameters);
|
||||
|
||||
if (result is Task task)
|
||||
{
|
||||
return task;
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task<T?> InterceptAsync<T>(Delegate action, params object[] parameters)
|
||||
{
|
||||
MethodInfo methodInfo = action.Method;
|
||||
|
||||
if (IsDryRun(methodInfo))
|
||||
{
|
||||
_logger.LogInformation("[DRY RUN] skipping method: {name}", methodInfo.Name);
|
||||
return Task.FromResult(default(T));
|
||||
}
|
||||
|
||||
object? result = action.DynamicInvoke(parameters);
|
||||
|
||||
if (result is Task<T?> task)
|
||||
{
|
||||
return task;
|
||||
}
|
||||
|
||||
return Task.FromResult(default(T));
|
||||
}
|
||||
|
||||
private bool IsDryRun(MethodInfo method)
|
||||
{
|
||||
return method.GetCustomAttributes(typeof(DryRunSafeguardAttribute), true).Any() && _config.IsDryRun;
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace Infrastructure.Interceptors;
|
||||
|
||||
public interface IDryRunInterceptor
|
||||
{
|
||||
void Intercept(Action action);
|
||||
|
||||
Task InterceptAsync(Delegate action, params object[] parameters);
|
||||
|
||||
Task<T?> InterceptAsync<T>(Delegate action, params object[] parameters);
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
namespace Infrastructure.Interceptors;
|
||||
|
||||
public interface IDryRunService : IInterceptedService
|
||||
{
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
namespace Infrastructure.Interceptors;
|
||||
|
||||
public interface IInterceptedService
|
||||
{
|
||||
public object Proxy { get; set; }
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
namespace Infrastructure.Interceptors;
|
||||
|
||||
public class InterceptedService : IInterceptedService
|
||||
{
|
||||
private object? _proxy;
|
||||
|
||||
public object Proxy
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_proxy is null)
|
||||
{
|
||||
throw new Exception("Proxy is not set");
|
||||
}
|
||||
|
||||
return _proxy;
|
||||
}
|
||||
|
||||
set => _proxy = value;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user