##// END OF EJS Templates
hooks: cleanup connection
super-admin -
r1110:98ceac59 python3
parent child Browse files
Show More
@@ -28,7 +28,7 b' import msgpack'
28 import dataclasses
28 import dataclasses
29 import pygit2
29 import pygit2
30
30
31 from http.client import HTTPConnection
31 import http.client
32
32
33
33
34 import mercurial.scmutil
34 import mercurial.scmutil
@@ -49,24 +49,33 b' class HooksHttpClient(object):'
49 def __init__(self, hooks_uri):
49 def __init__(self, hooks_uri):
50 self.hooks_uri = hooks_uri
50 self.hooks_uri = hooks_uri
51
51
52 def __repr__(self):
53 return f'{self.__class__}(hook_uri={self.hooks_uri}, proto={self.proto})'
54
52 def __call__(self, method, extras):
55 def __call__(self, method, extras):
53 connection = HTTPConnection(self.hooks_uri)
56 connection = http.client.HTTPConnection(self.hooks_uri)
54 # binary msgpack body
57 # binary msgpack body
55 headers, body = self._serialize(method, extras)
58 headers, body = self._serialize(method, extras)
56 try:
59 log.debug('Doing a new hooks call using HTTPConnection to %s', self.hooks_uri)
57 connection.request('POST', '/', body, headers)
60
58 except Exception as error:
59 log.error('Hooks calling Connection failed on %s, org error: %s', connection.__dict__, error)
60 raise
61 response = connection.getresponse()
62 try:
61 try:
63 return msgpack.load(response)
62 try:
64 except Exception:
63 connection.request('POST', '/', body, headers)
65 response_data = response.read()
64 except Exception as error:
66 log.exception('Failed to decode hook response json data. '
65 log.error('Hooks calling Connection failed on %s, org error: %s', connection.__dict__, error)
67 'response_code:%s, raw_data:%s',
66 raise
68 response.status, response_data)
67
69 raise
68 response = connection.getresponse()
69 try:
70 return msgpack.load(response)
71 except Exception:
72 response_data = response.read()
73 log.exception('Failed to decode hook response json data. '
74 'response_code:%s, raw_data:%s',
75 response.status, response_data)
76 raise
77 finally:
78 connection.close()
70
79
71 @classmethod
80 @classmethod
72 def _serialize(cls, hook_name, extras):
81 def _serialize(cls, hook_name, extras):
@@ -75,7 +84,8 b' class HooksHttpClient(object):'
75 'extras': extras
84 'extras': extras
76 }
85 }
77 headers = {
86 headers = {
78 'rc-hooks-protocol': cls.proto
87 "rc-hooks-protocol": cls.proto,
88 "Connection": "keep-alive"
79 }
89 }
80 return headers, msgpack.packb(data)
90 return headers, msgpack.packb(data)
81
91
General Comments 0
You need to be logged in to leave comments. Login now