Commit 8ad4ec2f authored by Łukasz Nowak's avatar Łukasz Nowak

Implement default timeout for slap communication.

Currently slap library was coded with optimistic assumption, that remote system
are not failing. But in order not to block library caller it is required to
setup timeout on socket operations, with default different than infinity.
parent 19a22a86
......@@ -413,13 +413,14 @@ class ConnectionHelper:
check given master-url argument, and make sure that IPv6 is enabled on \
your machine and that the server is available. The original error was:"
def __init__(self, connection_wrapper, host, path, key_file=None,
cert_file=None, master_ca_file=None):
cert_file=None, master_ca_file=None, timeout=None):
self.connection_wrapper = connection_wrapper
self.host = host
self.path = path
self.key_file = key_file
self.cert_file = cert_file
self.master_ca_file = master_ca_file
self.timeout = timeout
def getComputerInformation(self, computer_id):
self.GET('/getComputerInformation?computer_id=%s' % computer_id)
......@@ -438,58 +439,68 @@ your machine and that the server is available. The original error was:"
def GET(self, path):
try:
self.connect()
self.connection.request('GET', self.path + path)
self.response = self.connection.getresponse()
except socket.error, e:
raise socket.error(self.error_message_connect_fail + str(e))
# check self.response.status and raise exception early
if self.response.status == httplib.REQUEST_TIMEOUT:
# resource is not ready
raise ResourceNotReady(path)
elif self.response.status == httplib.NOT_FOUND:
raise NotFoundError(path)
elif self.response.status == httplib.FORBIDDEN:
raise Unauthorized(path)
elif self.response.status != httplib.OK:
message = 'Server responded with wrong code %s with %s' % \
(self.response.status, path)
raise ServerError(message)
default_timeout = socket.getdefaulttimeout()
socket.setdefaulttimeout(self.timeout)
try:
self.connect()
self.connection.request('GET', self.path + path)
self.response = self.connection.getresponse()
except socket.error, e:
raise socket.error(self.error_message_connect_fail + str(e))
# check self.response.status and raise exception early
if self.response.status == httplib.REQUEST_TIMEOUT:
# resource is not ready
raise ResourceNotReady(path)
elif self.response.status == httplib.NOT_FOUND:
raise NotFoundError(path)
elif self.response.status == httplib.FORBIDDEN:
raise Unauthorized(path)
elif self.response.status != httplib.OK:
message = 'Server responded with wrong code %s with %s' % \
(self.response.status, path)
raise ServerError(message)
finally:
socket.setdefaulttimeout(default_timeout)
def POST(self, path, parameter_dict,
content_type="application/x-www-form-urlencoded"):
try:
self.connect()
header_dict = {'Content-type': content_type}
self.connection.request("POST", self.path + path,
urllib.urlencode(parameter_dict), header_dict)
except socket.error, e:
raise socket.error(self.error_message_connect_fail + str(e))
self.response = self.connection.getresponse()
# check self.response.status and raise exception early
if self.response.status == httplib.REQUEST_TIMEOUT:
# resource is not ready
raise ResourceNotReady("%s - %s" % (path, parameter_dict))
elif self.response.status == httplib.NOT_FOUND:
raise NotFoundError("%s - %s" % (path, parameter_dict))
elif self.response.status == httplib.FORBIDDEN:
raise Unauthorized("%s - %s" % (path, parameter_dict))
elif self.response.status != httplib.OK:
message = 'Server responded with wrong code %s with %s' % \
(self.response.status, path)
raise ServerError(message)
default_timeout = socket.getdefaulttimeout()
socket.setdefaulttimeout(self.timeout)
try:
self.connect()
header_dict = {'Content-type': content_type}
self.connection.request("POST", self.path + path,
urllib.urlencode(parameter_dict), header_dict)
except socket.error, e:
raise socket.error(self.error_message_connect_fail + str(e))
self.response = self.connection.getresponse()
# check self.response.status and raise exception early
if self.response.status == httplib.REQUEST_TIMEOUT:
# resource is not ready
raise ResourceNotReady("%s - %s" % (path, parameter_dict))
elif self.response.status == httplib.NOT_FOUND:
raise NotFoundError("%s - %s" % (path, parameter_dict))
elif self.response.status == httplib.FORBIDDEN:
raise Unauthorized("%s - %s" % (path, parameter_dict))
elif self.response.status != httplib.OK:
message = 'Server responded with wrong code %s with %s' % \
(self.response.status, path)
raise ServerError(message)
finally:
socket.setdefaulttimeout(default_timeout)
class slap:
zope.interface.implements(interface.slap)
def initializeConnection(self, slapgrid_uri, key_file=None, cert_file=None,
master_ca_file=None):
master_ca_file=None, timeout=60):
self._initialiseConnectionHelper(slapgrid_uri, key_file, cert_file,
master_ca_file)
master_ca_file, timeout)
def _initialiseConnectionHelper(self, slapgrid_uri, key_file, cert_file,
master_ca_file):
master_ca_file, timeout):
SlapDocument._slapgrid_uri = slapgrid_uri
scheme, netloc, path, query, fragment = urlparse.urlsplit(
SlapDocument._slapgrid_uri)
......@@ -509,7 +520,7 @@ class slap:
connection_wrapper = httplib.HTTPSConnection
slap._connection_helper = \
SlapDocument._connection_helper = ConnectionHelper(connection_wrapper,
netloc, path, key_file, cert_file, master_ca_file)
netloc, path, key_file, cert_file, master_ca_file, timeout)
def _register(self, klass, *registration_argument_list):
if len(registration_argument_list) == 1 and type(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment