aboutsummaryrefslogtreecommitdiff
path: root/libs/VNLib.Plugins.Sessions.Cache.Client/src/SessionStore.cs
blob: 0797efba78c0de7e5f6dc5ea547ca9a182f4123e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
* Copyright (c) 2023 Vaughn Nugent
* 
* Library: VNLib
* Package: VNLib.Plugins.Sessions.Cache.Client
* File: SessionStore.cs 
*
* SessionStore.cs is part of VNLib.Plugins.Sessions.Cache.Client which is part of the larger 
* VNLib collection of libraries and utilities.
*
* VNLib.Plugins.Sessions.Cache.Client 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.Sessions.Cache.Client 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 System.Collections.Generic;

using VNLib.Net.Http;
using VNLib.Utils.Logging;
using VNLib.Plugins.Essentials.Sessions;
using VNLib.Plugins.Sessions.Cache.Client.Exceptions;

namespace VNLib.Plugins.Sessions.Cache.Client
{

    /// <summary>
    /// Provides an abstract <see cref="ISessionStore{TSession}"/> for managing sessions for HTTP connections
    /// remote cache backed sessions
    /// </summary>
    /// <typeparam name="TSession"></typeparam>
    public abstract class SessionStore<TSession> : ISessionStore<TSession> where TSession: IRemoteSession
    {
     
        /// <summary>
        /// Used to serialize access to session instances. Unless overridden, uses a <see cref="SessionSerializer{TSession}"/>
        /// implementation.
        /// </summary>
        protected virtual ISessionSerialzer<TSession> Serializer { get; } = new SessionSerializer<TSession>(100);

        /// <summary>
        /// The <see cref="ISessionIdFactory"/> that provides session ids for connections
        /// </summary>
        protected abstract ISessionIdFactory IdFactory { get; }

        /// <summary>
        /// The backing cache store
        /// </summary>
        protected abstract IRemoteCacheStore Cache { get; }

        /// <summary>
        /// The session factory, produces sessions from their initial data and session-id
        /// </summary>
        protected abstract ISessionFactory<TSession> SessionFactory { get; }

        /// <summary>
        /// The log provider for writing background update exceptions to
        /// </summary>
        protected abstract ILogProvider Log { get; }

        ///<inheritdoc/>
        public virtual async ValueTask<TSession?> GetSessionAsync(IHttpEvent entity, CancellationToken cancellationToken)
        {
            //Wrap exceptions in session exceptions
            try
            {
                if (!IdFactory.CanService(entity))
                {
                    return default;
                }

                //Get the session id from the entity
                string? sessionId = IdFactory.TryGetSessionId(entity);

                //If regeneration is not supported, return since id is null
                if (sessionId == null && !IdFactory.RegenerationSupported)
                {
                    return default;
                }

                if (sessionId == null)
                {
                    //Get new sessionid
                    sessionId = IdFactory.RegenerateId(entity);

                    //Create a new session
                    TSession? session = SessionFactory.GetNewSession(entity, sessionId, null);

                    if (session != null)
                    {
                        //Enter wait for the new session, this call should not block or yield
                        await Serializer.WaitAsync(session, cancellationToken);
                    }

                    return session;
                }
                else
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    Dictionary<string, string>? sessionData = null;

                    //See if the session is currently in use, if so wait for access to it
                    if (Serializer.TryGetSession(sessionId, out TSession? activeSession))
                    {
                        //reuse the session
                        await Serializer.WaitAsync(activeSession, cancellationToken);

                        //after wait has been completed, check the status of the session
                        if (activeSession.IsValid(out Exception? cause))
                        {
                            //Session is valid, we can use it
                            return activeSession;
                        }

                        //release the wait, no use for the session in invalid state
                        Serializer.Release(activeSession);

                        //Rethrow exception that cause invalidation
                        if (cause != null)
                        {
                            throw cause;
                        }

                        //Regen id and continue loading
                    }
                    else
                    {
                        //Session cannot be found local, so we need to retrieve it from cache
                        sessionData = await Cache.GetObjectAsync<Dictionary<string, string>>(sessionId, cancellationToken);
                    }


                    //If the cache entry is null, we may choose to regenrate the session id
                    if (sessionData == null && IdFactory.RegenIdOnEmptyEntry)
                    {
                        sessionId = IdFactory.RegenerateId(entity);
                    }

                    //Create a new session
                    TSession? session = SessionFactory.GetNewSession(entity, sessionId, sessionData);

                    if(session != null)
                    {
                        //Enter wait for the new session
                        await Serializer.WaitAsync(session, cancellationToken);
                    }

                    return session;
                }
            }
            catch(OperationCanceledException)
            {
                throw;
            }
            catch (TimeoutException)
            {
                throw;
            }
            catch (SessionException)
            {
                throw;
            }
            catch(Exception ex)
            {
                throw new SessionException("An exception occured during session processing", ex);
            }
        }

