better timeout discipline in socketutils

This commit is contained in:
Mahmoud Hashemi 2016-04-26 02:09:11 -07:00
parent 5d722a9a77
commit c84c313e78
1 changed files with 14 additions and 5 deletions

View File

@ -90,7 +90,10 @@ class BufferedSocket(object):
if maxbytes is not None and len(recvd) >= maxbytes: if maxbytes is not None and len(recvd) >= maxbytes:
raise NotFound(marker, len(recvd)) raise NotFound(marker, len(recvd))
if timeout: if timeout:
sock.settimeout(timeout - (time.time() - start)) cur_timeout = timeout - (time.time() - start)
if cur_timeout <= 0.0:
raise socket.timeout()
sock.settimeout(cur_timeout)
nxt = sock.recv(maxbytes) nxt = sock.recv(maxbytes)
if not nxt: if not nxt:
raise ConnectionClosed( raise ConnectionClosed(
@ -126,7 +129,11 @@ class BufferedSocket(object):
if total_bytes >= size: if total_bytes >= size:
break break
chunks.append(nxt) chunks.append(nxt)
self.sock.settimeout(timeout - (time.time() - start)) if timeout:
cur_timeout = timeout - (time.time() - start)
if cur_timeout <= 0.0:
raise socket.timeout()
self.sock.settimeout(cur_timeout)
nxt = self.sock.recv(size - total_bytes) nxt = self.sock.recv(size - total_bytes)
else: else:
raise ConnectionClosed( raise ConnectionClosed(
@ -136,7 +143,7 @@ class BufferedSocket(object):
self.rbuf = b''.join(chunks) self.rbuf = b''.join(chunks)
raise Timeout( raise Timeout(
timeout, 'read {0} of {1} bytes'.format(total_bytes, size)) timeout, 'read {0} of {1} bytes'.format(total_bytes, size))
except Exception: # in case of error, retain data read so far in buffer except Exception: # in case of error, retain data in buffer
self.rbuf = b''.join(chunks) self.rbuf = b''.join(chunks)
raise raise
extra_bytes = total_bytes - size extra_bytes = total_bytes - size
@ -147,7 +154,6 @@ class BufferedSocket(object):
chunks.append(last) chunks.append(last)
return b''.join(chunks) return b''.join(chunks)
def send(self, data, flags=0, timeout=_UNSET): def send(self, data, flags=0, timeout=_UNSET):
if timeout is _UNSET: if timeout is _UNSET:
timeout = self.timeout timeout = self.timeout
@ -164,7 +170,10 @@ class BufferedSocket(object):
sent = self.sock.send(sbuf[0]) sent = self.sock.send(sbuf[0])
sbuf[0] = sbuf[0][sent:] sbuf[0] = sbuf[0][sent:]
if timeout: if timeout:
self.sock.settimeout(timeout - (time.time() - start)) cur_timeout = timeout - (time.time() - start)
if cur_timeout <= 0.0:
raise socket.timeout()
self.sock.settimeout(cur_timeout)
except socket.timeout: except socket.timeout:
raise Timeout( raise Timeout(
timeout, "{0} bytes unsent".format(len(sbuf[0]))) timeout, "{0} bytes unsent".format(len(sbuf[0])))