Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reconnect watcher if server ends empty response #96

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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