3434import org .apache .ratis .thirdparty .io .netty .buffer .PooledByteBufAllocator ;
3535import org .apache .ratis .thirdparty .io .netty .handler .ssl .SslContext ;
3636import org .apache .ratis .util .JavaUtils ;
37+ import org .apache .ratis .util .MemoizedSupplier ;
3738import org .slf4j .Logger ;
3839import org .slf4j .LoggerFactory ;
3940
4041import java .util .function .BiFunction ;
4142import java .util .function .Consumer ;
43+ import java .util .function .Supplier ;
4244
4345public 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