Agent-Server TCP session redesign
Status
- Status: Accepted Target Design
- Scope:
Agent <-> Server - Out of scope:
SDK <-> Agent
Design conclusion
This design converges the transport model of the Agent <-> Server main link. The core conclusions:
- The default
Agent <-> Servertransport migrates from the currentlegacy REQ/REPto a standalonetcpsession, withtlsenabled as configured. - The
Agentkeeps only an outbound long connection to theServer; theServeris no longer required to dial back to therpc_addrtheAgentexposes. Server -> AgentInvoke,StartTask,CancelTask, andOpsrequests are all delivered over the existingAgent-Serversession.- The
Agent's local listen address serves onlyGameServer / SDK / third-party applicationsand no longer carries anyServer -> Agentcallback duty. - The
legacy messaging patternis no longer the abstraction center of the main link; everything converges on a lightweight session protocol that is "single connection, bidirectional, multiplexed, reconnectable, governable".
The shared session runtime and the subprotocol
Two terms need to be pinned down here as well:
shared session runtime- the session transport foundation shared by the two main links
- covering
tcp/tls + framing + mux + reconnect + heartbeat + drain + backpressure
subprotocol- the application-layer subprotocol running on that foundation
- for
Agent <-> Server, that is theagent-server subprotocol
The key characteristics of the agent-server subprotocol:
- The first frame must be a
RegisterRequestor its later equivalent connect message tlsis enabled by default- It targets the agent session, not an
rpc_addrcallback
Background
In the current repository, Agent <-> Server actually mixes two links with different semantics:
- Control plane: the
Agentactively connects to theServeroverlegacy REQ/REPand completesRegister/Heartbeat - Invocation plane: the
Server/Dispatcherthen dials back to theAgentbyrpc_addrto executeInvoke/Task/Ops
This creates several problems:
- A split transport model: one connection for the control plane, another for the invocation plane
- Confused
rpc_addrsemantics: it looks like "the Agent's registration info" but is essentially "the entry point for the Server to dial back" - Unclear cluster semantics: the
Agentis actually connected to a specificServernode, yet the invocation plane tries to bypass that session and dial theAgentdirectly legacy REQ/REPis inherently unsuited to "the Server proactively issuing new requests on an existing connection"
This is not because the legacy transport lacks long-connection capability, but because the REQ/REP pattern in use does not match the target session model.
What we actually need
What this scenario actually needs is not "some messaging-middleware pattern" but a lightweight application-layer session:
- One reliable long connection at the bottom
- Identity and capability negotiation first, right after the connection is established
- Both sides able to initiate new requests proactively over the same connection
- Multiple concurrent in-flight requests allowed on the same connection
- On disconnect, the old session invalidates immediately; after reconnection a new session is rebuilt
- Support for heartbeat, backpressure, drain, timeouts, cancellation, and graceful traffic removal
In other words, what we want is not REQ/REP but "a single-connection bidirectional session protocol".
Why TCP session fits this scenario better
The shape of the business boundary
The current Agent <-> Server scenario has a few very clear constraints:
- Deployment is mainly on intranets, within the same datacenter, on the same cluster network
- No need for
libp2p-style hole punching, peer discovery, or multi-node mesh capability - No need for a general protocol stack like
RSocketcovering many transports and interaction models - No need to pull in
Aeron, which leans toward high-performance messaging infrastructure, by default - What is needed is a transport layer that is stable, simple, controllable, and evolvable across versions
So the default design should prioritize:
- simplicity
- easy troubleshooting
- easy unification across services
- easy future reuse of the same session mental model with
SDK <-> Agent
Why not keep the legacy messaging pattern
The legacy transport itself is not wrong; the problem is that it provides a "messaging-pattern abstraction" while we need a "session-protocol abstraction".
The main problems of keeping the legacy transport on the main link:
REQ/REPdoes not suit bidirectional proactive requests- Even with
PAIRor other patterns, the session, mux, reconnection, and backpressure semantics would still have to be built by us - The complexity lands in our own protocol layer anyway — the
legacy transportdoes not solve it for us - Since the protocol layer is ours regardless, staying bound to the
legacy transportonly makes the model more contorted
The conclusion is not "the legacy transport cannot do long connections" but "the legacy messaging pattern is not the right abstraction for our current main link".
Why not RSocket / libp2p / Aeron / HTTP2
All of these directions are instructive, but none fits as the current default infrastructure:
RSocket- The upside: session, bidirectionality, and multiplexing are all complete
- The problem: the protocol layer is too heavy; headers and interaction models exceed this project's needs
libp2p- The upside: protocol negotiation and stream multiplexing are mature
- The problem: it primarily serves peer-to-peer networking and multi-node overlays, not our current need
Aeron- The upside: very strong for same-host and low-latency scenarios; a later reference for local optimization
- The problem: it is more a high-performance messaging infrastructure, wrong as the default control plane
HTTP/2- The upside: the stream/mux mental model is worth referencing
- The problem: full HTTP semantics and its header system are not what we want
So the right landing point is:
- reference
HTTP/2's "multiplexing over one connection" idea - stay lighter than
RSocket - stay more general and easier to adopt than
Aeron - implement the minimal session protocol directly on
TCPourselves
Target architecture
Target topology
+----------------------+ +----------------------+
| Agent | | Server Node |
| - Session client | | - Session manager |
| - Local dispatch | TCP(+TLS) | - Registry |
| - SDK/Game bridge +-----------> | - Invoke/Task/Ops |
| | | |
+----------------------+ +----------------------+Key changes:
- The
Agentconnects to theServerproactively - The
Serverowns theAgent session - All
Server -> Agentrequests travel over this existing session - The
Agent's local listener is reserved for local integrators only, never exposed to theServer
Session semantics
After the connection is established, the Agent must complete session registration on it:
- The
Agentestablishes atcpconnection, withtlsattached per configuration - The
Agentsends anAgentConnectRequestor equivalent registration message - The
Serverreturns thesession_id, the capability negotiation result, and any warnings - Both sides start heartbeats
- The
Serversubsequently deliversInvoke/Task/Opsto theAgenton the same connection - On disconnect, the session invalidates immediately
- After reconnecting, the
Agentmust re-register and obtain a newsession_id
Transport layer design
Default transport
- Default transport:
tcp - Default security mode:
tls.enabled tlsis a security configuration on top oftcp, not a separately defined new transport
framing
Keep the minimal framing:
+--------------+------------------+-----------+
| FrameLength | Croupier Header | Body |
| 4 bytes | 8 bytes | N bytes |
+--------------+------------------+-----------+Constraints:
FrameLengthis the total length of the followingHeader + BodyHeadercontinues to reuse the existingVersion + MsgID + RequestIDBodyis protobuf-encoded- Single-frame size is bounded by both sides' configured maximum frame length
The bidirectional multiplexing model
Basic principles
The same Agent-Server connection lets both sides initiate requests proactively:
Agent -> Server- Connect/Register
- Heartbeat
- Capabilities update
- Metrics / 状态上报
Server -> Agent- Invoke
- StartTask
- CancelTask
- Ops / control
- Drain / shutdown
Request-response rules
- Requests use odd
MsgIDs - Responses use even
MsgIDs - Each side maintains its own incrementing
RequestIDcounter - Responses must echo the original request's
RequestID - Multiple concurrent in-flight requests are allowed on the same connection
This means Agent <-> Server is no longer "one-way register/heartbeat plus a separate callback connection" but a genuinely bidirectional, multiplexed session.
Clustering and routing
This is the most critical difference in the Agent <-> Server overhaul.
The current problem
Today the Dispatcher can bypass the Agent -> Server control-plane session and dial the Agent directly by rpc_addr. Once the session model lands, this practice must be retired.
The new model
Introduce the notion of a "session's owning node":
- Every
Agent sessionbelongs to oneServer node - The registry must record:
agent_idsession_idserver_node_idexpire_at- capability and provider summaries
- After the
Dispatcherselects anAgent, it no longer dials theAgentdirectly - Instead it routes the request to the
Server nodeholding thatsession - That node delivers it to the
Agentover the existing session
Routing consequences
Once this is done:
rpc_addris no longer a runtime dependency ofServer -> Agent- An
Agentneeds only one outbound connection to someServernode - Other
Servernodes in the cluster only need to know "which node holds this session"
The boundary of the Agent's local listener
After the redesign, the Agent's local listen address serves local integrators only:
GameServer -> AgentSDK -> Agent- third-party local applications ->
Agent
Explicitly no longer used for:
Server -> Agentcallbacks
The following concepts therefore move to deprecated:
rpc_addras theServer -> Agentinvocation entry point- the
Dispatcherdialing theAgentdirectly - any model that relies on the
Agentexposing a control-plane callback port
Reconnection, backpressure, and draining
Reconnection
Recommended defaults:
enabled = trueinitial_delay_ms = 1000max_delay_ms = 30000backoff_multiplier = 2.0jitter_factor = 0.2- after the cap, keep retrying at the cheap fixed cadence
Backpressure
The Server should maintain at least:
max_inflight_requestsper agent session- a pending-queue cap per agent session
- a maximum frame size
- the session's current
drainingstate
Implementation status (v0.1.2):
transport/tcp/server.go(the Server's listen side): the read loop invokes the handler synchronously — a slow handler stalls the read loop, the TCP receive window tightens, and end-to-end backpressure forms naturally. Session-levelmax_inflight_requests/ queue caps are not yet implemented (unnecessary under the synchronous model for now, but an overloaded session cannot be proactively drained — that relies ondrain).transport/tcp/mux_conn.go(the bidirectional session for both Agent↔Server and Agent↔Provider): dual-lane bounded dispatch has landed (dispatchInbound, commit31d7bdc0f):- system/control lane (
0x01xxheartbeat/registration,0x05xxsession control including drain,0x060101Hello): a dedicated queue (ControlQLen, default 64) + one worker, never rejected — business overload cannot kill the session, and drain signals always get through - business lane (
0x03xx/0x04xx/0x060103forwarded invokes):DispatchWorkers(default NumCPU) workers + a boundedBusinessQLen(default workers×4) queue; when full, a busy error frame is returned, triggering peer failover - classification is carried by the explicit MsgID set in
protocol.IsControlRequest()(within the 0x06 family, Hello = control / ForwardInvoke = business; a byte-prefix rule cannot cut it) - tests:
mux_conn_dual_lane_test.go(heartbeats answer promptly under business saturation / the control lane is never rejected / concurrent writes are safe)
- system/control lane (
- The Go SDK has landed the dual lanes in step (
ctrlInbox); the other five languages are pending migration (see the dual-lane section ofsdk-wire-protocol.md). - The explicit overload signals (
overloaded/retry_after_ms/too_many_inflight) have no wire messages defined yet.
drain
The Server should support sending drain to a single Agent session:
- stop assigning it new requests
- let already-sent requests finish within a timeout window
- after the timeout, close the connection and invalidate the session
The drain here is session-level graceful traffic removal, not an immediate disconnect:
- the
Dispatcherno longer routes newInvoke / Task / Opsto that session - the
Agentkeeps processing in-flight requests - heartbeats continue
- the connection closes after the drain completes, or is force-closed after the grace period
Protocol and message evolution recommendations
The legacy-model cleanup of agent/v1/register.proto is partially complete:
rpc_addris reserved (the proto field is deleted), and the registry has executed the DB column drop (internal/platform/registry/agent_session_db.go, nativeALTER TABLE ... DROP COLUMNper CR-FIX-15 — notmigrator.DropColumn, whose SQLite rebuild path has historically wiped serializer columns); onlyinternal/logic/utils/registry_helpers.gokeeps a compatibility alias- Migration semantics (CR-FIX-15): no backfill — the old self-published addresses are retired, the reachable address now comes from the live TCP session
RemoteAddr; no rollback data — if the drop fails (e.g. blocked by a legacy view/trigger), the error is logged (WARN, never swallowed) and the unmapped legacy column stays in place harmlessly, with the drop retried on next boot RegisterRequest's semantics have converged from "control-plane registration" to "session establishment + registration in one" (see the message families insdk-wire-protocol.md)
Remaining naming evolution:
- Keep the existing messages for transitional compatibility
- Introduce more accurate names gradually, e.g.:
AgentConnectRequestAgentConnectResponseAgentHeartbeatRequestAgentDrainRequest
- Deprecate
rpc_addronce the transition completes
Implementation order
The recommended order of work:
- Freeze this document
- Introduce the
tcp session listeneron theServerside - Introduce the
tcp session clienton theAgentside - Get
Register/Heartbeatrunning on the new session - Switch
Invoke/StartTask/CancelTask/Opsto delivery over the existing session - Add
server_node_id + session_idrouting info to the registry - Rework the
Dispatcherso it no longer dials theAgentdirectly - Clean up the
rpc_addrdirect-connection dependency - Remove the
legacy transportfrom the default main link
Final assessment
For this project, the core need of Agent <-> Server is not:
- many transports, kitchen-sink style
- P2P discovery and hole punching
- an extreme same-host low-latency message bus
but:
- one connection
- bidirectional proactive requests
- multiplexing
- simple and controllable
- easy to troubleshoot
- easy to unify with the
SDK <-> Agentmodel
So Agent <-> Server is best converged into:
- a lightweight TCP session
- TLS on demand
- the Server owns the Agent session
- Invoke/Task/Ops all reuse the existing session
- the Agent's local listener serves local integrators only
That fits the system's real needs better than continuing to patch the current main link around the legacy messaging pattern.
