Skip to content

Commit ac603f3

Browse files
committed
Make NettyStreamFactoryFactory implement AutoCloseable (#1244)
Since StreamFactoryFactory is now internal, just made that interface extend AutoCloseable in order to simplify MongoClients code JAVA-5158
1 parent eed7815 commit ac603f3

File tree

6 files changed

+22
-6
lines changed

6 files changed

+22
-6
lines changed

driver-core/src/main/com/mongodb/connection/NettyTransportSettings.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ public Builder socketChannelClass(final Class<? extends SocketChannel> socketCha
8787
/**
8888
* Sets the event loop group.
8989
*
90-
* <p>It is highly recommended to supply your own event loop group and manage its shutdown. Otherwise, the event
91-
* loop group created by default will not be shutdown properly.</p>
90+
* <p>The application is responsible for shutting down the provided {@code eventLoopGroup}</p>
9291
*
9392
* @param eventLoopGroup the event loop group that all channels created by this factory will be a part of
9493
* @return this

driver-core/src/main/com/mongodb/internal/connection/AsynchronousSocketChannelStreamFactoryFactory.java

+4
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,8 @@ public AsynchronousSocketChannelStreamFactoryFactory(final InetAddressResolver i
3636
public StreamFactory create(final SocketSettings socketSettings, final SslSettings sslSettings) {
3737
return new AsynchronousSocketChannelStreamFactory(inetAddressResolver, socketSettings, sslSettings);
3838
}
39+
40+
@Override
41+
public void close() {
42+
}
3943
}

driver-core/src/main/com/mongodb/internal/connection/StreamFactoryFactory.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
/**
2323
* A factory of {@code StreamFactory} instances.
2424
*/
25-
public interface StreamFactoryFactory {
25+
public interface StreamFactoryFactory extends AutoCloseable {
2626

2727
/**
2828
* Create a {@code StreamFactory} with the given settings.
@@ -32,4 +32,7 @@ public interface StreamFactoryFactory {
3232
* @return a stream factory that will apply the given settins
3333
*/
3434
StreamFactory create(SocketSettings socketSettings, SslSettings sslSettings);
35+
36+
@Override
37+
void close();
3538
}

driver-core/src/main/com/mongodb/internal/connection/TlsChannelStreamFactoryFactory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
/**
6060
* A {@code StreamFactoryFactory} that supports TLS/SSL. The implementation supports asynchronous usage.
6161
*/
62-
public class TlsChannelStreamFactoryFactory implements StreamFactoryFactory, Closeable {
62+
public class TlsChannelStreamFactoryFactory implements StreamFactoryFactory {
6363

6464
private static final Logger LOGGER = Loggers.getLogger("connection.tls");
6565

driver-core/src/main/com/mongodb/internal/connection/netty/NettyStreamFactoryFactory.java

+11
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
public final class NettyStreamFactoryFactory implements StreamFactoryFactory {
4848

4949
private final EventLoopGroup eventLoopGroup;
50+
private final boolean ownsEventLoopGroup;
5051
private final Class<? extends SocketChannel> socketChannelClass;
5152
private final ByteBufAllocator allocator;
5253
@Nullable
@@ -202,6 +203,15 @@ public StreamFactory create(final SocketSettings socketSettings, final SslSettin
202203
sslContext);
203204
}
204205

206+
@Override
207+
public void close() {
208+
if (ownsEventLoopGroup) {
209+
// ignore the returned Future. This is in line with MongoClient behavior to not block waiting for connections to be returned
210+
// to the pool
211+
eventLoopGroup.shutdownGracefully();
212+
}
213+
}
214+
205215
@Override
206216
public boolean equals(final Object o) {
207217
if (this == o) {
@@ -225,6 +235,7 @@ private NettyStreamFactoryFactory(final Builder builder) {
225235
allocator = builder.allocator == null ? ByteBufAllocator.DEFAULT : builder.allocator;
226236
socketChannelClass = builder.socketChannelClass == null ? NioSocketChannel.class : builder.socketChannelClass;
227237
eventLoopGroup = builder.eventLoopGroup == null ? new NioEventLoopGroup() : builder.eventLoopGroup;
238+
ownsEventLoopGroup = builder.eventLoopGroup == null;
228239
sslContext = builder.sslContext;
229240
inetAddressResolver = builder.inetAddressResolver;
230241
}

driver-reactive-streams/src/main/com/mongodb/reactivestreams/client/MongoClients.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,9 @@ public static MongoClient create(final MongoClientSettings settings, @Nullable f
127127
}
128128
StreamFactory streamFactory = getStreamFactory(streamFactoryFactory, settings, false);
129129
StreamFactory heartbeatStreamFactory = getStreamFactory(streamFactoryFactory, settings, true);
130-
AutoCloseable externalResourceCloser = streamFactoryFactory instanceof AutoCloseable ? (AutoCloseable) streamFactoryFactory : null;
131130
MongoDriverInformation wrappedMongoDriverInformation = wrapMongoDriverInformation(mongoDriverInformation);
132131
Cluster cluster = createCluster(settings, wrappedMongoDriverInformation, streamFactory, heartbeatStreamFactory);
133-
return new MongoClientImpl(settings, wrappedMongoDriverInformation, cluster, externalResourceCloser);
132+
return new MongoClientImpl(settings, wrappedMongoDriverInformation, cluster, streamFactoryFactory);
134133
}
135134

136135
/**

0 commit comments

Comments
 (0)