From ab83fc34aa4a6a225742b800a8cbf16a6c090937 Mon Sep 17 00:00:00 2001 From: lo1nt Date: Tue, 8 Oct 2024 15:08:41 +0800 Subject: [PATCH] feat: support triple grpc heart beat (#1432) * feat: support triple grpc heart beat * fix: key --------- Co-authored-by: Lo1nt --- .../alipay/sofa/rpc/common/RpcOptions.java | 1 + .../sofa/rpc/common/config/RpcConfigKeys.java | 12 +++++++ .../sofa/rpc/common/rpc-config-default.json | 2 ++ .../triple/TripleClientTransport.java | 12 +++++++ .../triple/TripleClientTransportTest.java | 35 +++++++++++++++++++ 5 files changed, 62 insertions(+) create mode 100644 remoting/remoting-triple/src/test/java/com/alipay/sofa/rpc/transport/triple/TripleClientTransportTest.java diff --git a/core/api/src/main/java/com/alipay/sofa/rpc/common/RpcOptions.java b/core/api/src/main/java/com/alipay/sofa/rpc/common/RpcOptions.java index ff9ebebd3..2abdcdfc8 100644 --- a/core/api/src/main/java/com/alipay/sofa/rpc/common/RpcOptions.java +++ b/core/api/src/main/java/com/alipay/sofa/rpc/common/RpcOptions.java @@ -419,6 +419,7 @@ public class RpcOptions { * 默认 grpc maxInboundMessageSize大小 */ public static final String TRANSPORT_GRPC_MAX_INBOUND_MESSAGE_SIZE = "transport.grpc.maxInboundMessageSize"; + /** * 最大IO的buffer大小 */ diff --git a/core/common/src/main/java/com/alipay/sofa/rpc/common/config/RpcConfigKeys.java b/core/common/src/main/java/com/alipay/sofa/rpc/common/config/RpcConfigKeys.java index 2b4934ce0..d15aae3ac 100644 --- a/core/common/src/main/java/com/alipay/sofa/rpc/common/config/RpcConfigKeys.java +++ b/core/common/src/main/java/com/alipay/sofa/rpc/common/config/RpcConfigKeys.java @@ -140,4 +140,16 @@ public class RpcConfigKeys { false, "specify biz thread pool implementation type", new String[] { "sofa_rpc_server_thread_pool_type" }); + + /** + * grpc client keep alive interval + */ + public static ConfigKey TRIPLE_CLIENT_KEEP_ALIVE_INTERVAL = ConfigKey + .build( + "sofa.rpc.triple.client.keepAlive.interval", + 0, + false, + "keep alive interval in second for triple client", + new String[] { "sofa_rpc_triple_client_keepAlive_interval" }); + } diff --git a/core/common/src/main/resources/com/alipay/sofa/rpc/common/rpc-config-default.json b/core/common/src/main/resources/com/alipay/sofa/rpc/common/rpc-config-default.json index 796594c8f..48aaddf2b 100644 --- a/core/common/src/main/resources/com/alipay/sofa/rpc/common/rpc-config-default.json +++ b/core/common/src/main/resources/com/alipay/sofa/rpc/common/rpc-config-default.json @@ -273,6 +273,8 @@ PS:大家也看到了,本JSON文档是支持注释的,而标准JSON是不支 "compress.size.baseline": 2048, //Whether the Http2 Cleartext protocol client uses Prior Knowledge to start Http2 "transport.client.h2c.usePriorKnowledge": true, + // grpc client keep alive interval, default to 0, no keep alive + "sofa.rpc.triple.client.keepAlive.interval": 0, /*-------------Transport层相关配置结束-------------*/ /* diff --git a/remoting/remoting-triple/src/main/java/com/alipay/sofa/rpc/transport/triple/TripleClientTransport.java b/remoting/remoting-triple/src/main/java/com/alipay/sofa/rpc/transport/triple/TripleClientTransport.java index bf8adbe17..ee8f3e1bc 100644 --- a/remoting/remoting-triple/src/main/java/com/alipay/sofa/rpc/transport/triple/TripleClientTransport.java +++ b/remoting/remoting-triple/src/main/java/com/alipay/sofa/rpc/transport/triple/TripleClientTransport.java @@ -16,9 +16,11 @@ */ package com.alipay.sofa.rpc.transport.triple; +import com.alipay.sofa.common.config.SofaConfigs; import com.alipay.sofa.rpc.client.ProviderInfo; import com.alipay.sofa.rpc.common.RpcConfigs; import com.alipay.sofa.rpc.common.RpcOptions; +import com.alipay.sofa.rpc.common.config.RpcConfigKeys; import com.alipay.sofa.rpc.common.utils.NetUtils; import com.alipay.sofa.rpc.context.RpcInternalContext; import com.alipay.sofa.rpc.context.RpcInvokeContext; @@ -78,6 +80,10 @@ public class TripleClientTransport extends ClientTransport { protected final Object lock = new Object(); + protected static int KEEP_ALIVE_INTERVAL = SofaConfigs.getOrCustomDefault( + RpcConfigKeys.TRIPLE_CLIENT_KEEP_ALIVE_INTERVAL, + RpcConfigs.getIntValue(RpcConfigKeys.TRIPLE_CLIENT_KEEP_ALIVE_INTERVAL.getKey())); + /** * The constructor * @@ -278,6 +284,12 @@ private ManagedChannel initChannel(ProviderInfo url) { builder.disableRetry(); builder.intercept(clientHeaderClientInterceptor); builder.maxInboundMessageSize(RpcConfigs.getIntValue(RpcOptions.TRANSPORT_GRPC_MAX_INBOUND_MESSAGE_SIZE)); + + if (KEEP_ALIVE_INTERVAL > 0) { + builder.keepAliveWithoutCalls(true); + builder.keepAliveTime(KEEP_ALIVE_INTERVAL, TimeUnit.SECONDS); + } + return builder.build(); } diff --git a/remoting/remoting-triple/src/test/java/com/alipay/sofa/rpc/transport/triple/TripleClientTransportTest.java b/remoting/remoting-triple/src/test/java/com/alipay/sofa/rpc/transport/triple/TripleClientTransportTest.java new file mode 100644 index 000000000..23ca132d9 --- /dev/null +++ b/remoting/remoting-triple/src/test/java/com/alipay/sofa/rpc/transport/triple/TripleClientTransportTest.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.rpc.transport.triple; + +import org.junit.Assert; +import org.junit.Test; + +/** + * + * @author junyuan + * @version TripleClientTransportTest.java, v 0.1 2024-08-01 17:12 junyuan Exp $ + */ +public class TripleClientTransportTest { + + @Test + public void testInit() { + + Assert.assertEquals(TripleClientTransport.KEEP_ALIVE_INTERVAL, 0); + + } +} \ No newline at end of file