Skip to content

Commit 72a8079

Browse files
committed
add a keep-alive ping/heartbeat to the sockets we host, as well as the sockets we connect to
1 parent a42320b commit 72a8079

2 files changed

Lines changed: 9 additions & 5 deletions

File tree

cabotage/server/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,6 @@ class Config(metaclass=MetaFlaskEnv):
8181
GITHUB_APP_PRIVATE_KEY = None
8282
GITHUB_WEBHOOK_SECRET = None
8383
SHELLZ_ENABLED = False
84+
SOCK_SERVER_OPTIONS = {"ping_interval": 25}
8485
SIDECAR_IMAGE = "cabotage/sidecar:3"
8586
DATADOG_IMAGE = "datadog/agent:7.55.2"

cabotage/server/user/views.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -523,21 +523,24 @@ def project_application_shell_socket(ws, org_slug, project_slug, app_slug):
523523
_preload_content=False,
524524
)
525525

526+
last_ping = time.monotonic()
526527
while resp.is_open():
527528
resp.update()
529+
now = time.monotonic()
530+
if now - last_ping >= 25:
531+
try:
532+
resp.sock.ping()
533+
except Exception:
534+
break
535+
last_ping = now
528536
if data := ws.receive(timeout=0.01):
529-
print((data,))
530537
if data[0] == "\x00":
531538
resp.write_stdin(data[1:])
532539
elif data[0] == "\x01":
533540
resp.write_channel(kubernetes.stream.ws_client.RESIZE_CHANNEL, data[1:])
534-
else:
535-
print((data[0],))
536541
if data := resp.read_stdout(timeout=0.01):
537-
print((data,))
538542
ws.send("\x00" + data)
539543
if data := resp.read_stderr(timeout=0.01):
540-
print((data,))
541544
ws.send("\x00" + data)
542545

543546
resp.close()

0 commit comments

Comments
 (0)