;;;; Programmer: Kevin M. Rosenberg
;;;; Date Started: July 2003
;;;;
-;;;; $Id: sessions.lisp,v 1.1 2003/07/16 16:02:21 kevin Exp $
+;;;; $Id: sessions.lisp,v 1.3 2003/07/16 20:40:43 kevin Exp $
;;;;
;;;; This file and Wol are Copyright (c) 2003 by Kevin M. Rosenberg
;;;; *************************************************************************
(in-package #:wol)
-(defun start-reaper ()
- (process-run-function "wol-reaper"
- (lambda () (reap-sessions) (kmrcl::process-sleep *reap-interval*))))
-
-(defun reap-sessions ()
- (cmsg-c :debug "Reaping"))
(defun make-new-session-id ()
(random-string :length 24 :set :lower-alphanumeric))
"Find or make websession for key"
(let ((sessions (sessions (session-master (entity-project ent)))))
;; if sessions doesn't exist, then project is not session enabled
- (when session
+ (when sessions
(cond
((null key)
(make-websession req ent method))
(sess (make-instance 'websession
:key key
:lastref (get-universal-time)
+ :lifetime (lifetime (session-master (entity-project ent)))
:method method))
(hash (sessions (session-master (entity-project ent)))))
(when hash
(getf (request-plist req) :session-id))
(setf (websession-from-req req)
(ensure-websession it req ent :uri))))
+
+
+
+;;; Reap expired sessions
+
+
+(defun start-reaper ()
+ (process-run-function "wol-reaper"
+ (lambda () (reap-sessions) (kmrcl::process-sleep *reap-interval*))))
+
+(defun reap-sessions ()
+ (cmsg-c :debug "Reaping")
+ (dolist (expired (find-expired-sessions))
+ (flush-expired expired)))
+
+(defun find-expired-sessions ()
+ (loop for s in (all-sessions)
+ when (is-session-expired (car s))
+ collect s))
+
+(defun all-sessions (&aux s)
+ (maphash
+ (lambda (name proj)
+ (declare (ignore name))
+ (let* ((sm (session-master proj))
+ (sessions (when sm (sessions sm))))
+ (when sessions
+ (maphash
+ (lambda (k v)
+ (declare (ignore k))
+ (push (cons v proj) s))
+ sessions))))
+ *active-projects*)
+ s)
+
+(defmethod flush-expired (s)
+ (let ((sessions (sessions (session-master (cdr s)))))
+ (remhash (car s) sessions)
+ (add-log-entry (cdr s) "flush expired session: key=~A" (websession-key (car s)))))
+
+(defun is-session-expired (ws)
+ (> (get-universal-time) (+ (websession-lastref ws)
+ (websession-lifetime ws))))