changeset 150:80e43dd34d71

Avoid opening the database connection when serving static resources.
author Daniele Nicolodi <daniele@grinta.net>
date Wed, 26 Oct 2011 20:00:09 +0200
parents d2d83ea3f369
children 46caabb1ac77
files src/ltpdarepo/__init__.py
diffstat 1 files changed, 11 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/src/ltpdarepo/__init__.py	Wed Oct 26 19:59:04 2011 +0200
+++ b/src/ltpdarepo/__init__.py	Wed Oct 26 20:00:09 2011 +0200
@@ -18,19 +18,23 @@
 
 @app.before_request
 def before_request():
+    # get version information from package
+    g.version = get_distribution('ltpdarepo').version
+
+    # optimization: do not open db connection for static resources
+    if request.endpoint == 'static':
+        return
+
     # open database connection
     g.db = mysql.connect(host=app.config['HOSTNAME'], db=app.config['DATABASE'],
                          user=app.config['USERNAME'], passwd=app.config['PASSWORD'],
                          charset='utf8')
 
-    # get version information from package
-    g.version = get_distribution('ltpdarepo').version
-
     # validate schema revision
     curs = g.db.cursor()
     curs.execute("SELECT CAST(value AS UNSIGNED) FROM options WHERE name='version'")
     g.schema = curs.fetchone()[0]
-    if g.schema != SCHEMA and '/static/' not in request.url:
+    if g.schema != SCHEMA:
         raise InternalServerError(
             '<p>The database needs to be upgraded.</p><p>'
             'Current database schema version: %s. '
@@ -40,7 +44,9 @@
 @app.teardown_request
 def teardown_request(exception):
     # close database connection
-    g.db.close()
+    db = getattr(g, 'db', None)
+    if db is not None:
+        db.close()
 
 
 # register error handlers