From 9ddece0eac4dc3718c4f9279b4695d645a3e3cef Mon Sep 17 00:00:00 2001 From: vnugent Date: Thu, 2 Nov 2023 01:50:06 -0400 Subject: also carried away --- .../SessionProvider/src/RuntimeSessionProvider.cs | 54 ++++----------- .../SessionProvider/src/SessionProviderEntry.cs | 76 ++++++++-------------- 2 files changed, 38 insertions(+), 92 deletions(-) (limited to 'plugins/SessionProvider') diff --git a/plugins/SessionProvider/src/RuntimeSessionProvider.cs b/plugins/SessionProvider/src/RuntimeSessionProvider.cs index 28397c9..1058e2e 100644 --- a/plugins/SessionProvider/src/RuntimeSessionProvider.cs +++ b/plugins/SessionProvider/src/RuntimeSessionProvider.cs @@ -26,58 +26,28 @@ using System; using System.Threading; using System.Threading.Tasks; -using VNLib.Utils; -using VNLib.Utils.Logging; using VNLib.Net.Http; -using VNLib.Plugins.Extensions.Loading; +using VNLib.Utils.Resources; namespace VNLib.Plugins.Essentials.Sessions { - internal sealed class RuntimeSessionProvider : VnDisposeable, ISessionProvider + internal sealed class RuntimeSessionProvider : ISessionProvider { - private readonly AssemblyLoader _asm; - - private Func _canProcessMethod; - private Action _loadMethod; - private ISessionProvider _ref; + private readonly Func _canProcessMethod; + private readonly ISessionProvider _ref; - public RuntimeSessionProvider(AssemblyLoader asm) + public RuntimeSessionProvider(ISessionProvider externProvider) { - _asm = asm; + _ref = externProvider; - //Store ref to the resource to avoid loads - _ref = asm.Resource; - - //Get load method - _loadMethod = asm.TryGetMethod>("Load") - ?? throw new MissingMethodException("Provider is missing required Load method"); - - //Load canprocess method - _canProcessMethod = asm.TryGetMethod>("CanProcess") - ?? throw new MissingMethodException("Provider is missing required CanProcess method"); - } - - public ValueTask GetSessionAsync(IHttpEvent entity, CancellationToken cancellationToken) - { - return _ref.GetSessionAsync(entity, cancellationToken); - } - - public bool CanProcess(IHttpEvent ev) - { - return _canProcessMethod(ev); + //Load canprocess method dynamically + _canProcessMethod = ManagedLibrary.GetMethod>(externProvider, "CanProcess"); } - public void Load(PluginBase pbase, ILogProvider localized) - { - _loadMethod(pbase, localized); - } + /// + public ValueTask GetSessionAsync(IHttpEvent entity, CancellationToken cancellationToken) => _ref.GetSessionAsync(entity, cancellationToken); - protected override void Free() - { - _asm.Dispose(); - _canProcessMethod = null!; - _loadMethod = null!; - _ref = null!; - } + /// + public bool CanProcess(IHttpEvent ev) => _canProcessMethod(ev); } } diff --git a/plugins/SessionProvider/src/SessionProviderEntry.cs b/plugins/SessionProvider/src/SessionProviderEntry.cs index 61808d1..b5ba6fa 100644 --- a/plugins/SessionProvider/src/SessionProviderEntry.cs +++ b/plugins/SessionProvider/src/SessionProviderEntry.cs @@ -23,7 +23,6 @@ */ using System; -using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -33,7 +32,6 @@ using System.ComponentModel.Design; using VNLib.Net.Http; using VNLib.Utils; using VNLib.Utils.Logging; -using VNLib.Utils.Extensions; using VNLib.Plugins.Attributes; using VNLib.Plugins.Extensions.Loading; @@ -67,65 +65,46 @@ namespace VNLib.Plugins.Essentials.Sessions { List providers = new(); - try - { - Log.Verbose("Loading all specified session providers"); + Log.Verbose("Loading all specified session providers"); - //Get all provider names - IEnumerable providerAssemblyNames = PluginConfig.GetProperty("provider_assemblies") + //Get all provider names + IEnumerable providerAssemblyNames = PluginConfig.GetProperty("provider_assemblies") .EnumerateArray() .Where(s => s.GetString() != null) .Select(s => s.GetString()!); - - - foreach(string asm in providerAssemblyNames) - { - Log.Verbose("Loading {dll} session provider", asm); - - //Attempt to load provider - AssemblyLoader prov = this.LoadAssembly(asm); - - try - { - //Create localized log - ILogProvider scopded = Log.CreateScope(Path.GetFileName(asm)); - RuntimeSessionProvider p = new(prov); - - //Call load method - p.Load(this, scopded); - - //Add to list - providers.Add(p); - } - catch - { - prov.Dispose(); - throw; - } - } + foreach (string asm in providerAssemblyNames) + { + Log.Verbose("Loading {dll} session provider", asm); - if(providers.Count > 0) + try { - //Create array for searching for providers - _provider = new(providers.ToArray()); + //Attempt to load provider + ISessionProvider prov = this.CreateServiceExternal(asm); - Log.Information("Loaded {count} session providers", providers.Count); + //Add to list + providers.Add(new(prov)); } - else + catch (Exception ex) { - Log.Information("No session providers loaded"); + Log.Error("Failed to load session provider {dll}:\n{error}", asm, ex); } + } - Log.Information("Plugin loaded"); + if (providers.Count > 0) + { + //Create array for searching for providers + _provider = new(providers.ToArray()); + + Log.Information("Loaded {count} session providers", providers.Count); } - catch + else { - //Dispose providers - providers.ForEach(static s => s.Dispose()); - throw; + Log.Information("No session providers loaded"); } + + Log.Information("Plugin loaded"); } protected override void OnUnLoad() @@ -173,16 +152,13 @@ namespace VNLib.Plugins.Essentials.Sessions } //Return empty session - return new ValueTask(SessionHandle.Empty); + return new (SessionHandle.Empty); } protected override void Free() { //Remove current providers so we can dispose them - RuntimeSessionProvider[] current = Interlocked.Exchange(ref ProviderArray, Array.Empty()); - - //Cleanup assemblies - current.TryForeach(static p => p.Dispose()); + ProviderArray = Array.Empty(); } } } -- cgit