Skip to content

feat(gateway): resilient connection lifecycle, transport observability, and normalized event types - #12

Merged
cxyhhhhh merged 5 commits into
tencent-connect:mainfrom
cxyhhhhh:feat/connect
Nov 23, 2026
Merged

cxyhhhhh merged 5 commits into
tencent-connect:mainfrom
cxyhhhhh:feat/connect

Conversation

@cxyhhhhh

Copy link
Copy Markdown
Contributor

Summary

Reworks the WebSocket gateway into a pure-protocol, single-use
GatewayConnection with an explicit reconnect state machine, adds
transport-level observability, and opens the protocol types to the
platform's additive field evolution. Terminal failures are now reported
by rejecting start() with stable error codes instead of hanging or
retrying silently.

Connection & recovery

  • Session-preserving reconnects: opcode 7 / close 4009 keep the
    session and RESUME with the last seq, so events missed during the
    outage are replayed by the server instead of being lost to a fresh
    IDENTIFY. Reconnect budget only resets after READY/RESUMED, so
    "connect-then-kicked" loops still exhaust a finite budget.
  • Zombie-link detection: a heartbeat sent without a preceding ACK
    means the link is dead — the client closes and reconnects, with a
    watchdog to terminate the socket when even the close handshake stalls
    in a network black hole.
  • Terminal failures reject start() once with a GatewayError
    (GATEWAY_RETRY_EXHAUSTED for finite budgets, GATEWAY_FATAL_CLOSE
    for 4914/4915) instead of looping forever. The unlimited default is
    unchanged; set reconnect.maxAttempts when an outer supervisor owns
    restarts.
  • Robustness hardening: malformed HELLO heartbeat_interval values
    no longer clamp setInterval to 1ms (heartbeat storm) — they fail
    fast and reconnect; numeric strings are coerced for forward
    compatibility. The jittered first heartbeat is tracked and released
    in cleanup(), so a finished run never holds the event loop.

Observability

  • bot.connectionState / bot.ping / bot.lastHeartbeatAt — health
    checks without tracking ready/resumed yourself
  • new disconnected event ({ code, reason, willReconnect }) fired
    after the reconnect decision is made, so willReconnect always
    reflects reality
  • new connection event streaming state transitions
    (idle → connecting → connected/resuming → disconnected)
  • user callbacks are fully isolated: a throwing or async-rejecting
    handler can never stall the protocol loop

Type layer

  • InboundMessage.author — normalized, closed sender model; flat
    senderId/senderName/senderIsBot are deprecated but kept
  • OpenFields open index signatures + msg.raw passthrough: new
    platform fields are readable immediately without SDK updates
  • GatewayError matches on a stable code string (not instanceof),
    so it works across module copies

Fixes

  • token background refresh: a stop→start cycle no longer deletes the
    replacement refresh loop's controller
  • abort during token/gateway-URL lookup no longer opens a late socket

⚠️ Behavior note for consumers

Previously a fatal close (4914/4915) or an exhausted retry budget left
start() pending forever — no rejection, no cleanup, no signal to the
caller. It now rejects once with a GatewayError after releasing
resources. Callers that don't await bot.start() should add a
.catch() to avoid an unhandledRejection crash.

Testing

217 tests green, including integration tests against a real WebSocket
server (test/helpers/fake-gateway.ts): resume-on-reconnect, close-code
decision table, black-holed handshake timeout, heartbeat-storm
regression, budget exhaustion and restart. Coverage on changed modules:
reconnect.ts 91%, token.ts 92%, gateway-connection.ts ~74%,
errors.ts 100%.

Docs

  • README (en/zh): "Resources & community" section — official platform
    docs link, feedback assistant and developer channel QR codes (bundled
    into the npm package)

@cxyhhhhh
cxyhhhhh merged commit b316f18 into tencent-connect:main Nov 23, 2026
0 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant