Skip to content

Make NettyStreamFactoryFactory implement AutoCloseable #1244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ public Builder socketChannelClass(final Class<? extends SocketChannel> socketCha
/**
* Sets the event loop group.
*
* <p>It is highly recommended to supply your own event loop group and manage its shutdown. Otherwise, the event
* loop group created by default will not be shutdown properly.</p>
* <p>The application is responsible for shutting down the provided {@code eventLoopGroup}</p>
*
* @param eventLoopGroup the event loop group that all channels created by this factory will be a part of
* @return this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ public AsynchronousSocketChannelStreamFactoryFactory(final InetAddressResolver i
public StreamFactory create(final SocketSettings socketSettings, final SslSettings sslSettings) {
return new AsynchronousSocketChannelStreamFactory(inetAddressResolver, socketSettings, sslSettings);
}

@Override
public void close() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
/**
* A factory of {@code StreamFactory} instances.
*/
public interface StreamFactoryFactory {
public interface StreamFactoryFactory extends AutoCloseable {

/**
* Create a {@code StreamFactory} with the given settings.
Expand All @@ -32,4 +32,7 @@ public interface StreamFactoryFactory {
* @return a stream factory that will apply the given settins
*/
StreamFactory create(SocketSettings socketSettings, SslSettings sslSettings);

@Override
void close();
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
/**
* A {@code StreamFactoryFactory} that supports TLS/SSL. The implementation supports asynchronous usage.
*/
public class TlsChannelStreamFactoryFactory implements StreamFactoryFactory, Closeable {
public class TlsChannelStreamFactoryFactory implements StreamFactoryFactory {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
public final class NettyStreamFactoryFactory implements StreamFactoryFactory {

private final EventLoopGroup eventLoopGroup;
private final boolean ownsEventLoopGroup;
private final Class<? extends SocketChannel> socketChannelClass;
private final ByteBufAllocator allocator;
@Nullable
Expand Down Expand Up @@ -202,6 +203,15 @@ public StreamFactory create(final SocketSettings socketSettings, final SslSettin
sslContext);
}

@Override
public void close() {
if (ownsEventLoopGroup) {
// ignore the returned Future. This is in line with MongoClient behavior to not block waiting for connections to be returned
// to the pool
eventLoopGroup.shutdownGracefully();
}
}

@Override
public boolean equals(final Object o) {
if (this == o) {
Expand All @@ -225,6 +235,7 @@ private NettyStreamFactoryFactory(final Builder builder) {
allocator = builder.allocator == null ? ByteBufAllocator.DEFAULT : builder.allocator;
socketChannelClass = builder.socketChannelClass == null ? NioSocketChannel.class : builder.socketChannelClass;
eventLoopGroup = builder.eventLoopGroup == null ? new NioEventLoopGroup() : builder.eventLoopGroup;
ownsEventLoopGroup = builder.eventLoopGroup == null;
sslContext = builder.sslContext;
inetAddressResolver = builder.inetAddressResolver;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,9 @@ public static MongoClient create(final MongoClientSettings settings, @Nullable f
}
StreamFactory streamFactory = getStreamFactory(streamFactoryFactory, settings, false);
StreamFactory heartbeatStreamFactory = getStreamFactory(streamFactoryFactory, settings, true);
AutoCloseable externalResourceCloser = streamFactoryFactory instanceof AutoCloseable ? (AutoCloseable) streamFactoryFactory : null;
MongoDriverInformation wrappedMongoDriverInformation = wrapMongoDriverInformation(mongoDriverInformation);
Cluster cluster = createCluster(settings, wrappedMongoDriverInformation, streamFactory, heartbeatStreamFactory);
return new MongoClientImpl(settings, wrappedMongoDriverInformation, cluster, externalResourceCloser);
return new MongoClientImpl(settings, wrappedMongoDriverInformation, cluster, streamFactoryFactory);
}

/**
Expand Down