##// END OF EJS Templates
request-wrapper: add gc.collect to every 25th request to clear up memory....
marcink -
r757:4b34b23b default
parent child Browse files
Show More
@@ -1,65 +1,69 b''
1 # RhodeCode VCSServer provides access to different vcs backends via network.
1 # RhodeCode VCSServer provides access to different vcs backends via network.
2 # Copyright (C) 2014-2019 RhodeCode GmbH
2 # Copyright (C) 2014-2019 RhodeCode GmbH
3 #
3 #
4 # This program is free software; you can redistribute it and/or modify
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
7 # (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software Foundation,
15 # along with this program; if not, write to the Free Software Foundation,
16 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
18
18 import gc
19
20 import time
19 import time
21 import logging
20 import logging
22
21
23
22
24 from vcsserver.utils import safe_str
23 from vcsserver.utils import safe_str
25
24
26
25
27 log = logging.getLogger(__name__)
26 log = logging.getLogger(__name__)
28
27
29
28
30 def get_access_path(request):
29 def get_access_path(request):
31 environ = request.environ
30 environ = request.environ
32 return environ.get('PATH_INFO')
31 return environ.get('PATH_INFO')
33
32
34
33
35 def get_user_agent(environ):
34 def get_user_agent(environ):
36 return environ.get('HTTP_USER_AGENT')
35 return environ.get('HTTP_USER_AGENT')
37
36
38
37
39 class RequestWrapperTween(object):
38 class RequestWrapperTween(object):
40 def __init__(self, handler, registry):
39 def __init__(self, handler, registry):
41 self.handler = handler
40 self.handler = handler
42 self.registry = registry
41 self.registry = registry
42 self.gc_max_requests = 25
43
43
44 # one-time configuration code goes here
44 # one-time configuration code goes here
45
45
46 def __call__(self, request):
46 def __call__(self, request):
47 start = time.time()
47 start = time.time()
48 try:
48 try:
49 response = self.handler(request)
49 response = self.handler(request)
50 finally:
50 finally:
51 end = time.time()
51 end = time.time()
52 total = end - start
52 total = end - start
53 count = request.request_count()
53 count = request.request_count()
54 log.info(
54 log.info(
55 'Req[%4s] IP: %s %s Request to %s time: %.4fs [%s]',
55 'Req[%4s] IP: %s %s Request to %s time: %.4fs [%s]',
56 count, '127.0.0.1', request.environ.get('REQUEST_METHOD'),
56 count, '127.0.0.1', request.environ.get('REQUEST_METHOD'),
57 safe_str(get_access_path(request)), total, get_user_agent(request.environ))
57 safe_str(get_access_path(request)), total, get_user_agent(request.environ))
58
58
59 if self.gc_max_requests and count % self.gc_max_requests == 0:
60 log.info('Performing gc.collect now')
61 gc.collect()
62
59 return response
63 return response
60
64
61
65
62 def includeme(config):
66 def includeme(config):
63 config.add_tween(
67 config.add_tween(
64 'vcsserver.tweens.request_wrapper.RequestWrapperTween',
68 'vcsserver.tweens.request_wrapper.RequestWrapperTween',
65 )
69 )
General Comments 0
You need to be logged in to leave comments. Login now