pavex_session/
store_.rs

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
use crate::SessionId;
use errors::{
    ChangeIdError, CreateError, DeleteError, DeleteExpiredError, LoadError, UpdateError,
    UpdateTtlError,
};
use serde_json::Value;
use std::{borrow::Cow, collections::HashMap, num::NonZeroUsize};

/// Where server-side session records are stored.
///
/// It is a thin wrapper
/// [around your chosen storage backend implementation][`SessionStorageBackend`],
/// removing the need to specify the concrete type of the storage backend
/// everywhere in your code.
#[derive(Debug)]
pub struct SessionStore(Box<dyn SessionStorageBackend>);

impl SessionStore {
    /// Creates a new session store using the provided backend.
    pub fn new<Backend>(backend: Backend) -> Self
    where
        Backend: SessionStorageBackend + 'static,
    {
        Self(Box::new(backend))
    }

    /// Creates a new session record in the store using the provided ID.
    pub async fn create(
        &self,
        id: &SessionId,
        record: SessionRecordRef<'_>,
    ) -> Result<(), CreateError> {
        self.0.create(id, record).await
    }

    /// Update the state of an existing session in the store.
    ///
    /// It overwrites the existing record with the provided one.
    pub async fn update(
        &self,
        id: &SessionId,
        record: SessionRecordRef<'_>,
    ) -> Result<(), UpdateError> {
        self.0.update(id, record).await
    }

    /// Update the TTL of an existing session record in the store.
    ///
    /// It leaves the session state unchanged.
    pub async fn update_ttl(
        &self,
        id: &SessionId,
        ttl: std::time::Duration,
    ) -> Result<(), UpdateTtlError> {
        self.0.update_ttl(id, ttl).await
    }

    /// Loads an existing session record from the store using the provided ID.
    ///
    /// If a session with the given ID exists, it is returned. If the session
    /// does not exist or has been invalidated (e.g., expired), `None` is
    /// returned.
    pub async fn load(&self, id: &SessionId) -> Result<Option<SessionRecord>, LoadError> {
        self.0.load(id).await
    }

    /// Deletes a session record from the store using the provided ID.
    ///
    /// If the session exists, it is removed from the store.
    pub async fn delete(&self, id: &SessionId) -> Result<(), DeleteError> {
        self.0.delete(id).await
    }

    /// Change the session id associated with an existing session record.
    ///
    /// The server-side state is left unchanged.
    pub async fn change_id(
        &self,
        old_id: &SessionId,
        new_id: &SessionId,
    ) -> Result<(), ChangeIdError> {
        self.0.change_id(old_id, new_id).await
    }

    /// Deletes expired session records from the store.
    pub async fn delete_expired(
        &self,
        batch_size: Option<NonZeroUsize>,
    ) -> Result<usize, DeleteExpiredError> {
        self.0.delete_expired(batch_size).await
    }
}

#[async_trait::async_trait]
/// The interface of a session storage backend.
pub trait SessionStorageBackend: std::fmt::Debug + Send + Sync {
    /// Creates a new session record in the store using the provided ID.
    async fn create(&self, id: &SessionId, record: SessionRecordRef<'_>)
        -> Result<(), CreateError>;

    /// Update the state of an existing session in the store.
    ///
    /// It overwrites the existing record with the provided one.
    async fn update(&self, id: &SessionId, record: SessionRecordRef<'_>)
        -> Result<(), UpdateError>;

    /// Update the TTL of an existing session record in the store.
    ///
    /// It leaves the session state unchanged.
    async fn update_ttl(
        &self,
        id: &SessionId,
        ttl: std::time::Duration,
    ) -> Result<(), UpdateTtlError>;

    /// Loads an existing session record from the store using the provided ID.
    ///
    /// If a session with the given ID exists, it is returned. If the session
    /// does not exist or has been invalidated (e.g., expired), `None` is
    /// returned.
    async fn load(&self, session_id: &SessionId) -> Result<Option<SessionRecord>, LoadError>;

    /// Deletes a session record from the store using the provided ID.
    ///
    /// If the session exists, it is removed from the store.
    async fn delete(&self, session_id: &SessionId) -> Result<(), DeleteError>;

    /// Change the session id associated with an existing session record.
    ///
    /// The server-side state is left unchanged.
    async fn change_id(&self, old_id: &SessionId, new_id: &SessionId) -> Result<(), ChangeIdError>;

    /// Deletes expired session records from the store.
    ///
    /// If `batch_size` is provided, the query will delete at most `batch_size` expired sessions.
    /// In either case, if successful, the method returns the number of expired sessions that
    /// have been deleted.
    ///
    /// # When should you delete in batches?
    ///
    /// If there are a lot of expired sessions in the database, deleting them all at once can
    /// cause performance issues. By deleting in batches, you can limit the number of sessions
    /// deleted in a single query, reducing the impact.
    ///
    /// # Do I need to call this method?
    ///
    /// It depends on the storage backend you are using. Some backends (e.g. Redis) have
    /// built-in support for expiring keys, so you may not need to call this method at all.
    ///
    /// If you're adding support for a new backend that has built-in support for expiring keys,
    /// you can simply return `Ok(0)` from this method.
    async fn delete_expired(
        &self,
        batch_size: Option<NonZeroUsize>,
    ) -> Result<usize, DeleteExpiredError>;
}

/// A server-side session record that's going to be stored in the
/// chosen storage backend.
#[derive(Debug)]
pub struct SessionRecordRef<'session> {
    /// The set of key-value pairs attached to a session.
    pub state: Cow<'session, HashMap<String, Value>>,
    /// The session time-to-live.
    pub ttl: std::time::Duration,
}

impl SessionRecordRef<'_> {
    pub(crate) fn empty(ttl: std::time::Duration) -> Self {
        Self {
            state: Cow::Owned(HashMap::new()),
            ttl,
        }
    }
}

