Skip to content

Commit

Permalink
More configuration stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
aritchie committed Sep 21, 2024
1 parent f66f1db commit addfc0b
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 26 deletions.
1 change: 0 additions & 1 deletion samples/Sample/Handlers/SingletonRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ namespace Sample.Handlers;


[SingletonHandler]
[TimedLogging(3000)]
public class SingletonRequestHandler(IMediator mediator, AppSqliteConnection data) : IRequestHandler<MyMessageRequest, MyMessageResponse>
{
// [Cache(Storage = StoreType.File, MaxAgeSeconds = 30, OnlyForOffline = true)]
Expand Down
6 changes: 6 additions & 0 deletions samples/Sample/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
"My.Namespace.Contact": "https://otherlocalhost",
"My.Namespace.*" : "https://localhost"
},
"Performance": {
// * works here
"*": {
"ErrorThresholdMilliseconds": 5000
}
},
"Offline": {
// * probably a bad idea - block?
"*": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ CancellationToken cancellationToken
return await next().ConfigureAwait(false);

var acrossSessions = true;
var section = configuration.GetHandlerSection("Offline", request!, this);
var section = configuration.GetHandlerSection("Offline", request!, requestHandler);
if (section == null)
{
var cfg = requestHandler.GetHandlerHandleMethodAttribute<TRequest, OfflineAvailableAttribute>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
using System.Reflection;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration;
using Shiny.Mediator.Infrastructure;

namespace Shiny.Mediator.Caching.Infrastructure;


public class CachingRequestMiddleware<TRequest, TResult>(IMemoryCache cache) : IRequestMiddleware<TRequest, TResult>
public class CachingRequestMiddleware<TRequest, TResult>(
IConfiguration configuration,
IMemoryCache cache
) : IRequestMiddleware<TRequest, TResult>
{
public async Task<TResult> Process(
TRequest request,
Expand All @@ -16,6 +20,8 @@ CancellationToken cancellationToken
{
if (typeof(TResult) == typeof(Unit))
return await next().ConfigureAwait(false);

configuration.GetHandlerSection("Cache", request, requestHandler);

var attribute = requestHandler.GetHandlerHandleMethodAttribute<TRequest, CacheAttribute>();
attribute ??= request!.GetType().GetCustomAttribute<CacheAttribute>();
Expand Down
8 changes: 4 additions & 4 deletions src/Shiny.Mediator/MediatorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static IServiceCollection AddShinyMediator(this IServiceCollection servic
cfg.AddHttpClient();
cfg.AddOpenStreamMiddleware(typeof(TimerRefreshStreamRequestMiddleware<,>));
cfg.AddEventExceptionHandlingMiddleware();
cfg.AddTimedMiddleware();
cfg.AddPerformanceLoggingMiddleware();
}

services.TryAddSingleton<IMediator, Impl.Mediator>();
Expand All @@ -55,12 +55,12 @@ public static IServiceCollection AddShinyMediator(this IServiceCollection servic


/// <summary>
/// Timed middleware logging
/// Performance logging middleware
/// </summary>
/// <param name="cfg"></param>
/// <returns></returns>
public static ShinyConfigurator AddTimedMiddleware(this ShinyConfigurator cfg)
=> cfg.AddOpenRequestMiddleware(typeof(TimedLoggingRequestMiddleware<,>));
public static ShinyConfigurator AddPerformanceLoggingMiddleware(this ShinyConfigurator cfg)
=> cfg.AddOpenRequestMiddleware(typeof(PerformanceLoggingRequestMiddleware<,>));


/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
using System.Diagnostics;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Shiny.Mediator.Infrastructure;

namespace Shiny.Mediator.Middleware;


public class TimedLoggingRequestMiddleware<TRequest, TResult>(
public class PerformanceLoggingRequestMiddleware<TRequest, TResult>(
IConfiguration configuration,
ILogger<TRequest> logger
) : IRequestMiddleware<TRequest, TResult>
{
public async Task<TResult> Process(TRequest request, RequestHandlerDelegate<TResult> next, IRequestHandler requestHandler, CancellationToken cancellationToken)
{
var attribute = requestHandler.GetHandlerHandleMethodAttribute<TRequest, TimedLoggingAttribute>();
if (attribute == null)
var section = configuration.GetHandlerSection("Performance", request!, requestHandler);
if (section == null)
return await next().ConfigureAwait(false);

var millis = section.GetValue("ErrorThresholdMilliseconds", 5000);
var ts = TimeSpan.FromMilliseconds(millis);
var sw = Stopwatch.StartNew();
var result = await next();
sw.Stop();

var ts = TimeSpan.FromMilliseconds(attribute.ErrorThresholdMillis);
if (attribute.ErrorThresholdMillis > 0 && sw.Elapsed > ts)

if (sw.Elapsed > ts)
logger.LogError("{RequestType} took longer than {Threshold} to execute - {Elapsed}", typeof(TRequest), ts, sw.Elapsed);

else if (logger.IsEnabled(LogLevel.Debug))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ CancellationToken cancellationToken
)
{
var refreshSeconds = 0;
var attribute = requestHandler.GetHandlerHandleMethodAttribute<TRequest, TimerRefreshAttribute>();
if (attribute != null)
var section = config.GetHandlerSection("TimerRefresh", request, requestHandler);
if (section != null)
{
refreshSeconds = attribute.RefreshSeconds;
refreshSeconds = section.GetValue("Interval", 0);
}
else
{
var section = config.GetHandlerSection("TimerRefresh", request, this);
if (section != null)
refreshSeconds = section.GetValue("Interval", 0);
var attribute = requestHandler.GetHandlerHandleMethodAttribute<TRequest, TimerRefreshAttribute>();
if (attribute != null)
refreshSeconds = attribute.RefreshSeconds;
}

if (refreshSeconds <= 0)
Expand Down
7 changes: 0 additions & 7 deletions src/Shiny.Mediator/TimedLoggingAttribute.cs

This file was deleted.

0 comments on commit addfc0b

Please sign in to comment.