Files
ykxmz/_build/commit_watchdog_grace.py
T
xmz7891 2b204cae07 修看门狗重启死循环(我上一版改过头了)
用户报 502 Bad Gateway,Cloudflare 的图显示
「Host: relay.xxmxxs.top - Error」,也就是源站/隧道这一跳挂了。

查 watchdog 日志,发现它在**每 50 秒重启一次隧道**:
    [10:31:57] 只剩 1 条连接 -> 重启隧道
    [10:32:22] 重启完
    [10:32:24] 又只剩 1 条 -> 距上次才 27 秒,先不重启
    [10:32:47] 距上次 50 秒 -> 又重启
    ... 循环不停

根因(我上一版的错):
  隧道在**质量差的 4G 上重建 4 条连接要 30~60 秒**。
  而我上一版把 MIN_RESTART_GAP 设成 45 秒,并且
  "公网不通 + 连接数<=1"就立刻重启 ——
  结果每次隧道还没把连接建齐,就被下一次重启打断,
  **永远停在"只剩 1 条"的状态**。
  等于我自己制造了不稳定。

修法:
  1. 重启后给 GRACE=150 秒宽限期,这期间绝不重启,
     让 cloudflared 专心把连接建齐
  2. MIN_RESTART_GAP 从 45 提到 150(和宽限期一致)
  3. "立刻重启"的判定加一条:距上次重启必须超过宽限期

实测(停掉 watchdog -> 手动重启隧道 -> 观察):
    10 秒内建齐 4 条连接,公网立刻 200
    宽限期生效后公网 20/20 = 100%,延迟 ~690ms

顺带一个教训:之前测出 80%、20% 的 000,
是我自己后台还跑着一个观察任务在并发打接口造成的干扰。
**测稳定性的时候,测量工具本身不能成为干扰源。**
2026-10-07 10:37:26 +08:00

103 lines
3.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
r'''把 watchdog 的宽限期修复同步到仓库并提交。'''
from __future__ import annotations
import os
import subprocess
import sys
from pathlib import Path
ROOT = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote")
GIT = r"C:\Program Files\Git\bin\git.exe"
WATCHDOG = ROOT / "relay" / "watchdog.sh"
sys.path.insert(0, str(ROOT / "_build"))
from devssh import run # noqa: E402
MSG = """修看门狗重启死循环(我上一版改过头了)
用户报 502 Bad Gateway,Cloudflare 的图显示
「Host: relay.xxmxxs.top - Error」,也就是源站/隧道这一跳挂了。
查 watchdog 日志,发现它在**每 50 秒重启一次隧道**:
[10:31:57] 只剩 1 条连接 -> 重启隧道
[10:32:22] 重启完
[10:32:24] 又只剩 1 条 -> 距上次才 27 秒,先不重启
[10:32:47] 距上次 50 秒 -> 又重启
... 循环不停
根因(我上一版的错):
隧道在**质量差的 4G 上重建 4 条连接要 30~60 秒**。
而我上一版把 MIN_RESTART_GAP 设成 45 秒,并且
"公网不通 + 连接数<=1"就立刻重启 ——
结果每次隧道还没把连接建齐,就被下一次重启打断,
**永远停在"只剩 1 条"的状态**。
等于我自己制造了不稳定。
修法:
1. 重启后给 GRACE=150 秒宽限期,这期间绝不重启,
让 cloudflared 专心把连接建齐
2. MIN_RESTART_GAP 从 45 提到 150(和宽限期一致)
3. "立刻重启"的判定加一条:距上次重启必须超过宽限期
实测(停掉 watchdog -> 手动重启隧道 -> 观察):
10 秒内建齐 4 条连接,公网立刻 200
宽限期生效后公网 20/20 = 100%,延迟 ~690ms
顺带一个教训:之前测出 80%、20% 的 000,
是我自己后台还跑着一个观察任务在并发打接口造成的干扰。
**测稳定性的时候,测量工具本身不能成为干扰源。**
"""
def sync_watchdog() -> None:
"""把设备上改好的 watchdog.sh 拉回本地仓库。"""
_c, out, _e = run("cat /root/lanremote-relay/watchdog.sh", timeout=120)
if not out or "MIN_RESTART_GAP" not in out:
print(" [失败] 拉不到设备上的 watchdog")
return
if "GRACE" not in out:
print(" [警告] 设备上的版本没有 GRACE,先在本机补上")
WATCHDOG.write_text(out, encoding="utf-8", newline="\n")
print(" 已同步 watchdog.sh(%d 行)" % out.count("\n"))
print(" 关键参数:")
for line in out.splitlines():
if line.startswith(("MIN_RESTART_GAP", "GRACE", "THRESHOLD",
"STALE_MINUTES")):
print(" " + line.strip())
def commit() -> None:
print()
subprocess.run([GIT, "add", "-A"], cwd=str(ROOT), check=False)
msg_file = ROOT / "_build" / "_c13.txt"
msg_file.write_bytes(MSG.encode("utf-8"))
r = subprocess.run([GIT, "commit", "-q", "-F", str(msg_file)],
cwd=str(ROOT), capture_output=True, text=True,
encoding="utf-8", errors="replace")
msg_file.unlink(missing_ok=True)
print(" commit exit=%s" % r.returncode)
show = subprocess.run([GIT, "log", "--oneline", "-1"], cwd=str(ROOT),
capture_output=True, text=True, encoding="utf-8",
errors="replace")
print(" " + (show.stdout or "").strip())
env = dict(os.environ)
env["GIT_SSH_COMMAND"] = r'"C:\Program Files\Git\usr\bin\ssh.exe"'
push = subprocess.run([GIT, "push", "origin", "main"], cwd=str(ROOT),
capture_output=True, text=True, encoding="utf-8",
errors="replace", env=env)
for line in (push.stderr or push.stdout or "").strip().splitlines()[-2:]:
print(" " + line)
def main() -> int:
sync_watchdog()
commit()
return 0
if __name__ == "__main__":
sys.exit(main())