diff --git a/vcsserver/hooks.py b/vcsserver/hooks.py --- a/vcsserver/hooks.py +++ b/vcsserver/hooks.py @@ -28,7 +28,7 @@ import msgpack import dataclasses import pygit2 -from http.client import HTTPConnection +import http.client import mercurial.scmutil @@ -49,24 +49,33 @@ class HooksHttpClient(object): def __init__(self, hooks_uri): self.hooks_uri = hooks_uri + def __repr__(self): + return f'{self.__class__}(hook_uri={self.hooks_uri}, proto={self.proto})' + def __call__(self, method, extras): - connection = HTTPConnection(self.hooks_uri) + connection = http.client.HTTPConnection(self.hooks_uri) # binary msgpack body headers, body = self._serialize(method, extras) - try: - connection.request('POST', '/', body, headers) - except Exception as error: - log.error('Hooks calling Connection failed on %s, org error: %s', connection.__dict__, error) - raise - response = connection.getresponse() + log.debug('Doing a new hooks call using HTTPConnection to %s', self.hooks_uri) + try: - return msgpack.load(response) - except Exception: - response_data = response.read() - log.exception('Failed to decode hook response json data. ' - 'response_code:%s, raw_data:%s', - response.status, response_data) - raise + try: + connection.request('POST', '/', body, headers) + except Exception as error: + log.error('Hooks calling Connection failed on %s, org error: %s', connection.__dict__, error) + raise + + response = connection.getresponse() + try: + return msgpack.load(response) + except Exception: + response_data = response.read() + log.exception('Failed to decode hook response json data. ' + 'response_code:%s, raw_data:%s', + response.status, response_data) + raise + finally: + connection.close() @classmethod def _serialize(cls, hook_name, extras): @@ -75,7 +84,8 @@ class HooksHttpClient(object): 'extras': extras } headers = { - 'rc-hooks-protocol': cls.proto + "rc-hooks-protocol": cls.proto, + "Connection": "keep-alive" } return headers, msgpack.packb(data)