Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Saving needed only when session change #672

Closed
wants to merge 1 commit into from

7 participants

@thomascoding

Saving needed only when session changes, otherwise session do save on any page load (why if its not changed?).
I think setTimeout can be replaced. 

Sorry for my bad english ;)

@thomascoding thomascoding Update lib/middleware/session.js
Saving needed only when session changes, otherwise session do save on any page load (why if its not changed?). 
I think setTimeout can be replaced. 

Sorry for my bad english ;)
e2142b7
@langpavel langpavel commented on the diff
lib/middleware/session.js
@@ -276,12 +275,19 @@ function session(options){
res.end = function(data, encoding){
res.end = end;
if (!req.session) return res.end(data, encoding);
- debug('saving');
- req.session.resetMaxAge();
- req.session.save(function(){
- debug('saved');
- res.end(data, encoding);
- });
+ if (originalHash == hash(req.session)) {
+ debug('unmodified session, dont save');
+ setTimeout(function(){

process.nextTick ?

@tj Owner
tj added a note

I dont get the intention here, we shouldn't need any nextTicks or timeouts in here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@langpavel langpavel commented on the diff
lib/middleware/session.js
@@ -276,12 +275,19 @@ function session(options){
res.end = function(data, encoding){
res.end = end;
if (!req.session) return res.end(data, encoding);
- debug('saving');
- req.session.resetMaxAge();
- req.session.save(function(){
- debug('saved');
- res.end(data, encoding);
- });
+ if (originalHash == hash(req.session)) {
+ debug('unmodified session, dont save');
+ setTimeout(function(){
+ res.end(data, encoding);
+ },0);
+ }else{
+ debug('saving');
+ req.session.resetMaxAge();

resetMaxAge called only when session change?
What happens if I login user and don't touch session ever after? Session expires? Very unpractical. This needs be solved first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@evadnoob

+1 I've patched my local connect with a similar change and this has fixed an issue where multiple asynchronous http requests from the same user overwrite session data with stale data(originating from a req.session "get").

@jonathanong

never do setTimeout(fn, 0) in node. keep styles consistent - i see a }else{. also, address langpavel's comments.

needs a test, rebase, and squash. ping me when you've made these changes!

@thomascoding

jonathanong i cant make this changes on github. Also i cant test because i am currently don't use this lib. As you see other people make changes and this code solve problem.

@jonathanong

Okay I'll just close this for now. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 14, 2012
  1. @thomascoding

    Update lib/middleware/session.js

    thomascoding authored
    Saving needed only when session changes, otherwise session do save on any page load (why if its not changed?). 
    I think setTimeout can be replaced. 
    
    Sorry for my bad english ;)
This page is out of date. Refresh to see the latest.
Showing with 13 additions and 7 deletions.
  1. +13 −7 lib/middleware/session.js
View
20 lib/middleware/session.js
@@ -1,4 +1,3 @@
-
/*!
* Connect - session
* Copyright(c) 2010 Sencha Inc.
@@ -276,12 +275,19 @@ function session(options){
res.end = function(data, encoding){
res.end = end;
if (!req.session) return res.end(data, encoding);
- debug('saving');
- req.session.resetMaxAge();
- req.session.save(function(){
- debug('saved');
- res.end(data, encoding);
- });
+ if (originalHash == hash(req.session)) {
+ debug('unmodified session, dont save');
+ setTimeout(function(){

process.nextTick ?

@tj Owner
tj added a note

I dont get the intention here, we shouldn't need any nextTicks or timeouts in here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ res.end(data, encoding);
+ },0);
+ }else{
+ debug('saving');
+ req.session.resetMaxAge();

resetMaxAge called only when session change?
What happens if I login user and don't touch session ever after? Session expires? Very unpractical. This needs be solved first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ req.session.save(function(){
+ debug('save session');
+ res.end(data, encoding);
+ });
+ }
};
// generate the session
Something went wrong with that request. Please try again.