|
26 | 26 | from urllib.parse import urlencode |
27 | 27 |
|
28 | 28 | try: |
29 | | - from httplib import HTTPConnection |
| 29 | + from httplib import HTTPConnection, BadStatusLine |
30 | 30 | except ImportError: |
31 | | - from http.client import HTTPConnection |
| 31 | + from http.client import HTTPConnection, BadStatusLine |
32 | 32 |
|
33 | 33 | import linstor.sharedconsts as apiconsts |
34 | 34 |
|
@@ -163,7 +163,7 @@ def _decode_response_data(cls, response): |
163 | 163 | return zlib.decompress(data, zlib.MAX_WBITS | 16) |
164 | 164 | return data |
165 | 165 |
|
166 | | - def _rest_request(self, apicall, method, path, body=None): |
| 166 | + def _rest_request(self, apicall, method, path, body=None, reconnect=True): |
167 | 167 | """ |
168 | 168 |
|
169 | 169 | :param str apicall: linstor apicall strid |
@@ -208,7 +208,17 @@ def _rest_request(self, apicall, method, path, body=None): |
208 | 208 | except socket.timeout: |
209 | 209 | raise LinstorTimeoutError("Socket timeout, no data received for more than {t}s.".format(t=self._timeout)) |
210 | 210 | except socket.error as err: |
211 | | - raise LinstorNetworkError("Error reading response from {hp}: {err}".format(hp=self._ctrl_host, err=err)) |
| 211 | + if self._keep_alive and reconnect: |
| 212 | + self.connect() |
| 213 | + return self._rest_request(apicall, method, path, body, reconnect=False) |
| 214 | + else: |
| 215 | + raise LinstorNetworkError("Error reading response from {hp}: {err}".format(hp=self._ctrl_host, err=err)) |
| 216 | + except BadStatusLine: # python2 raises BadStatusLine on connection closed |
| 217 | + if self._keep_alive and reconnect: |
| 218 | + self.connect() |
| 219 | + return self._rest_request(apicall, method, path, body, reconnect=False) |
| 220 | + else: |
| 221 | + raise |
212 | 222 |
|
213 | 223 | def __convert_rest_response(self, apicall, response, path): |
214 | 224 | resp_data = self._decode_response_data(response) |
|
0 commit comments