Skip to content
Snippets Groups Projects
Commit 679137dc authored by Nate Coraor's avatar Nate Coraor
Browse files

Support timeout handling in the standard (urllib-based) client

transport.
parent ec577686
No related branches found
No related tags found
No related merge requests found
...@@ -4,23 +4,27 @@ Pulsar HTTP Client layer based on Python Standard Library (urllib2) ...@@ -4,23 +4,27 @@ Pulsar HTTP Client layer based on Python Standard Library (urllib2)
from __future__ import with_statement from __future__ import with_statement
from os.path import getsize from os.path import getsize
import mmap import mmap
import socket
try: try:
from urllib2 import urlopen from urllib2 import urlopen, URLError
except ImportError: except ImportError:
from urllib.request import urlopen from urllib.request import urlopen
from urllib.error import URLError
try: try:
from urllib2 import Request from urllib2 import Request
except ImportError: except ImportError:
from urllib.request import Request from urllib.request import Request
from ..exceptions import PulsarClientTransportError
class Urllib2Transport(object): class Urllib2Transport(object):
def __init__(self, **kwrgs): def __init__(self, timeout=None, **kwrgs):
pass self.timeout = timeout
def _url_open(self, request, data): def _url_open(self, request, data):
return urlopen(request, data) return urlopen(request, data, self.timeout)
def execute(self, url, method=None, data=None, input_path=None, output_path=None): def execute(self, url, method=None, data=None, input_path=None, output_path=None):
request = self.__request(url, data, method) request = self.__request(url, data, method)
...@@ -34,7 +38,12 @@ class Urllib2Transport(object): ...@@ -34,7 +38,12 @@ class Urllib2Transport(object):
else: else:
data = b"" data = b""
request.add_header('Content-Length', str(size)) request.add_header('Content-Length', str(size))
response = self._url_open(request, data) try:
response = self._url_open(request, data)
except socket.timeout as exc:
raise PulsarClientTransportError(code=PulsarClientTransportError.TIMEOUT)
except URLError as exc:
raise PulsarClientTransportError(transport_message=exc.reason)
finally: finally:
if input: if input:
input.close() input.close()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment