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: ...@@ -413,13 +413,14 @@ class ConnectionHelper:
check given master-url argument, and make sure that IPv6 is enabled on \ 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:" your machine and that the server is available. The original error was:"
def __init__(self, connection_wrapper, host, path, key_file=None, 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.connection_wrapper = connection_wrapper
self.host = host self.host = host
self.path = path self.path = path
self.key_file = key_file self.key_file = key_file
self.cert_file = cert_file self.cert_file = cert_file
self.master_ca_file = master_ca_file self.master_ca_file = master_ca_file
self.timeout = timeout
def getComputerInformation(self, computer_id): def getComputerInformation(self, computer_id):
self.GET('/getComputerInformation?computer_id=%s' % 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:" ...@@ -438,58 +439,68 @@ your machine and that the server is available. The original error was:"
def GET(self, path): def GET(self, path):
try: try:
self.connect() default_timeout = socket.getdefaulttimeout()
self.connection.request('GET', self.path + path) socket.setdefaulttimeout(self.timeout)
self.response = self.connection.getresponse() try:
except socket.error, e: self.connect()
raise socket.error(self.error_message_connect_fail + str(e)) self.connection.request('GET', self.path + path)
# check self.response.status and raise exception early self.response = self.connection.getresponse()
if self.response.status == httplib.REQUEST_TIMEOUT: except socket.error, e:
# resource is not ready raise socket.error(self.error_message_connect_fail + str(e))
raise ResourceNotReady(path) # check self.response.status and raise exception early
elif self.response.status == httplib.NOT_FOUND: if self.response.status == httplib.REQUEST_TIMEOUT:
raise NotFoundError(path) # resource is not ready
elif self.response.status == httplib.FORBIDDEN: raise ResourceNotReady(path)
raise Unauthorized(path) elif self.response.status == httplib.NOT_FOUND:
elif self.response.status != httplib.OK: raise NotFoundError(path)
message = 'Server responded with wrong code %s with %s' % \ elif self.response.status == httplib.FORBIDDEN:
(self.response.status, path) raise Unauthorized(path)
raise ServerError(message) 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, def POST(self, path, parameter_dict,
content_type="application/x-www-form-urlencoded"): content_type="application/x-www-form-urlencoded"):
try: try:
self.connect() default_timeout = socket.getdefaulttimeout()
header_dict = {'Content-type': content_type} socket.setdefaulttimeout(self.timeout)
self.connection.request("POST", self.path + path, try:
urllib.urlencode(parameter_dict), header_dict) self.connect()
except socket.error, e: header_dict = {'Content-type': content_type}
raise socket.error(self.error_message_connect_fail + str(e)) self.connection.request("POST", self.path + path,
self.response = self.connection.getresponse() urllib.urlencode(parameter_dict), header_dict)
# check self.response.status and raise exception early except socket.error, e:
if self.response.status == httplib.REQUEST_TIMEOUT: raise socket.error(self.error_message_connect_fail + str(e))
# resource is not ready self.response = self.connection.getresponse()
raise ResourceNotReady("%s - %s" % (path, parameter_dict)) # check self.response.status and raise exception early
elif self.response.status == httplib.NOT_FOUND: if self.response.status == httplib.REQUEST_TIMEOUT:
raise NotFoundError("%s - %s" % (path, parameter_dict)) # resource is not ready
elif self.response.status == httplib.FORBIDDEN: raise ResourceNotReady("%s - %s" % (path, parameter_dict))
raise Unauthorized("%s - %s" % (path, parameter_dict)) elif self.response.status == httplib.NOT_FOUND:
elif self.response.status != httplib.OK: raise NotFoundError("%s - %s" % (path, parameter_dict))
message = 'Server responded with wrong code %s with %s' % \ elif self.response.status == httplib.FORBIDDEN:
(self.response.status, path) raise Unauthorized("%s - %s" % (path, parameter_dict))
raise ServerError(message) 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: class slap:
zope.interface.implements(interface.slap) zope.interface.implements(interface.slap)
def initializeConnection(self, slapgrid_uri, key_file=None, cert_file=None, 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, self._initialiseConnectionHelper(slapgrid_uri, key_file, cert_file,
master_ca_file) master_ca_file, timeout)
def _initialiseConnectionHelper(self, slapgrid_uri, key_file, cert_file, def _initialiseConnectionHelper(self, slapgrid_uri, key_file, cert_file,
master_ca_file): master_ca_file, timeout):
SlapDocument._slapgrid_uri = slapgrid_uri SlapDocument._slapgrid_uri = slapgrid_uri
scheme, netloc, path, query, fragment = urlparse.urlsplit( scheme, netloc, path, query, fragment = urlparse.urlsplit(
SlapDocument._slapgrid_uri) SlapDocument._slapgrid_uri)
...@@ -509,7 +520,7 @@ class slap: ...@@ -509,7 +520,7 @@ class slap:
connection_wrapper = httplib.HTTPSConnection connection_wrapper = httplib.HTTPSConnection
slap._connection_helper = \ slap._connection_helper = \
SlapDocument._connection_helper = ConnectionHelper(connection_wrapper, 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): def _register(self, klass, *registration_argument_list):
if len(registration_argument_list) == 1 and type( 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