szetszwo commented on code in PR #1306: URL: https://github.com/apache/ratis/pull/1306#discussion_r2521275170
########## ratis-grpc/src/main/java/org/apache/ratis/grpc/server/GrpcStubPool.java: ########## @@ -0,0 +1,110 @@ +/* + * 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 org.apache.ratis.grpc.server; + +import org.apache.ratis.protocol.RaftPeer; +import org.apache.ratis.thirdparty.io.grpc.ManagedChannel; +import org.apache.ratis.thirdparty.io.grpc.netty.NegotiationType; +import org.apache.ratis.thirdparty.io.grpc.netty.NettyChannelBuilder; +import org.apache.ratis.thirdparty.io.grpc.stub.AbstractStub; +import org.apache.ratis.thirdparty.io.netty.channel.ChannelOption; +import org.apache.ratis.thirdparty.io.netty.channel.WriteBufferWaterMark; +import org.apache.ratis.thirdparty.io.netty.handler.ssl.SslContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.Semaphore; +import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.TimeUnit; +import java.util.function.Function; + +final class GrpcStubPool<S extends AbstractStub<S>> { + public static final Logger LOG = LoggerFactory.getLogger(GrpcStubPool.class); + + static final class PooledStub<S extends AbstractStub<S>> { + private final ManagedChannel ch; + private final S stub; + private final Semaphore permits; + + PooledStub(ManagedChannel ch, S stub, int maxInflight) { + this.ch = ch; + this.stub = stub; + this.permits = new Semaphore(maxInflight); + } + + S getStub() { + return stub; + } + + void release() { + permits.release(); + } + } + + private final List<PooledStub<S>> pool; Review Comment: Use MemoizedSupplier so the Stub is lazily created. ```java private final List<MemoizedSupplier<Stub<S>>> pool; ``` ########## ratis-grpc/src/main/java/org/apache/ratis/grpc/GrpcConfigKeys.java: ########## @@ -282,6 +282,15 @@ static GrpcTlsConfig tlsConf(Parameters parameters) { static void setTlsConf(Parameters parameters, GrpcTlsConfig conf) { parameters.put(TLS_CONF_PARAMETER, conf, TLS_CONF_CLASS); } + + String STUB_POOL_SIZE_KEY = PREFIX + ".stub.pool.size"; + int STUB_POOL_SIZE_DEFAULT = 10; Review Comment: Change the default to 1. ########## ratis-grpc/src/main/java/org/apache/ratis/grpc/server/GrpcServerProtocolClient.java: ########## @@ -45,6 +45,7 @@ class GrpcServerProtocolClient implements Closeable { // Common channel private final ManagedChannel channel; + private final GrpcStubPool<RaftServerProtocolServiceStub> pool; Review Comment: Do not create a pool when the size is 1. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
