Skip to content

Commit 3de28e0

Browse files
committed
linstorapi: fix keep-alive mode
1 parent 3841714 commit 3de28e0

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

linstor/linstorapi.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
from urllib.parse import urlencode
2727

2828
try:
29-
from httplib import HTTPConnection
29+
from httplib import HTTPConnection, BadStatusLine
3030
except ImportError:
31-
from http.client import HTTPConnection
31+
from http.client import HTTPConnection, BadStatusLine
3232

3333
import linstor.sharedconsts as apiconsts
3434

@@ -163,7 +163,7 @@ def _decode_response_data(cls, response):
163163
return zlib.decompress(data, zlib.MAX_WBITS | 16)
164164
return data
165165

166-
def _rest_request(self, apicall, method, path, body=None):
166+
def _rest_request(self, apicall, method, path, body=None, reconnect=True):
167167
"""
168168
169169
:param str apicall: linstor apicall strid
@@ -208,7 +208,17 @@ def _rest_request(self, apicall, method, path, body=None):
208208
except socket.timeout:
209209
raise LinstorTimeoutError("Socket timeout, no data received for more than {t}s.".format(t=self._timeout))
210210
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
212222

213223
def __convert_rest_response(self, apicall, response, path):
214224
resp_data = self._decode_response_data(response)

0 commit comments

Comments
 (0)