/// A server-side session record that was retrieved from the
/// chosen storage backend.
#[derive(Debug)]
pub struct SessionRecord {
    /// The set of key-value pairs attached to a session.
    pub state: HashMap<String, Value>,
    /// The session time-to-live.
    pub ttl: std::time::Duration,
}

/// Errors that can occur when interacting with a session storage backend.
pub mod errors {
    use crate::SessionId;

    #[non_exhaustive]
    #[derive(Debug, thiserror::Error)]
    /// The error returned by [`SessionStorageBackend::create`][super::SessionStorageBackend::create].
    pub enum CreateError {
        /// Failed to serialize the session state.
        #[error("Failed to serialize the session state.")]
        SerializationError(#[from] serde_json::Error),
        #[error(transparent)]
        /// A session with the same ID already exists.
        DuplicateId(#[from] DuplicateIdError),
        /// Something else went wrong when creating a new session record.
        #[error("Something went wrong when creating a new session record.")]
        Other(#[source] anyhow::Error),
    }

    #[non_exhaustive]
    #[derive(Debug, thiserror::Error)]
    /// The error returned by [`SessionStorageBackend::update`][super::SessionStorageBackend::update].
    pub enum UpdateError {
        #[error("Failed to serialize the session state.")]
        /// Failed to serialize the session state.
        SerializationError(#[from] serde_json::Error),
        #[error(transparent)]
        /// There is no session with the given ID.
        UnknownIdError(#[from] UnknownIdError),
        /// Something else went wrong when updating the session record.
        #[error("Something went wrong when updating the session record.")]
        Other(#[source] anyhow::Error),
    }

    #[non_exhaustive]
    #[derive(Debug, thiserror::Error)]
    /// The error returned by [`SessionStorageBackend::update_ttl`][super::SessionStorageBackend::update_ttl].
    pub enum UpdateTtlError {
        #[error(transparent)]
        /// There is no session with the given ID.
        UnknownId(#[from] UnknownIdError),
        /// Something else went wrong when updating the session record.
        #[error("Something went wrong when updating the TTL of the session record.")]
        Other(#[source] anyhow::Error),
    }

    #[non_exhaustive]
    #[derive(Debug, thiserror::Error)]
    /// The error returned by [`SessionStorageBackend::load`][super::SessionStorageBackend::load].
    pub enum LoadError {
        #[error("Failed to deserialize the session state.")]
        /// Failed to deserialize the session state.
        DeserializationError(#[source] anyhow::Error),
        /// Something else went wrong when loading the session record.
        #[error("Something went wrong when loading the session record.")]
        Other(#[source] anyhow::Error),
    }

    #[non_exhaustive]
    #[derive(Debug, thiserror::Error)]
    /// The error returned by [`SessionStorageBackend::delete`][super::SessionStorageBackend::delete].
    pub enum DeleteError {
        #[error(transparent)]
        /// There is no session with the given ID.
        UnknownId(#[from] UnknownIdError),
        /// Something else went wrong when deleting the session record.
        #[error("Something went wrong when deleting the session record.")]
        Other(#[source] anyhow::Error),
    }

    #[non_exhaustive]
    #[derive(Debug, thiserror::Error)]
    /// The error returned by [`SessionStorageBackend::change_id`][super::SessionStorageBackend::change_id].
    pub enum ChangeIdError {
        #[error(transparent)]
        /// There is no session with the given ID.
        UnknownId(#[from] UnknownIdError),
        #[error(transparent)]
        /// There is already a session associated with the new ID>
        DuplicateId(#[from] DuplicateIdError),
        /// Something else went wrong when deleting the session record.
        #[error("Something went wrong when changing the session id for a session record.")]
        Other(#[source] anyhow::Error),
    }

    /// The error returned by [`SessionStorageBackend::delete_expired`][super::SessionStorageBackend::delete_expired].
    #[derive(Debug, thiserror::Error)]
    #[error("Something went wrong when deleting expired sessions")]
    pub struct DeleteExpiredError(#[from] anyhow::Error);

    #[derive(thiserror::Error)]
    #[error("There is no session with the given id")]
    /// There is no session with the given ID.
    pub struct UnknownIdError {
        pub id: SessionId,
    }

    impl std::fmt::Debug for UnknownIdError {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            f.write_str("UnknownIdError")
        }
    }

    #[derive(thiserror::Error)]
    #[error("A session with the same ID already exists.")]
    /// A session with the same ID already exists.
    pub struct DuplicateIdError {
        pub id: SessionId,
    }

    impl std::fmt::Debug for DuplicateIdError {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            f.write_str("DuplicateIdError")
        }
    }
}