gRPC(Java) keepAlive机制研究
作者:互联网
结论
- gRPC keepAlive是grpc框架在应用层面连接保活的一种措施。即当grpc连接上没有业务数据时,是否发送pingpong,以保持连接活跃性,不因长时间空闲而被Server或操作系统关闭
- gRPC keepAlive在client与server都有,client端默认关闭(keepAliveTime为Long.MAX_VALUE), server端默认打开,keepAliveTime为2小时,即每2小时向client发送一次ping
// io.grpc.internal.GrpcUtil
public static final long DEFAULT_SERVER_KEEPALIVE_TIME_NANOS = TimeUnit.HOURS.toNanos(2L);
- KeepAlive的管理使用类
io.grpc.internal.KeepAliveManager
, 用于管理KeepAlive状态,ping任务调度与执行.
Client端KeepAlive
使用入口
- 我们在使用io.grpc框架创建grpc连接的时候,可以设置keeplive, 例如下面:
NettyChannelBuilder builder = NettyChannelBuilder.forTarget(String.format("grpc://%s", provider)) //
.usePlaintext() //
.defaultLoadBalancingPolicy(props.getBalancePolicy()) //
.maxInboundMessageSize(props.getMaxInboundMessageSize()) //
.keepAliveTime(1,TimeUnit.MINUTES)
.keepAliveWithoutCalls(true)
.keepAliveTimeout(10,TimeUnit.SECONDS)
.intercept(channelManager.getInterceptors()); //
- 其中与keepAlive相关的参数有三个,
keepAliveTime
,keepAliveTimeout
,keepAliveWithoutCalls
。这三个变量有什么作用呢?
- keepAliveTime: 表示当grpc连接没有数据传递时,多久之后开始向server发送ping packet
- keepAliveTimeout: 表示当发送完ping packet后多久没收到server回应算超时
- keepAliveTimeoutCalls: 表示如果grpc连接没有数据传递时,是否keepAlive,默认为false
简要时序列表
Create & Start
NettyChannelBuilder
-----> NettyTransportFactory
---------> NettyClientTransport
-------------> KeepAliveManager & NettyClientHandler
响应各种事件
当Active、Idle、DataReceived、Started、Termination事件发生时,更改KeepAlive状态,调度发送ping任务。
Server端KeepAlive
使用入口
// 只截取关键代码,详细代码请看`NettyServerBuilder`
ServerImpl server = new ServerImpl(
this,
buildTransportServers(getTracerFactories()),
Context.ROOT);
for (InternalNotifyOnServerBuild notifyTarget : notifyOnBuildList) {
notifyTarget.notifyOnBuild(server);
}
return server;
// 在buildTransportServers方法中创建NettyServer
List<NettyServer> transportServers = new ArrayList<>(listenAddresses.size());
for (SocketAddress listenAddress : listenAddresses) {
NettyServer transportServer = new NettyServer(
listenAddress, resolvedChannelType, channelOptions, bossEventLoopGroupPool,
workerEventLoopGroupPool, negotiator, streamTracerFactories,
getTransportTracerFactory(), maxConcurrentCallsPerConnection, flowControlWindow,
maxMessageSize, maxHeaderListSize, keepAliveTimeInNanos, keepAliveTimeoutInNanos,
maxConnectionIdleInNanos, maxConnectionAgeInNanos, maxConnectionAgeGraceInNanos,
permitKeepAliveWithoutCalls, permitKeepAliveTimeInNanos, getChannelz());
transportServers.add(transportServer);
}
简要时序列表
Create & Start
NettyServerBuilder
---> NettyServer
---------> NettyServerTransport
-------------> NettyServerHandler
-----------------> KeepAliveEnforcer
连接准备就绪
调用 io.netty.channel.ChannelHandler的handlerAdded
方法,关于此方法的描述:
Gets called after the ChannelHandler was added to the actual context and it's ready to handle events.
NettyServerHandler(handlerAdded)
---> 创建KeepAliveManager对象
响应各种事件
同Client
KeepAliveEnforcer
在上面Server端的简要时序图中,可以看见,server端有一个特有的io.grpc.netty.KeepAliveEnforcer
类
此类的作用是监控clinet ping的频率,以确保其在一个合理范围内。