You are viewing a plain text version of this content. The canonical link for it is here.
Posted to mod_python-commits@quetz.apache.org by nl...@apache.org on 2005/04/11 08:27:18 UTC

svn commit: r160840 - httpd/mod_python/trunk/lib/python/mod_python/FileSession.py

Author: nlehuen
Date: Sun Apr 10 23:27:16 2005
New Revision: 160840

URL: http://svn.apache.org/viewcvs?view=rev&rev=160840
Log:
Jim Gallacher's contribution : uses of separate locks for session-level and file-level locking + a little more cleanup on file path construction.

Modified:
    httpd/mod_python/trunk/lib/python/mod_python/FileSession.py

Modified: httpd/mod_python/trunk/lib/python/mod_python/FileSession.py
URL: http://svn.apache.org/viewcvs/httpd/mod_python/trunk/lib/python/mod_python/FileSession.py?view=diff&r1=160839&r2=160840
==============================================================================
--- httpd/mod_python/trunk/lib/python/mod_python/FileSession.py (original)
+++ httpd/mod_python/trunk/lib/python/mod_python/FileSession.py Sun Apr 10 23:27:16 2005
@@ -19,9 +19,10 @@
 
 import cPickle
 import tempfile
-import os
+import os, os.path
 import apache, _apache
 import cStringIO
+import time
 import traceback
 
 from mod_python import Session
@@ -30,26 +31,44 @@
 
 # Credits : this was initially contributed by dharana <dh...@dharana.net>
 class FileSession(Session.BaseSession):
-    def __init__(self, req, sid=0, secret=None, timeout=0, lock=1):
+    def __init__(self, req, sid=0, secret=None,
+                timeout=0, lock=0,
+                fast_cleanup=True, verify_cleanup=False):
+        
+        opts = req.get_options()
+        self._fast_cleanup = fast_cleanup
+        self._verify_cleanup = verify_cleanup
+        self._fast_timeout = timeout
+        self._sessdir = opts.get('FileSessionDir',tempdir)
         Session.BaseSession.__init__(self, req, sid=sid, secret=secret,
             timeout=timeout, lock=lock)
 
     def do_cleanup(self):
-        self._req.log_error('Sessions cleanup...',apache.APLOG_NOTICE)
+        self._req.log_error('Sessions cleanup (fast=%s, verify=%s) ...' 
+            % (self._fast_cleanup,self._verify_cleanup),
+            apache.APLOG_NOTICE)
 
-        # is there any faster way of doing this?
-        for f in os.listdir(tempdir):
+        for f in os.listdir(self._sessdir):
             if not f.startswith('mp_sess_'):
                 continue
-
+            
             try:
-                fp = file('%s/%s' % (tempdir, f))
-                try:
-                    dict = cPickle.load(fp)
-                    if (time() - dict['_accessed']) > dict['_timeout']:
-                        os.unlink('%s%s' % (tempdir, f))
-                finally:
-                    fp.close()
+                filename = os.path.join(self._sessdir, f)
+                if self._fast_cleanup:
+                    mtime = os.stat(filename).st_mtime
+                    if time.time() - mtime < self._fast_timeout:
+                        continue
+
+                if self._fast_cleanup and not self._verify_cleanup:        
+                        os.unlink(filename)
+                else:
+                    try:
+                        fp = file(filename)
+                        dict = cPickle.load(fp)
+                        if (time.time() - dict['_accessed']) > dict['_timeout']:
+                            os.unlink(filename)
+                    finally:
+                        fp.close()
             except:
                 s = cStringIO.StringIO()
                 traceback.print_exc(file=s)
@@ -57,37 +76,50 @@
                 self._req.log_error('Error while cleaning up the sessions : %s'%s)
 
     def do_load(self):
+        _apache._global_lock(self._req.server, 'fl_%s' % self._sid)
         try:
-            # again, is there a more pythonic way of doing this check?
-            # TODO : why does this load fails sometimes with an EOFError ?
-            fp = file('%s/mp_sess_%s' % (tempdir, self._sid))
             try:
-                data = cPickle.load(fp)
-                return data
-            finally:
-                fp.close()
-        except:
-            s = cStringIO.StringIO()
-            traceback.print_exc(file=s)
-            s = s.getvalue()
-            self._req.log_error('Error while loading a session : %s'%s)
-            return None
+                # again, is there a more pythonic way of doing this check?
+                # TODO : why does this load fails sometimes with an EOFError ?
+                fp = file(os.path.join(self._sessdir, 'mp_sess_%s' % self._sid))
+                try:
+                    data = cPickle.load(fp)
+                    return data
+                finally:
+                    fp.close()
+            except:
+                s = cStringIO.StringIO()
+                traceback.print_exc(file=s)
+                s = s.getvalue()
+                self._req.log_error('Error while loading a session : %s'%s)
+                return None
+        finally:
+            _apache._global_unlock(self._req.server, 'fl_%s' % self._sid)
 
     def do_save(self, dict):
+        _apache._global_lock(self._req.server, 'fl_%s' % self._sid)
         try:
-            fp = file('%s/mp_sess_%s' % (tempdir, self._sid), 'w+')
             try:
-                cPickle.dump(dict, fp, 2)
-            finally:
-                fp.close()
-        except:
-            s = cStringIO.StringIO()
-            traceback.print_exc(file=s)
-            s = s.getvalue()
-            self._req.log_error('Error while saving a session : %s'%s)
+                fp = file(os.path.join(self._sessdir, 'mp_sess_%s' % self._sid), 'w+')
+                try:
+                    cPickle.dump(dict, fp, 2)
+                finally:
+                    fp.close()
+            except:
+                s = cStringIO.StringIO()
+                traceback.print_exc(file=s)
+                s = s.getvalue()
+                self._req.log_error('Error while saving a session : %s'%s)
+        finally:
+            _apache._global_unlock(self._req.server, 'fl_%s' % self._sid)
 
     def do_delete(self):
+        _apache._global_lock(self._req.server, 'fl_%s' % self._sid)
         try:
-            os.unlink('%s/mp_sess_%s' % (tempdir, self._sid))
-        except Exception:
-            pass
+            try:
+                os.unlink(os.path.join(self._sessdir, 'mp_sess_%s' % self._sid))
+            except Exception:
+                pass
+        finally:
+            _apache._global_unlock(self._req.server, 'fl_%s' % self._sid)
+