        ///<inheritdoc/>
        public virtual ValueTask ReleaseSessionAsync(TSession session, IHttpEvent entity)
        {
            //Get status on release
            SessionStatus status = session.GetStatus();

            //confirm the cache client is connected
            if(status != SessionStatus.None && !Cache.IsConnected)
            {
                throw new SessionUpdateFailedException("The required session operation cannot be completed because the cache is not connected");
            }

            //Delete status is required
            if (status.HasFlag(SessionStatus.Delete))
            {
                //Delete the session
                _ = DeleteSessionAsync(session);
            }
            else if (status.HasFlag(SessionStatus.RegenId))
            {
                if (!IdFactory.RegenerationSupported)
                {
                    throw new SessionException("Session id regeneration is not supported by this store");
                }

                //Get new id for session
                string newId = IdFactory.RegenerateId(entity);

                //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
                _ = UpdateSessionAndIdAsync(session, null);
            }
            else
            {
                //Always release the session after update
                Serializer.Release(session);
            }

            return ValueTask.CompletedTask;
        }

        /// <summary>
        /// Updates a mondified session on release, and optionally updates 
        /// its id
        /// </summary>
        /// <param name="session"></param>
        /// <param name="newId">The session's new id</param>
        /// <returns>A task that completes when the session update is complete</returns>
        /// <remarks>
        /// Unless overridden
        /// </remarks>
        protected virtual async Task UpdateSessionAndIdAsync(TSession session, string? newId)
        {
            try
            {
                //Get the session's data
                IDictionary<string, string> sessionData = session.GetSessionData();

                //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
                 */

                if(newId != null)
                {
                    session.Destroy(null);
                }
                else
                {
                    session.SessionUpdateComplete();
                }
            }
            catch (Exception ex)
            {
                Log.Error("Exception raised during session update, ID {id} NewID {nid}\n{ex}", session.SessionID, newId, ex);

                //Destroy the session with an error
                session.Destroy(ex);
            }
            finally
            {
                //Always release the session after update
                Serializer.Release(session);
            }
        }

        /// <summary>
        /// Delets a session when the session has the <see cref="SessionStatus.Delete"/>
        /// flag set
        /// </summary>
        /// <param name="session">The session to delete</param>
        /// <returns>A task that completes when the session is destroyed</returns>
        protected virtual async Task DeleteSessionAsync(TSession session)
        {
            Exception? cause = null;

            try
            { 
                //Update the session's data async
                _ = await Cache.DeleteObjectAsync(session.SessionID);
            }
            catch (Exception ex)
            {
                Log.Error("Exception raised during session delete, ID {id}\n{ex}", session.SessionID, ex);
                cause = ex;
            }
            finally
            {
                //Always destroy the session
                session.Destroy(cause);

                //Release the session now that delete has been set
                Serializer.Release(session);
            }
        }
    }
}