Skip to content

Commit

Permalink
Reconnect watcher if server ends empty response
Browse files Browse the repository at this point in the history
If the server sends an empty response (e.g. a server-side timeout was
exceeded), the watcher should reconnect unless the user has specified a
timeout. This is similar to the behavior in the standard Python
Kubernetes library.
  • Loading branch information
JacobHenner committed Mar 26, 2020
1 parent 05f4251 commit eed4040
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions kubernetes_asyncio/watch/watch.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,20 +145,21 @@ async def next(self):
line = await self.resp.content.readline()
except asyncio.TimeoutError:
if 'timeout_seconds' not in self.func.keywords:
self.resp.close()
self.resp = None
if self.resource_version:
self.func.keywords['resource_version'] = self.resource_version
self._handle_reconnection()
continue
else:
raise

line = line.decode('utf8')

# Stop the iterator if K8s sends an empty response. This happens when
# eg the supplied timeout has expired.
# Reconnect if k8s sends an empty response. This usually happens
# when the server-side timeout has expired.
if line == '':
raise StopAsyncIteration
if 'timeout_seconds' not in self.func.keywords:
self._handle_reconnection()
continue
else:
raise StopAsyncIteration

return self.unmarshal_event(line, self.return_type)

Expand Down Expand Up @@ -204,8 +205,13 @@ async def __aenter__(self):

async def __aexit__(self, exc_type, exc_value, traceback):
self.close()

def close(self):
if self.resp is not None:
self.resp.release()
self.resp = None

def _handle_reconnection(self):
self.resp.close()
self.resp = None
self.func.keywords['resource_version'] = self.resource_version

0 comments on commit eed4040

Please sign in to comment.