Skip to content

Commit eb6d51d

Browse files
committed
fix unnecessary cookie creation when updating the session data
1 parent 06e81f1 commit eb6d51d

3 files changed

Lines changed: 14 additions & 4 deletions

File tree

src/options.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub struct RocketFlexSessionOptions {
88
/// The session cookie's `HttpOnly` attribute (default: `true`)
99
pub http_only: bool,
1010
/// The session cookie's `Max-Age` attribute, in seconds. This also determines
11-
/// the session storage TTL, unless you specify a different `ttl` setting. (default: 14 days)
11+
/// the session storage TTL, unless you specify a different `ttl` setting. (default: 2 weeks)
1212
pub max_age: u32,
1313
/// The session cookie's `Path` attribute (default: `"/"`)
1414
pub path: String,

src/session.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,11 @@ where
223223
return;
224224
};
225225

226-
// Generate new cookie
227-
self.cookie_jar
228-
.add_private(create_session_cookie(id, self.options));
226+
// Generate new session cookie if needed
227+
if inner.is_new() {
228+
let session_cookie = create_session_cookie(id, self.options);
229+
self.cookie_jar.add_private(session_cookie);
230+
}
229231

230232
// Notify any cookie-based storage
231233
let save_result = self.storage.save_cookie(

src/session_inner.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ struct ActiveSession<T> {
1212
pending_data: Option<T>,
1313
/// Time-to-live in seconds
1414
ttl: u32,
15+
/// Whether this is a new session that hasn't been stored yet
16+
new: bool,
1517
}
1618
impl<T: Clone> ActiveSession<T> {
1719
/// Create a new active session with a generated ID, to be saved in storage
@@ -21,6 +23,7 @@ impl<T: Clone> ActiveSession<T> {
2123
data: new_data.clone(),
2224
pending_data: Some(new_data),
2325
ttl,
26+
new: true,
2427
}
2528
}
2629
/// Active session that already exists in storage
@@ -30,6 +33,7 @@ impl<T: Clone> ActiveSession<T> {
3033
data,
3134
pending_data: None,
3235
ttl,
36+
new: false,
3337
}
3438
}
3539
}
@@ -79,6 +83,10 @@ where
7983
self.current.as_ref().map(|s| s.ttl)
8084
}
8185

86+
pub(crate) fn is_new(&self) -> bool {
87+
self.current.as_ref().map(|s| s.new).unwrap_or(false)
88+
}
89+
8290
pub(crate) fn set_data(&mut self, new_data: T, default_ttl: u32) {
8391
match &mut self.current {
8492
Some(current) => current.pending_data = Some(new_data),

0 commit comments

Comments
 (0)