docs(handler/tunnel): add architecture documentation and fix observeStats event loss

- Add package-level architecture doc in handler.go covering:
  - NAT traversal reverse proxy architecture
  - CmdBind/CmdConnect roles and data flow
  - Connector lifecycle and waitClose semantics
  - Entrypoint protocol dispatch (first-byte sniffing)
  - SD fallback behavior
- Add data-flow-oriented doc comments to all business files:
  - entrypoint.go: protocol dispatch, dial flow
  - connector.go: Connector/ConnectorPool semantics
  - dialer.go: two-phase dial strategy
  - tunnel.go: MaxWeight semantics, selection algorithm
  - bind.go: 6-step CmdBind flow
  - connect.go: CmdConnect flow with relay framing
  - ephttp.go, eptls.go, eprelay.go: per-protocol entrypoint flow
- Fix observeStats: after successful error retry, also flush new events
  instead of skipping the current tick (handler.go:261-271)
- Add test for observeStats retry-then-flush (handler_test.go)
This commit is contained in:
ginuerzh
2026-06-02 18:36:52 +08:00
parent e791ba47e2
commit 31878a72ec
12 changed files with 264 additions and 3 deletions
+14
View File
@@ -9,6 +9,20 @@ import (
"github.com/go-gost/core/sd"
)
// Dialer resolves a tunnel connector and returns a stream to it.
//
// Dial strategy (two-phase):
// 1. Local pool: try ConnectorPool.Get() up to retry times. Each attempt
// calls GetConn() (= mux.OpenStream) on the same connector — if the
// connector is dead, all retries fail until Tunnel.clean() removes it
// (up to TTL, default 15s).
// 2. SD fallback: if pool returns nil AND sd is configured, query service
// discovery for remote nodes. Filter out self (d.node) and mismatched
// networks. Establish a raw TCP connection to the remote address,
// bypassing the mux layer entirely.
//
// The returned node and connector ID identify which node/hop the stream
// is connected to — used by callers to decide the relay protocol framing.
type Dialer struct {
node string
pool *ConnectorPool