aboutsummaryrefslogtreecommitdiff
path: root/libs/VNLib.Plugins.Sessions.VNCache
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 /libs/VNLib.Plugins.Sessions.VNCache
parent43c9196b01799e334bde92e892f0bac47759901a (diff)
also carried away
Diffstat (limited to 'libs/VNLib.Plugins.Sessions.VNCache')
-rw-r--r--libs/VNLib.Plugins.Sessions.VNCache/src/WebSessionIdFactory.cs26
-rw-r--r--libs/VNLib.Plugins.Sessions.VNCache/src/WebSessionProvider.cs31
-rw-r--r--libs/VNLib.Plugins.Sessions.VNCache/src/WebSessionProviderEntry.cs61
-rw-r--r--libs/VNLib.Plugins.Sessions.VNCache/src/WebSessionStore.cs24
4 files changed, 46 insertions, 96 deletions
diff --git a/libs/VNLib.Plugins.Sessions.VNCache/src/WebSessionIdFactory.cs b/libs/VNLib.Plugins.Sessions.VNCache/src/WebSessionIdFactory.cs
index 03d8980..3cdf56b 100644
--- a/libs/VNLib.Plugins.Sessions.VNCache/src/WebSessionIdFactory.cs
+++ b/libs/VNLib.Plugins.Sessions.VNCache/src/WebSessionIdFactory.cs
@@ -38,13 +38,14 @@ namespace VNLib.Plugins.Sessions.VNCache
/// <see cref="ISessionIdFactory"/> implementation, using
/// http cookies as session id storage
/// </summary>
- [ConfigurationName(WebSessionProviderEntry.WEB_SESSION_CONFIG)]
+ [ConfigurationName(WebSessionProvider.WEB_SESSION_CONFIG)]
internal sealed class WebSessionIdFactory : ISessionIdFactory
{
public TimeSpan ValidFor { get; }
///<inheritdoc/>
public bool RegenerationSupported { get; } = true;
+
///<inheritdoc/>
public bool RegenIdOnEmptyEntry { get; } = true;
@@ -65,13 +66,14 @@ namespace VNLib.Plugins.Sessions.VNCache
_cookieSize = (int)cookieSize;
}
- public WebSessionIdFactory(PluginBase pbase, IConfigScope config)
- {
- _cookieSize = (int)config["cookie_size"].GetUInt32();
- SessionCookieName = config["cookie_name"].GetString()
- ?? throw new KeyNotFoundException($"Missing required element 'cookie_name' for config '{WebSessionProviderEntry.WEB_SESSION_CONFIG}'");
- ValidFor = config["valid_for_sec"].GetTimeSpan(TimeParseType.Seconds);
- }
+ //Create instance from config
+ public WebSessionIdFactory(PluginBase plugin, IConfigScope config):
+ this(
+ config["cookie_size"].GetUInt32(),
+ config.GetRequiredProperty("cookie_name", p => p.GetString()!),
+ config["valid_for_sec"].GetTimeSpan(TimeParseType.Seconds)
+ )
+ { }
public string RegenerateId(IHttpEvent entity)
@@ -91,17 +93,13 @@ namespace VNLib.Plugins.Sessions.VNCache
};
//Set the session id cookie
- entity.Server.SetCookie(cookie);
+ entity.Server.SetCookie(in cookie);
//return session-id value from cookie value
return sessionId;
}
- public string? TryGetSessionId(IHttpEvent entity)
- {
- //Get session cookie
- return entity.Server.RequestCookies.GetValueOrDefault(SessionCookieName);
- }
+ public string? TryGetSessionId(IHttpEvent entity) => entity.Server.RequestCookies.GetValueOrDefault(SessionCookieName);
public bool CanService(IHttpEvent entity)
{
diff --git a/libs/VNLib.Plugins.Sessions.VNCache/src/WebSessionProvider.cs b/libs/VNLib.Plugins.Sessions.VNCache/src/WebSessionProvider.cs
index 655bae8..5516fef 100644
--- a/libs/VNLib.Plugins.Sessions.VNCache/src/WebSessionProvider.cs
+++ b/libs/VNLib.Plugins.Sessions.VNCache/src/WebSessionProvider.cs
@@ -27,6 +27,7 @@ using System.Threading;
using System.Threading.Tasks;
using VNLib.Net.Http;
+using VNLib.Utils.Logging;
using VNLib.Plugins.Essentials;
using VNLib.Plugins.Essentials.Sessions;
using VNLib.Plugins.Extensions.Loading;
@@ -34,17 +35,20 @@ using VNLib.Plugins.Extensions.Loading;
namespace VNLib.Plugins.Sessions.VNCache
{
- [ConfigurationName(WebSessionProviderEntry.WEB_SESSION_CONFIG)]
- internal sealed class WebSessionProvider : ISessionProvider
+ [ExternService]
+ [ConfigurationName(WEB_SESSION_CONFIG)]
+ public sealed class WebSessionProvider : ISessionProvider
{
+ internal const string WEB_SESSION_CONFIG = "web";
+ internal const string LOGGER_SCOPE = "WEB-SESSIONS";
+
private static readonly SessionHandle _vf = new (null, FileProcessArgs.VirtualSkip, null);
private readonly WebSessionStore _sessions;
private readonly uint _maxConnections;
private uint _waitingConnections;
-
- public bool IsConnected => _sessions.IsConnected;
+
public WebSessionProvider(PluginBase plugin, IConfigScope config)
{
@@ -52,15 +56,20 @@ namespace VNLib.Plugins.Sessions.VNCache
//Init session provider
_sessions = plugin.GetOrCreateSingleton<WebSessionStore>();
- }
- private SessionHandle PostProcess(WebSession? session)
- {
- return session == null ? SessionHandle.Empty : new SessionHandle(session, OnSessionReleases);
+ ILogProvider logger = plugin.Log.CreateScope("WEB-SESSIONS");
+
+ logger.Information("Session provider loaded");
}
- private ValueTask OnSessionReleases(ISession session, IHttpEvent entity) => _sessions.ReleaseSessionAsync((WebSession)session, entity);
+ /*
+ * This is a dynamic method captured by the session loader to determine if the
+ * current session provider can process the incoming request.
+ */
+ public bool CanProcess(IHttpEvent entity) => _sessions.IsConnected;
+
+ ///<inheritdoc/>
public ValueTask<SessionHandle> GetSessionAsync(IHttpEvent entity, CancellationToken cancellationToken)
{
//Limit max number of waiting clients and make sure were connected
@@ -88,6 +97,10 @@ namespace VNLib.Plugins.Sessions.VNCache
}
}
+ private SessionHandle PostProcess(WebSession? session) => session == null ? SessionHandle.Empty : new (session, OnSessionReleases);
+
+ private ValueTask OnSessionReleases(ISession session, IHttpEvent entity) => _sessions.ReleaseSessionAsync((WebSession)session, entity);
+
private async Task<SessionHandle> AwaitAsyncGet(ValueTask<WebSession?> async)
{
//Inct wait count while async waiting
diff --git a/libs/VNLib.Plugins.Sessions.VNCache/src/WebSessionProviderEntry.cs b/libs/VNLib.Plugins.Sessions.VNCache/src/WebSessionProviderEntry.cs
deleted file mode 100644
index 3b1b1ac..0000000
--- a/libs/VNLib.Plugins.Sessions.VNCache/src/WebSessionProviderEntry.cs
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
-* Copyright (c) 2023 Vaughn Nugent
-*
-* Library: VNLib
-* Package: VNLib.Plugins.Essentials.Sessions.VNCache
-* File: WebSessionProviderEntry.cs
-*
-* WebSessionProviderEntry.cs is part of VNLib.Plugins.Essentials.Sessions.VNCache which is part of the larger
-* VNLib collection of libraries and utilities.
-*
-* VNLib.Plugins.Essentials.Sessions.VNCache is free software: you can redistribute it and/or modify
-* it under the terms of the GNU Affero General Public License as
-* published by the Free Software Foundation, either version 3 of the
-* License, or (at your option) any later version.
-*
-* VNLib.Plugins.Essentials.Sessions.VNCache is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-* GNU Affero General Public License for more details.
-*
-* You should have received a copy of the GNU Affero General Public License
-* along with this program. If not, see https://www.gnu.org/licenses/.
-*/
-
-using System;
-using System.Threading;
-using System.Threading.Tasks;
-
-using VNLib.Net.Http;
-using VNLib.Utils.Logging;
-using VNLib.Plugins.Extensions.Loading;
-using VNLib.Plugins.Essentials.Sessions;
-
-namespace VNLib.Plugins.Sessions.VNCache
-{
-
- public sealed class WebSessionProviderEntry : ISessionProvider
- {
- internal const string WEB_SESSION_CONFIG = "web";
-
- private WebSessionProvider? _sessions;
-
-
- //Web sessions can always be provided so long as cache is loaded
- public bool CanProcess(IHttpEvent entity) => _sessions != null && _sessions.IsConnected;
-
- ValueTask<SessionHandle> ISessionProvider.GetSessionAsync(IHttpEvent entity, CancellationToken cancellationToken)
- {
- return _sessions!.GetSessionAsync(entity, cancellationToken);
- }
-
-
- public void Load(PluginBase plugin, ILogProvider localized)
- {
- //Load session provider
- _sessions = plugin.GetOrCreateSingleton<WebSessionProvider>();
-
- localized.Information("Session provider loaded");
- }
- }
-} \ No newline at end of file
diff --git a/libs/VNLib.Plugins.Sessions.VNCache/src/WebSessionStore.cs b/libs/VNLib.Plugins.Sessions.VNCache/src/WebSessionStore.cs
index c0b1e5d..ec515f6 100644
--- a/libs/VNLib.Plugins.Sessions.VNCache/src/WebSessionStore.cs
+++ b/libs/VNLib.Plugins.Sessions.VNCache/src/WebSessionStore.cs
@@ -37,7 +37,7 @@ using VNLib.Plugins.Essentials.Sessions;
namespace VNLib.Plugins.Sessions.VNCache
{
- [ConfigurationName(WebSessionProviderEntry.WEB_SESSION_CONFIG)]
+ [ConfigurationName(WebSessionProvider.WEB_SESSION_CONFIG)]
internal sealed class WebSessionStore : SessionStore<WebSession>
{
const int MAX_SESSION_BUFFER_SIZE = 16 * 1024;
@@ -49,10 +49,10 @@ namespace VNLib.Plugins.Sessions.VNCache
protected override ISessionFactory<WebSession> SessionFactory { get; }
protected override ILogProvider Log => baseLog!;
- public WebSessionStore(PluginBase pbase, IConfigScope config)
+ public WebSessionStore(PluginBase plugin, IConfigScope config)
{
//Get id factory
- IdFactory = pbase.GetOrCreateSingleton<WebSessionIdFactory>();
+ IdFactory = plugin.GetOrCreateSingleton<WebSessionIdFactory>();
//Session factory
SessionFactory = new WebSessionFactory();
@@ -62,24 +62,24 @@ namespace VNLib.Plugins.Sessions.VNCache
* the config
*/
- string cachePrefix = config["cache_prefix"].GetString()
- ?? throw new KeyNotFoundException($"Missing required element 'cache_prefix' for config '{WebSessionProviderEntry.WEB_SESSION_CONFIG}'");
+ string cachePrefix = config.GetRequiredProperty("cache_prefix", p => p.GetString()!);
//Create a simple prefix cache provider
- IGlobalCacheProvider cache = pbase.GetOrCreateSingleton<VnGlobalCache>()
+ IGlobalCacheProvider? cache = plugin.GetDefaultGlobalCache()?
.GetPrefixedCache(cachePrefix, HashAlg.SHA256);
+ _ = cache ?? throw new MissingDependencyException("A global cache provider is required to store VNCache sessions. Please configure a cache provider");
+
+ //Get debug log if enabled
+ ILogProvider? sessDebugLog = plugin.HostArgs.HasArgument("--debug-sessions") ? plugin.Log.CreateScope("VNCache-Sessions") : null;
+
//Create cache store from global cache
- Cache = new GlobalCacheStore(cache, MAX_SESSION_BUFFER_SIZE);
+ Cache = new GlobalCacheStore(cache, MAX_SESSION_BUFFER_SIZE, sessDebugLog);
//Default log to plugin log
- baseLog = pbase.Log;
+ baseLog = plugin.Log.CreateScope(WebSessionProvider.LOGGER_SCOPE);
}
- public void InitLog(ILogProvider log)
- {
- baseLog = log;
- }
/// <summary>
/// A value that indicates if the remote cache client is connected