Skip to content

fix: complete P0 RPC correctness and lifecycle - #9

Merged
xiaoyumuxi merged 2 commits into
mainfrom
fix/p0-rpc-correctness-lifecycle
Sep 14, 2026
Merged

xiaoyumuxi merged 2 commits into
mainfrom
fix/p0-rpc-correctness-lifecycle

Conversation

@xiaoyumuxi

Copy link
Copy Markdown
Owner

目标

集中完成当前 P0 级别的 RPC 正确性和资源生命周期问题,不扩展 Retry / 熔断 / Nacos 缓存等 P1/P2 功能。

1. 修复 CompletableFuture 异步返回类型

之前 JDK / ByteBuddy 代理会把 method.getReturnType()(即 CompletableFuture.class)直接传给 RpcClient,导致异步 RPC 无法按真正的 T 反序列化。

本次:

  • 新增 RpcReturnTypeResolver
  • CompletableFuture<User> 会提取 User.class 作为响应反序列化类型
  • JDK Proxy / ByteBuddy Proxy 保持相同行为
  • 对 CompletableFuture<List<User>> 等当前 Serializer API 无法准确表达的嵌套参数化类型明确报错,不再静默错误反序列化
  • 新增两套代理的异步回归测试

2. 去除同地址并发重复建连

重构 ChannelProvider:

  • 从静态全局连接池改为单个 Netty 客户端实例持有
  • 已建立连接继续缓存 + LRU
  • 新增 connectingChannels,用 CompletableFuture<Channel> 表示正在建立的连接
  • 同一地址的并发首次请求共享同一个 in-flight connection
  • 移除原来的 CountDownLatch.await(5s) 同步建连等待
  • 连接失败后可正常重试
  • Provider 关闭时失败正在连接的 Future,并关闭缓存 Channel

新增确定性单元测试,不依赖真实端口。

3. 完善 Client / Server 生命周期

Client

  • TransportClient extends AutoCloseable
  • RpcClient implements AutoCloseable
  • RpcClient.close() 幂等
  • 关闭后拒绝新请求,不再继续服务发现/建连
  • NettyTransportClient 不再使用 static EventLoopGroup / Bootstrap
  • 每个客户端实例独立拥有 EventLoopGroup + ChannelProvider
  • 关闭 Channel 时,所有 pending request 通过 channelInactive 失败返回
  • ProxyFactory 支持 close,JDK / ByteBuddy 关闭其持有的 RpcClient
  • 新增 RpcClientProxy.shutdown(),让静态代理入口也有应用级资源释放路径

Server

  • RpcServer implements AutoCloseable
  • stop()/close() 幂等,统一清注册中心 + 停 TransportServer
  • NettyTransportServer 保存 listener Channel,stop() 会真正 close listener,使 start() 阻塞线程可退出
  • EventLoopGroup / business executor 在停止时回收
  • E2E 集成测试改为显式关闭 RpcClient/RpcServer,并断言 Server 线程退出

测试

新增/扩展:

  • ProxyAsyncReturnTypeTest
  • RpcClientTest close/idempotency
  • ChannelProviderTest
  • NettyRpcClientHandlerTest channelInactive pending cleanup
  • FullIntegrationTest client/server lifecycle

等待主 CI 的 Build / Unit Tests & Coverage / RPC Integration / CI Gate 全量验证。

@xiaoyumuxi
xiaoyumuxi merged commit cca65ea into main Sep 14, 2026
4 checks passed
@xiaoyumuxi
xiaoyumuxi deleted the fix/p0-rpc-correctness-lifecycle branch September 14, 2026 07:33
@xiaoyumuxi
xiaoyumuxi restored the fix/p0-rpc-correctness-lifecycle branch September 14, 2026 07:36
@xiaoyumuxi
xiaoyumuxi deleted the fix/p0-rpc-correctness-lifecycle branch September 14, 2026 07:37
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