Changeset - b42ee1bdf082
[Not reviewed]
default
0 1 0
Mads Kiilerich - 6 years ago 2019-10-19 23:07:12
mads@kiilerich.com
Grafted from: 990973f1a798
wsgi: make WSGI wrapper follow the result and log when it actually has finished

The wrapper lost control after "%s responding after %.3fs" when it passed the
iterator through.

Now, wrap the iterator and keep track of its use and when it is closed and make
an additional loging of "%s responded after %.3fs".
1 file changed with 23 insertions and 2 deletions:
0 comments (0 inline, 0 general)
kallithea/lib/middleware/wrapper.py
Show inline comments
 
@@ -6,25 +6,26 @@
 
#
 
# This program is distributed in the hope that it will be useful,
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
# GNU General Public License for more details.
 
#
 
# You should have received a copy of the GNU General Public License
 
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
"""
 
kallithea.lib.middleware.wrapper
 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 

	
 
Wrap app to measure request and response time ... until the response starts.
 
Wrap app to measure request and response time ... all the way to the response
 
WSGI iterator has been closed.
 

	
 
This file was forked by the Kallithea project in July 2014.
 
Original author and date, and relevant copyright and licensing information is below:
 
:created_on: May 23, 2013
 
:author: marcink
 
:copyright: (c) 2013 RhodeCode GmbH, and others.
 
:license: GPLv3, see LICENSE.md for more details.
 
"""
 

	
 
import logging
 
import time
 

	
 
@@ -35,29 +36,49 @@ from kallithea.lib.utils2 import safe_un
 
log = logging.getLogger(__name__)
 

	
 

	
 
class Meter:
 

	
 
    def __init__(self):
 
        self._start = time.time()
 

	
 
    def duration(self):
 
        return time.time() - self._start
 

	
 

	
 
class ResultIter:
 

	
 
    def __init__(self, result, meter, description):
 
        self._result_close = getattr(result, 'close', None) or (lambda: None)
 
        self._next = iter(result).next
 
        self._meter = meter
 
        self._description = description
 

	
 
    def __iter__(self):
 
        return self
 

	
 
    def next(self):
 
        chunk = self._next()
 
        return chunk
 

	
 
    def close(self):
 
        self._result_close()
 
        log.info("%s responded after %.3fs", self._description, self._meter.duration())
 

	
 

	
 
class RequestWrapper(object):
 

	
 
    def __init__(self, app, config):
 
        self.application = app
 
        self.config = config
 

	
 
    def __call__(self, environ, start_response):
 
        meter = Meter()
 
        description = "Request from %s for %s" % (
 
            _get_ip_addr(environ),
 
            safe_unicode(_get_access_path(environ)),
 
        )
 
        try:
 
            result = self.application(environ, start_response)
 
        finally:
 
            log.info("%s responding after %.3fs", description, meter.duration())
 
        return result
 
        return ResultIter(result, meter, description)
0 comments (0 inline, 0 general)