comparison roundup/cgi/apache.py @ 4213:302bc481ceb1

Cache tracker instances.
author Stefan Seefeld <stefan@seefeld.name>
date Tue, 14 Jul 2009 14:00:47 +0000
parents e70643990e9c
children 92757447dcf0 86b6cea7a975
comparison
equal deleted inserted replaced
4212:51a098592b78 4213:302bc481ceb1
8 # with python 2.3.3, mod_python 3.1.3, roundup 0.7.0. 8 # with python 2.3.3, mod_python 3.1.3, roundup 0.7.0.
9 # 9 #
10 # This module operates with only one tracker 10 # This module operates with only one tracker
11 # and must be placed in the tracker directory. 11 # and must be placed in the tracker directory.
12 # 12 #
13 # History (most recent first):
14 # 11-jul-2004 [als] added 'TrackerLanguage' option;
15 # pass message translator to the tracker client instance
16 # 04-jul-2004 [als] tracker lookup moved from module global to request handler;
17 # use PythonOption TrackerHome (configured in apache)
18 # to open the tracker
19 # 06-may-2004 [als] use cgi.FieldStorage from Python library
20 # instead of mod_python FieldStorage
21 # 29-apr-2004 [als] created
22
23 __version__ = "$Revision: 1.6 $"[11:-2]
24 __date__ = "$Date: 2006-11-09 00:36:21 $"[7:-2]
25 13
26 import cgi 14 import cgi
27 import os 15 import os
16 import threading
28 17
29 from mod_python import apache 18 from mod_python import apache
30 19
31 import roundup.instance 20 import roundup.instance
32 from roundup.cgi import TranslationService 21 from roundup.cgi import TranslationService
81 def sendfile(self, filename, offset = 0, len = -1): 70 def sendfile(self, filename, offset = 0, len = -1):
82 """Send 'filename' to the user.""" 71 """Send 'filename' to the user."""
83 72
84 return self._req.sendfile(filename, offset, len) 73 return self._req.sendfile(filename, offset, len)
85 74
75 __tracker_cache = {}
76 """A cache of optimized tracker instances.
77
78 The keys are strings giving the directories containing the trackers.
79 The values are tracker instances."""
80
81 __tracker_cache_lock = threading.Lock()
82 """A lock used to guard access to the cache."""
83
86 84
87 def handler(req): 85 def handler(req):
88 """HTTP request handler""" 86 """HTTP request handler"""
89 _options = req.get_options() 87 _options = req.get_options()
90 _home = _options.get("TrackerHome") 88 _home = _options.get("TrackerHome")
92 _timing = _options.get("TrackerTiming", "no") 90 _timing = _options.get("TrackerTiming", "no")
93 if _timing.lower() in ("no", "false"): 91 if _timing.lower() in ("no", "false"):
94 _timing = "" 92 _timing = ""
95 _debug = _options.get("TrackerDebug", "no") 93 _debug = _options.get("TrackerDebug", "no")
96 _debug = _debug.lower() not in ("no", "false") 94 _debug = _debug.lower() not in ("no", "false")
97 if not (_home and os.path.isdir(_home)): 95
98 apache.log_error( 96 # We do not need to take a lock here (the fast path) because reads
99 "PythonOption TrackerHome missing or invalid for %(uri)s" 97 # from dictionaries are atomic.
100 % {'uri': req.uri}) 98 if not _debug and _home in __tracker_cache:
101 return apache.HTTP_INTERNAL_SERVER_ERROR 99 _tracker = __tracker_cache[_home]
102 _tracker = roundup.instance.open(_home, not _debug) 100 else:
101 if not (_home and os.path.isdir(_home)):
102 apache.log_error(
103 "PythonOption TrackerHome missing or invalid for %(uri)s"
104 % {'uri': req.uri})
105 return apache.HTTP_INTERNAL_SERVER_ERROR
106 if _debug:
107 _tracker = roundup.instance.open(_home, optimize=0)
108 else:
109 __tracker_cache_lock.acquire()
110 try:
111 # The tracker may have been added while we were acquiring
112 # the lock.
113 if _home in __tracker_cache:
114 _tracker = __tracker_cache[home]
115 else:
116 _tracker = roundup.instance.open(_home, optimize=1)
117 __tracker_cache[_home] = _tracker
118 finally:
119 __tracker_cache_lock.release()
103 # create environment 120 # create environment
104 # Note: cookies are read from HTTP variables, so we need all HTTP vars 121 # Note: cookies are read from HTTP variables, so we need all HTTP vars
105 req.add_common_vars() 122 req.add_common_vars()
106 _env = dict(req.subprocess_env) 123 _env = dict(req.subprocess_env)
107 # XXX classname must be the first item in PATH_INFO. roundup.cgi does: 124 # XXX classname must be the first item in PATH_INFO. roundup.cgi does:

Roundup Issue Tracker: http://roundup-tracker.org/