Skip to content

Commit 03ec1e1

Browse files
committed
Initialize SslContexts only if it is used.
1 parent 1d38136 commit 03ec1e1

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

ratis-grpc/src/main/java/org/apache/ratis/grpc/GrpcFactory.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@
3434
import org.apache.ratis.thirdparty.io.netty.buffer.PooledByteBufAllocator;
3535
import org.apache.ratis.thirdparty.io.netty.handler.ssl.SslContext;
3636
import org.apache.ratis.util.JavaUtils;
37+
import org.apache.ratis.util.MemoizedSupplier;
3738
import org.slf4j.Logger;
3839
import org.slf4j.LoggerFactory;
3940

4041
import java.util.function.BiFunction;
4142
import java.util.function.Consumer;
43+
import java.util.function.Supplier;
4244

4345
public class GrpcFactory implements ServerFactory, ClientFactory {
4446

@@ -90,8 +92,8 @@ private SslContexts(GrpcTlsConfig tlsConfig, GrpcTlsConfig adminTlsConfig,
9092

9193
private final GrpcServices.Customizer servicesCustomizer;
9294

93-
private final SslContexts forServer;
94-
private final SslContexts forClient;
95+
private final Supplier<SslContexts> forServerSupplier;
96+
private final Supplier<SslContexts> forClientSupplier;
9597

9698
public GrpcFactory(Parameters parameters) {
9799
this(GrpcConfigKeys.Server.servicesCustomizer(parameters),
@@ -107,10 +109,10 @@ private GrpcFactory(GrpcServices.Customizer servicesCustomizer,
107109
GrpcTlsConfig clientTlsConfig, GrpcTlsConfig serverTlsConfig) {
108110
this.servicesCustomizer = servicesCustomizer;
109111

110-
this.forServer = new SslContexts(tlsConfig, adminTlsConfig, clientTlsConfig, serverTlsConfig,
111-
BUILD_SSL_CONTEXT_FOR_SERVER);
112-
this.forClient = new SslContexts(tlsConfig, adminTlsConfig, clientTlsConfig, serverTlsConfig,
113-
BUILD_SSL_CONTEXT_FOR_CLIENT);
112+
this.forServerSupplier = MemoizedSupplier.valueOf(() -> new SslContexts(
113+
tlsConfig, adminTlsConfig, clientTlsConfig, serverTlsConfig, BUILD_SSL_CONTEXT_FOR_SERVER));
114+
this.forClientSupplier = MemoizedSupplier.valueOf(() -> new SslContexts(
115+
tlsConfig, adminTlsConfig, clientTlsConfig, serverTlsConfig, BUILD_SSL_CONTEXT_FOR_CLIENT));
114116
}
115117

116118
@Override
@@ -127,6 +129,8 @@ public LogAppender newLogAppender(RaftServer.Division server, LeaderState state,
127129
public GrpcServices newRaftServerRpc(RaftServer server) {
128130
checkPooledByteBufAllocatorUseCacheForAllThreads(LOG::info);
129131

132+
final SslContexts forServer = forServerSupplier.get();
133+
final SslContexts forClient = forClientSupplier.get();
130134
return GrpcServicesImpl.newBuilder()
131135
.setServer(server)
132136
.setCustomizer(servicesCustomizer)
@@ -141,6 +145,7 @@ public GrpcServices newRaftServerRpc(RaftServer server) {
141145
public GrpcClientRpc newRaftClientRpc(ClientId clientId, RaftProperties properties) {
142146
checkPooledByteBufAllocatorUseCacheForAllThreads(LOG::debug);
143147

148+
final SslContexts forClient = forClientSupplier.get();
144149
return new GrpcClientRpc(clientId, properties, forClient.adminSslContext, forClient.clientSslContext);
145150
}
146151
}

0 commit comments

Comments
 (0)