Skip to content

Commit

Permalink
Avoid keeping dangling connections when the client closes.
Browse files Browse the repository at this point in the history
  • Loading branch information
rblank committed Sep 19, 2024
1 parent a364ce2 commit 335fc0b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
27 changes: 18 additions & 9 deletions tdoc/common/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,28 +297,37 @@ def dispatch_star_handler(self, write_content):
url = parse.urlparse(self.path)
if not url.path.startswith('/*'): return
if handler := getattr(self, f'handle_star_{url.path[2:]}', None):
content = handler(url)
content = handler(url, write_content)
if write_content and content: self.wfile.write(content)
else:
self.send_error(server.HTTPStatus.NOT_FOUND)
return True

def handle_star_build(self, url):
def handle_star_build(self, url, write_content):
t = None
for k, v in parse.parse_qsl(url.query):
if k == 't':
t = v
break
with self.server.lock:
while ((mtime := self.server.build_mtime) is None
or t == build_tag(mtime)):
self.server.lock.wait()
tag = build_tag(mtime).encode('utf-8')
# We send padding back at regular intervals, which allows detecting when
# the client closes the connection (we get a BrokenPipeError). Since the
# content length is needed upfront, we return a fixed size, and
# terminate the request if the padding exceeds the available space.
size = 600
self.send_response(server.HTTPStatus.OK)
self.send_header('Content-type', 'text/plain')
self.send_header('Content-Length', str(len(tag)))
self.send_header('Content-Length', str(size))
self.end_headers()
return tag
if not write_content: return
with self.server.lock:
while ((mtime := self.server.build_mtime) is None
or t == build_tag(mtime)) and size > 0:
if self.server.lock.wait(timeout=1): continue
self.wfile.write(b' ')
size -= 1
tag = build_tag(mtime).encode('utf-8')
if len(tag) > size: tag = b'' # Not enough remaining capacity
return b' ' * (size - len(tag)) + tag


def build_tag(mtime):
Expand Down
3 changes: 2 additions & 1 deletion tdoc/common/static/tdoc-reload.js.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ async function reloadOnTagChange() {
`${resp.statusText}`);
continue;
}
const tag = await resp.text();
const tag = (await resp.text()).trim();
if (tag === '') continue;
if (tag !== build) location.reload();
} catch (e) {
console.error(e);
Expand Down

0 comments on commit 335fc0b

Please sign in to comment.