From 6138eb6ba9eded16044a770bd5cf08d431375802 Mon Sep 17 00:00:00 2001 From: vnugent Date: Sat, 9 Sep 2023 19:50:57 -0400 Subject: detach api support --- .../src/SessionStore.cs | 55 +++++++++++++++------- 1 file changed, 37 insertions(+), 18 deletions(-) (limited to 'libs/VNLib.Plugins.Sessions.Cache.Client/src/SessionStore.cs') diff --git a/libs/VNLib.Plugins.Sessions.Cache.Client/src/SessionStore.cs b/libs/VNLib.Plugins.Sessions.Cache.Client/src/SessionStore.cs index 97dfa46..2709a65 100644 --- a/libs/VNLib.Plugins.Sessions.Cache.Client/src/SessionStore.cs +++ b/libs/VNLib.Plugins.Sessions.Cache.Client/src/SessionStore.cs @@ -43,32 +43,33 @@ namespace VNLib.Plugins.Sessions.Cache.Client /// public abstract class SessionStore : ISessionStore where TSession: IRemoteSession { -#nullable disable - - /* - * Default imple for serializer - */ + + /// + /// Used to serialize access to session instances. Unless overridden, uses a + /// implementation. + /// protected virtual ISessionSerialzer Serializer { get; } = new SessionSerializer(100); /// /// The that provides session ids for connections /// protected abstract ISessionIdFactory IdFactory { get; } + /// /// The backing cache store /// protected abstract IRemoteCacheStore Cache { get; } + /// /// The session factory, produces sessions from their initial data and session-id /// protected abstract ISessionFactory SessionFactory { get; } + /// /// The log provider for writing background update exceptions to /// protected abstract ILogProvider Log { get; } -#nullable enable - /// public virtual async ValueTask GetSessionAsync(IHttpEvent entity, CancellationToken cancellationToken) { @@ -209,6 +210,28 @@ namespace VNLib.Plugins.Sessions.Cache.Client //Update data and id _ = UpdateSessionAndIdAsync(session, newId); } + else if(status.HasFlag(SessionStatus.Detach)) + { + /* + * Special case. We are regenerating the session id, but we are not updating the session. + * This will cause the client's session id to detach from the current session. + * + * All other updates will be persisted to the cache. + * + * The id should require regeneration on the user's next request then attach a new session. + * + * The session is still valid, however the current connection should effectivly be 'detatched' + * from it. + */ + + if (!IdFactory.RegenerationSupported) + { + throw new SessionException("Session id regeneration is not supported by this store"); + } + + _ = IdFactory.RegenerateId(entity); + _ = UpdateSessionAndIdAsync(session, null); + } else if (status.HasFlag(SessionStatus.UpdateOnly)) { //Just run update @@ -243,7 +266,6 @@ namespace VNLib.Plugins.Sessions.Cache.Client //Update the session's data async await Cache.AddOrUpdateObjectAsync(session.SessionID, newId, sessionData); - /* * If the session id changes, the old sesion can be invalidated * and the session will be recovered from cache on load @@ -280,30 +302,27 @@ namespace VNLib.Plugins.Sessions.Cache.Client /// A task that completes when the session is destroyed protected virtual async Task DeleteSessionAsync(TSession session) { + Exception? cause = null; + try - { + { //Update the session's data async await Cache.DeleteObjectAsync(session.SessionID); - - //Destroy the session - session.Destroy(null); } catch(ObjectNotFoundException) { //ingore onfe, if the session does not exist in cache - - //Destroy the session - session.Destroy(null); } catch (Exception ex) { Log.Error("Exception raised during session delete, ID {id}\n{ex}", session.SessionID, ex); - - //Destroy the session with an error - session.Destroy(ex); + cause = ex; } finally { + //Always destroy the session + session.Destroy(cause); + //Release the session now that delete has been set Serializer.Release(session); } -- cgit