##// END OF EJS Templates
implemented cache for repeated queries in simplehg mercurial requests
marcink -
r343:64849630 default
parent child Browse files
Show More
@@ -44,11 +44,13 b' cache_dir = %(here)s/data'
44 44 ####################################
45 45 beaker.cache.data_dir=/%(here)s/data/cache/data
46 46 beaker.cache.lock_dir=/%(here)s/data/cache/lock
47 beaker.cache.regions=short_term,long_term
47 beaker.cache.regions=super_short_term,short_term,long_term
48 48 beaker.cache.long_term.type=memory
49 49 beaker.cache.long_term.expire=36000
50 50 beaker.cache.short_term.type=memory
51 51 beaker.cache.short_term.expire=60
52 beaker.cache.super_short_term.type=memory
53 beaker.cache.super_short_term.expire=10
52 54
53 55 ################################################################################
54 56 ## WARNING: *THE LINE BELOW MUST BE UNCOMMENTED ON A PRODUCTION ENVIRONMENT* ##
@@ -39,17 +39,18 b' static_files = false'
39 39 lang=en
40 40 cache_dir = %(here)s/data
41 41
42
43 42 ####################################
44 43 ### BEAKER CACHE ####
45 44 ####################################
46 45 beaker.cache.data_dir=/%(here)s/data/cache/data
47 46 beaker.cache.lock_dir=/%(here)s/data/cache/lock
48 beaker.cache.regions=short_term,long_term
47 beaker.cache.regions=super_short_term,short_term,long_term
49 48 beaker.cache.long_term.type=memory
50 49 beaker.cache.long_term.expire=36000
51 50 beaker.cache.short_term.type=memory
52 51 beaker.cache.short_term.expire=60
52 beaker.cache.super_short_term.type=memory
53 beaker.cache.super_short_term.expire=10
53 54
54 55 ################################################################################
55 56 ## WARNING: *THE LINE BELOW MUST BE UNCOMMENTED ON A PRODUCTION ENVIRONMENT* ##
@@ -22,18 +22,18 b' Created on April 4, 2010'
22 22
23 23 @author: marcink
24 24 """
25
25 from beaker.cache import cache_region
26 26 from functools import wraps
27 from pylons import session, url, request
27 from pylons import config, session, url, request
28 28 from pylons.controllers.util import abort, redirect
29 from pylons_app.lib.utils import get_repo_slug
29 30 from pylons_app.model import meta
30 31 from pylons_app.model.db import User, Repo2Perm, Repository, Permission
31 from pylons_app.lib.utils import get_repo_slug
32 32 from sqlalchemy.exc import OperationalError
33 33 from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
34 34 import crypt
35 35 import logging
36 from pylons import config
36
37 37 log = logging.getLogger(__name__)
38 38
39 39 def get_crypt_password(password):
@@ -43,11 +43,17 b' def get_crypt_password(password):'
43 43 """
44 44 return crypt.crypt(password, '6a')
45 45
46
47 @cache_region('super_short_term', 'cached_user')
48 def get_user_cached(username):
49 sa = meta.Session
50 user = sa.query(User).filter(User.username == username).one()
51 return user
52
46 53 def authfunc(environ, username, password):
47 sa = meta.Session
48 54 password_crypt = get_crypt_password(password)
49 55 try:
50 user = sa.query(User).filter(User.username == username).one()
56 user = get_user_cached(username)
51 57 except (NoResultFound, MultipleResultsFound, OperationalError) as e:
52 58 log.error(e)
53 59 user = None
@@ -2,7 +2,7 b''
2 2 # encoding: utf-8
3 3 # middleware to handle mercurial api calls
4 4 # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com>
5
5
6 6 # This program is free software; you can redistribute it and/or
7 7 # modify it under the terms of the GNU General Public License
8 8 # as published by the Free Software Foundation; version 2
@@ -27,21 +27,23 b" It's implemented with basic auth functio"
27 27 """
28 28 from datetime import datetime
29 29 from itertools import chain
30 from mercurial.error import RepoError
30 31 from mercurial.hgweb import hgweb
31 32 from mercurial.hgweb.request import wsgiapplication
32 from mercurial.error import RepoError
33 33 from paste.auth.basic import AuthBasicAuthenticator
34 34 from paste.httpheaders import REMOTE_USER, AUTH_TYPE
35 from pylons_app.lib.auth import authfunc, HasPermissionAnyMiddleware
35 from pylons_app.lib.auth import authfunc, HasPermissionAnyMiddleware, \
36 get_user_cached
36 37 from pylons_app.lib.utils import is_mercurial, make_ui, invalidate_cache, \
37 38 check_repo_fast
38 39 from pylons_app.model import meta
39 40 from pylons_app.model.db import UserLog, User
40 import pylons_app.lib.helpers as h
41 41 from webob.exc import HTTPNotFound, HTTPForbidden, HTTPInternalServerError
42 42 import logging
43 43 import os
44 import pylons_app.lib.helpers as h
44 45 import traceback
46
45 47 log = logging.getLogger(__name__)
46 48
47 49 class SimpleHg(object):
@@ -56,86 +58,84 b' class SimpleHg(object):'
56 58 def __call__(self, environ, start_response):
57 59 if not is_mercurial(environ):
58 60 return self.application(environ, start_response)
59 else:
60 #===================================================================
61 # AUTHENTICATE THIS MERCURIAL REQUEST
62 #===================================================================
63 username = REMOTE_USER(environ)
64 if not username:
65 result = self.authenticate(environ)
66 if isinstance(result, str):
67 AUTH_TYPE.update(environ, 'basic')
68 REMOTE_USER.update(environ, result)
69 else:
70 return result.wsgi_application(environ, start_response)
71
61
62 #===================================================================
63 # AUTHENTICATE THIS MERCURIAL REQUEST
64 #===================================================================
65 username = REMOTE_USER(environ)
66 if not username:
67 result = self.authenticate(environ)
68 if isinstance(result, str):
69 AUTH_TYPE.update(environ, 'basic')
70 REMOTE_USER.update(environ, result)
71 else:
72 return result.wsgi_application(environ, start_response)
73
74 try:
75 repo_name = '/'.join(environ['PATH_INFO'].split('/')[1:])
76 except:
77 log.error(traceback.format_exc())
78 return HTTPInternalServerError()(environ, start_response)
79
80 #===================================================================
81 # CHECK PERMISSIONS FOR THIS REQUEST
82 #===================================================================
83 action = self.__get_action(environ)
84 if action:
85 username = self.__get_environ_user(environ)
72 86 try:
73 repo_name = '/'.join(environ['PATH_INFO'].split('/')[1:])
87 user = self.__get_user(username)
74 88 except:
75 89 log.error(traceback.format_exc())
76 90 return HTTPInternalServerError()(environ, start_response)
91 #check permissions for this repository
92 if action == 'pull':
93 if not HasPermissionAnyMiddleware('repository.read',
94 'repository.write',
95 'repository.admin')\
96 (user, repo_name):
97 return HTTPForbidden()(environ, start_response)
98 if action == 'push':
99 if not HasPermissionAnyMiddleware('repository.write',
100 'repository.admin')\
101 (user, repo_name):
102 return HTTPForbidden()(environ, start_response)
77 103
78 #===================================================================
79 # CHECK PERMISSIONS FOR THIS REQUEST
80 #===================================================================
81 action = self.__get_action(environ)
82 if action:
83 username = self.__get_environ_user(environ)
84 try:
85 sa = meta.Session
86 user = sa.query(User)\
87 .filter(User.username == username).one()
88 except:
89 log.error(traceback.format_exc())
90 return HTTPInternalServerError()(environ, start_response)
91 #check permissions for this repository
92 if action == 'pull':
93 if not HasPermissionAnyMiddleware('repository.read',
94 'repository.write',
95 'repository.admin')\
96 (user, repo_name):
97 return HTTPForbidden()(environ, start_response)
98 if action == 'push':
99 if not HasPermissionAnyMiddleware('repository.write',
100 'repository.admin')\
101 (user, repo_name):
102 return HTTPForbidden()(environ, start_response)
103
104 #log action
105 proxy_key = 'HTTP_X_REAL_IP'
106 def_key = 'REMOTE_ADDR'
107 ipaddr = environ.get(proxy_key, environ.get(def_key, '0.0.0.0'))
108 self.__log_user_action(user, action, repo_name, ipaddr)
109
110 #===================================================================
111 # MERCURIAL REQUEST HANDLING
112 #===================================================================
113 environ['PATH_INFO'] = '/'#since we wrap into hgweb, reset the path
114 self.baseui = make_ui('db')
115 self.basepath = self.config['base_path']
116 self.repo_path = os.path.join(self.basepath, repo_name)
104 #log action
105 proxy_key = 'HTTP_X_REAL_IP'
106 def_key = 'REMOTE_ADDR'
107 ipaddr = environ.get(proxy_key, environ.get(def_key, '0.0.0.0'))
108 self.__log_user_action(user, action, repo_name, ipaddr)
109
110 #===================================================================
111 # MERCURIAL REQUEST HANDLING
112 #===================================================================
113 environ['PATH_INFO'] = '/'#since we wrap into hgweb, reset the path
114 self.baseui = make_ui('db')
115 self.basepath = self.config['base_path']
116 self.repo_path = os.path.join(self.basepath, repo_name)
117 117
118 #quick check if that dir exists...
119 if check_repo_fast(repo_name, self.basepath):
118 #quick check if that dir exists...
119 if check_repo_fast(repo_name, self.basepath):
120 return HTTPNotFound()(environ, start_response)
121 try:
122 app = wsgiapplication(self.__make_app)
123 except RepoError as e:
124 if str(e).find('not found') != -1:
120 125 return HTTPNotFound()(environ, start_response)
121 try:
122 app = wsgiapplication(self.__make_app)
123 except RepoError as e:
124 if str(e).find('not found') != -1:
125 return HTTPNotFound()(environ, start_response)
126 except Exception:
127 log.error(traceback.format_exc())
128 return HTTPInternalServerError()(environ, start_response)
129
130 #invalidate cache on push
131 if action == 'push':
132 self.__invalidate_cache(repo_name)
133 messages = []
134 messages.append('thank you for using hg-app')
135
136 return self.msg_wrapper(app, environ, start_response, messages)
137 else:
138 return app(environ, start_response)
126 except Exception:
127 log.error(traceback.format_exc())
128 return HTTPInternalServerError()(environ, start_response)
129
130 #invalidate cache on push
131 if action == 'push':
132 self.__invalidate_cache(repo_name)
133 messages = []
134 messages.append('thank you for using hg-app')
135
136 return self.msg_wrapper(app, environ, start_response, messages)
137 else:
138 return app(environ, start_response)
139 139
140 140
141 141 def msg_wrapper(self, app, environ, start_response, messages=[]):
@@ -160,6 +160,11 b' class SimpleHg(object):'
160 160 def __get_environ_user(self, environ):
161 161 return environ.get('REMOTE_USER')
162 162
163 def __get_user(self, username):
164 return get_user_cached(username)
165
166
167
163 168 def __get_size(self, repo_path, content_size):
164 169 size = int(content_size)
165 170 for path, dirs, files in os.walk(repo_path):
@@ -16,6 +16,7 b''
16 16 # along with this program; if not, write to the Free Software
17 17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 18 # MA 02110-1301, USA.
19 from beaker.cache import cache_region
19 20
20 21 """
21 22 Created on April 18, 2010
@@ -75,6 +76,13 b' def check_repo(repo_name, base_path, ver'
75 76 log.info('%s repo is free for creation', repo_name)
76 77 return True
77 78
79
80 @cache_region('super_short_term', 'cached_hg_ui')
81 def get_hg_ui_cached():
82 from pylons_app.model.meta import Session
83 sa = Session()
84 return sa.query(HgAppUi).all()
85
78 86 def make_ui(read_from='file', path=None, checkpaths=True):
79 87 """
80 88 A function that will read python rc files or database
@@ -112,10 +120,7 b" def make_ui(read_from='file', path=None,"
112 120
113 121
114 122 elif read_from == 'db':
115 from pylons_app.model.meta import Session
116 sa = Session()
117
118 hg_ui = sa.query(HgAppUi).all()
123 hg_ui = get_hg_ui_cached()
119 124 for ui_ in hg_ui:
120 125 baseui.setconfig(ui_.ui_section, ui_.ui_key, ui_.ui_value)
121 126
General Comments 0
You need to be logged in to leave comments. Login now