Skip to content

Commit d8bbe46

Browse files
committed
Cache tracker instances.
1 parent 407fcc6 commit d8bbe46

File tree

1 file changed

+35
-18
lines changed

1 file changed

+35
-18
lines changed

roundup/cgi/apache.py

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,10 @@
1010
# This module operates with only one tracker
1111
# and must be placed in the tracker directory.
1212
#
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]
2513

2614
import cgi
2715
import os
16+
import threading
2817

2918
from mod_python import apache
3019

@@ -83,6 +72,15 @@ def sendfile(self, filename, offset = 0, len = -1):
8372

8473
return self._req.sendfile(filename, offset, len)
8574

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+
8684

8785
def handler(req):
8886
"""HTTP request handler"""
@@ -94,12 +92,31 @@ def handler(req):
9492
_timing = ""
9593
_debug = _options.get("TrackerDebug", "no")
9694
_debug = _debug.lower() not in ("no", "false")
97-
if not (_home and os.path.isdir(_home)):
98-
apache.log_error(
99-
"PythonOption TrackerHome missing or invalid for %(uri)s"
100-
% {'uri': req.uri})
101-
return apache.HTTP_INTERNAL_SERVER_ERROR
102-
_tracker = roundup.instance.open(_home, not _debug)
95+
96+
# We do not need to take a lock here (the fast path) because reads
97+
# from dictionaries are atomic.
98+
if not _debug and _home in __tracker_cache:
99+
_tracker = __tracker_cache[_home]
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()
103120
# create environment
104121
# Note: cookies are read from HTTP variables, so we need all HTTP vars
105122
req.add_common_vars()

0 commit comments

Comments
 (0)