aboutsummaryrefslogtreecommitdiff
path: root/plugins/SessionProvider
diff options
context:
space:
mode:
authorLibravatar vnugent <public@vaughnnugent.com>2023-11-02 01:50:06 -0400
committerLibravatar vnugent <public@vaughnnugent.com>2023-11-02 01:50:06 -0400
commit9ddece0eac4dc3718c4f9279b4695d645a3e3cef (patch)
tree85f24fe1ee6f3845ef5bbb390530ea7e8042bbf2 /plugins/SessionProvider
parent43c9196b01799e334bde92e892f0bac47759901a (diff)
also carried away
Diffstat (limited to 'plugins/SessionProvider')
-rw-r--r--plugins/SessionProvider/src/RuntimeSessionProvider.cs54
-rw-r--r--plugins/SessionProvider/src/SessionProviderEntry.cs76
2 files changed, 38 insertions, 92 deletions
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<ISessionProvider> _asm;
-
- private Func<IHttpEvent, bool> _canProcessMethod;
- private Action<PluginBase, ILogProvider> _loadMethod;
- private ISessionProvider _ref;
+ private readonly Func<IHttpEvent, bool> _canProcessMethod;
+ private readonly ISessionProvider _ref;
- public RuntimeSessionProvider(AssemblyLoader<ISessionProvider> 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<Action<PluginBase, ILogProvider>>("Load")
- ?? throw new MissingMethodException("Provider is missing required Load method");
-
- //Load canprocess method
- _canProcessMethod = asm.TryGetMethod<Func<IHttpEvent, bool>>("CanProcess")
- ?? throw new MissingMethodException("Provider is missing required CanProcess method");
- }
-
- public ValueTask<SessionHandle> 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<Func<IHttpEvent, bool>>(externProvider, "CanProcess");
}
- public void Load(PluginBase pbase, ILogProvider localized)
- {
- _loadMethod(pbase, localized);
- }
+ ///<inheritdoc/>
+ public ValueTask<SessionHandle> GetSessionAsync(IHttpEvent entity, CancellationToken cancellationToken) => _ref.GetSessionAsync(entity, cancellationToken);
- protected override void Free()
- {
- _asm.Dispose();
- _canProcessMethod = null!;
- _loadMethod = null!;
- _ref = null!;
- }
+ ///<inheritdoc/>
+ 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<RuntimeSessionProvider> providers = new();
- try
- {
- Log.Verbose("Loading all specified session providers");
+ Log.Verbose("Loading all specified session providers");
- //Get all provider names
- IEnumerable<string> providerAssemblyNames = PluginConfig.GetProperty("provider_assemblies")
+ //Get all provider names
+ IEnumerable<string> 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<ISessionProvider> prov = this.LoadAssembly<ISessionProvider>(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<ISessionProvider>(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>(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<RuntimeSessionProvider>());
-
- //Cleanup assemblies
- current.TryForeach(static p => p.Dispose());
+ ProviderArray = Array.Empty<RuntimeSessionProvider>();
}
}
}