feat: use HTTP POST for bootstrap to avoid WSS TLS handshake#29
Open
akiasprin wants to merge 2 commits into
Open
feat: use HTTP POST for bootstrap to avoid WSS TLS handshake#29akiasprin wants to merge 2 commits into
akiasprin wants to merge 2 commits into
Conversation
Co-Authored-By: Claude <noreply@anthropic.com>
6437cdc to
5c3f07f
Compare
- Rename wssUrl param to wsUrl since it can be ws:// or wss:// - Convert ws: to http: in addition to wss: to https: - Stop stripping /ws suffix — the HTTP endpoint path may differ from the WS path Co-Authored-By: Claude <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bootstrap JSON-RPC over HTTP POST
Bootstrap now uses HTTP POST for JSON-RPC calls instead of WebSocket. This allows the initial data load to reuse the page's existing HTTPS connection and removes WebSocket connection establishment from the bootstrap critical path.
Why
Bootstrap is latency-sensitive because no node data can be displayed until the initial RPC requests complete.
Previously, bootstrap requests were sent over WSS. Although a WebSocket handshake starts as an HTTP Upgrade request, browsers do not reuse the fetch/XHR connection pool for WebSockets —
new WebSocket()always establishes a separate connection (RFC 6455 §4.1). Even on the same origin, this requires an additional TCP/TLS handshake before any RPC request can be sent.In practice, establishing a WSS connection can easily take around 100–200 ms depending on network conditions. Only after the handshake completes can the first JSON-RPC request be sent, and the response typically requires another network round trip (~50–100 ms).
As a result, bootstrap could spend roughly 200–300 ms waiting on connection setup and the first response before any data becomes available.
By moving bootstrap RPCs to HTTP POST, requests can be issued immediately over the page's existing HTTPS connection. The WSS connection is still established in the background and remains available for subsequent polling and updates.
Before
Bootstrap loaded data through WSS:
The initial data fetch was blocked on WebSocket connection establishment.
After
Bootstrap loads data through three parallel HTTP POST JSON-RPC calls:
These requests reuse the existing HTTPS connection and can start immediately. Meanwhile, the WSS connection is established asynchronously in the background for subsequent polling.
Changes
src/api/client.tsurl,token, andnameaspublic readonly; addedhttpRpcCall(), which convertswss://URLs tohttps://and sends JSON-RPC requests via HTTP POSTsrc/hooks/useNodes.tshttpRpcCall(); loadslistAgentUuids, metadata/static data, and dynamic data in parallel; removed the separatetickDynamic()bootstrap callBootstrap 阶段改用 HTTP POST 发送 JSON-RPC
Bootstrap 阶段改为通过 HTTP POST 发送 JSON-RPC 请求,而不再依赖 WebSocket。这样可以直接复用页面已有的 HTTPS 连接,并将 WebSocket 建连过程移出 Bootstrap 的关键路径。
为什么
Bootstrap 对延迟非常敏感,因为在初始 RPC 请求完成之前,页面无法展示任何节点数据。
此前 Bootstrap 通过 WSS 发起请求。虽然 WebSocket 握手本质上是一个 HTTP Upgrade 请求,但浏览器不会让 WebSocket 复用 fetch/XHR 的连接池——
new WebSocket()总是建立独立连接(RFC 6455 §4.1)。即使是同源请求,也需要额外完成一次 TCP/TLS 握手后才能发送 RPC 请求。实际网络环境下,建立 WSS 连接通常需要约 100–200 ms。只有握手完成后才能发送第一个 JSON-RPC 请求,而等待请求返回通常还需要额外一次网络往返(约 50–100 ms)。
因此在节点数据可用之前,Bootstrap 很容易额外消耗约 200–300 ms 的等待时间,仅仅用于连接建立和首个请求返回。
改为 HTTP POST 后,Bootstrap 请求可以立即通过页面已有的 HTTPS 连接发出。同时 WSS 连接仍会在后台建立,并继续用于后续轮询和实时更新。
改动前
Bootstrap 通过 WSS 加载数据:
初始数据获取必须等待 WebSocket 建连完成。
改动后
Bootstrap 改为通过三个并行 HTTP POST JSON-RPC 请求加载数据:
这些请求直接复用现有 HTTPS 连接,可立即开始执行;与此同时,WSS 连接在后台异步建立,供后续轮询使用。
代码改动
src/api/client.tsurl、token、name暴露为public readonly;新增httpRpcCall(),将wss://转换为https://并通过 HTTP POST 发送 JSON-RPC 请求src/hooks/useNodes.tshttpRpcCall();并行加载listAgentUuids、meta/static 和 dynamic 数据;移除单独的tickDynamic()初始化调用