Skip to content

Commit

Permalink
NIO domain socket support (#10852)
Browse files Browse the repository at this point in the history
* fix(deps): update netty monorepo to v4.1.110.final

* NIO domain socket support
From netty/netty#13965

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
  • Loading branch information
yawkat and renovate[bot] authored May 23, 2024
1 parent 59f6a60 commit 38997d3
Show file tree
Hide file tree
Showing 14 changed files with 166 additions and 264 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,11 @@

import io.micronaut.context.annotation.BootstrapContextCompatible;
import io.micronaut.context.annotation.Primary;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.util.ArgumentUtils;
import io.micronaut.http.netty.configuration.NettyGlobalConfiguration;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.ServerChannel;
import io.netty.channel.socket.ServerSocketChannel;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.unix.ServerDomainSocketChannel;
import io.netty.util.ResourceLeakDetector;
import jakarta.inject.Inject;
import jakarta.inject.Named;
Expand Down Expand Up @@ -106,48 +101,16 @@ public EventLoopGroup createEventLoopGroup(int threads, @Nullable ThreadFactory
return nativeFactory.createEventLoopGroup(threads, threadFactory, ioRatio);
}

@Override
public Class<? extends ServerSocketChannel> serverSocketChannelClass() {
return nativeFactory.serverSocketChannelClass();
}

@Override
public Class<? extends ServerDomainSocketChannel> domainServerSocketChannelClass() throws UnsupportedOperationException {
return nativeFactory.domainServerSocketChannelClass();
}

@Override
public Class<? extends Channel> channelClass(NettyChannelType type) throws UnsupportedOperationException {
return nativeFactory.channelClass(type);
}

@NonNull
@Override
public Class<? extends ServerSocketChannel> serverSocketChannelClass(EventLoopGroupConfiguration configuration) {
return getFactory(configuration).serverSocketChannelClass(configuration);
}

@NonNull
@Override
public Class<? extends ServerDomainSocketChannel> domainServerSocketChannelClass(EventLoopGroupConfiguration configuration) {
return getFactory(configuration).domainServerSocketChannelClass(configuration);
}

@Override
public Class<? extends Channel> channelClass(NettyChannelType type, @Nullable EventLoopGroupConfiguration configuration) {
return getFactory(configuration).channelClass(type, configuration);
}

@Override
public ServerSocketChannel serverSocketChannelInstance(EventLoopGroupConfiguration configuration) {
return getFactory(configuration).serverSocketChannelInstance(configuration);
}

@Override
public ServerChannel domainServerSocketChannelInstance(@Nullable EventLoopGroupConfiguration configuration) {
return getFactory(configuration).domainServerSocketChannelInstance(configuration);
}

@Override
public Channel channelInstance(NettyChannelType type, @Nullable EventLoopGroupConfiguration configuration) {
return getFactory(configuration).channelInstance(type, configuration);
Expand All @@ -158,12 +121,6 @@ public Channel channelInstance(NettyChannelType type, EventLoopGroupConfiguratio
return getFactory(configuration).channelInstance(type, configuration, parent, fd);
}

@NonNull
@Override
public Class<? extends SocketChannel> clientSocketChannelClass(@Nullable EventLoopGroupConfiguration configuration) {
return getFactory(configuration).clientSocketChannelClass(configuration);
}

private EventLoopGroupFactory getFactory(@Nullable EventLoopGroupConfiguration configuration) {
if (configuration != null && configuration.isPreferNativeTransport()) {
return this.nativeFactory;
Expand All @@ -172,10 +129,4 @@ private EventLoopGroupFactory getFactory(@Nullable EventLoopGroupConfiguration c
}
}

@NonNull
@Override
public SocketChannel clientSocketChannelInstance(@Nullable EventLoopGroupConfiguration configuration) {
return getFactory(configuration).clientSocketChannelInstance(configuration);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,17 @@
import io.micronaut.context.annotation.BootstrapContextCompatible;
import io.micronaut.context.annotation.Requires;
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.annotation.Order;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.ServerChannel;
import io.netty.channel.epoll.Epoll;
import io.netty.channel.epoll.EpollDatagramChannel;
import io.netty.channel.epoll.EpollDomainSocketChannel;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.epoll.EpollServerDomainSocketChannel;
import io.netty.channel.epoll.EpollServerSocketChannel;
import io.netty.channel.epoll.EpollSocketChannel;
import io.netty.channel.socket.ServerSocketChannel;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.unix.ServerDomainSocketChannel;
import jakarta.inject.Named;
import jakarta.inject.Singleton;
import org.slf4j.Logger;
Expand Down Expand Up @@ -82,51 +77,6 @@ public EventLoopGroup createEventLoopGroup(int threads, Executor executor, @Null
return new EpollEventLoopGroup(threads, executor);
}

/**
* Returns the server channel class.
*
* @return EpollServerSocketChannel.
*/
@Override
public Class<? extends ServerSocketChannel> serverSocketChannelClass() {
return EpollServerSocketChannel.class;
}

@Override
public Class<? extends ServerDomainSocketChannel> domainServerSocketChannelClass() throws UnsupportedOperationException {
try {
return EpollServerDomainSocketChannel.class;
} catch (NoClassDefFoundError e) {
throw new UnsupportedOperationException(e);
}
}

@NonNull
@Override
public EpollServerSocketChannel serverSocketChannelInstance(@Nullable EventLoopGroupConfiguration configuration) {
return new EpollServerSocketChannel();
}

@Override
public ServerChannel domainServerSocketChannelInstance(@Nullable EventLoopGroupConfiguration configuration) {
try {
return new EpollServerDomainSocketChannel();
} catch (NoClassDefFoundError e) {
throw new UnsupportedOperationException(e);
}
}

@NonNull
@Override
public Class<? extends SocketChannel> clientSocketChannelClass(@Nullable EventLoopGroupConfiguration configuration) {
return EpollSocketChannel.class;
}

@Override
public SocketChannel clientSocketChannelInstance(EventLoopGroupConfiguration configuration) {
return new EpollSocketChannel();
}

@Override
public boolean isNative() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,25 @@ default EventLoopGroup createEventLoopGroup(int threads, @Nullable Integer ioRat
* Returns the server channel class.
*
* @return A ServerChannelClass.
* @deprecated Use {@link #channelClass(NettyChannelType)} instead
*/
@NonNull Class<? extends ServerSocketChannel> serverSocketChannelClass();
@Deprecated(since = "4.5.0", forRemoval = true)
@NonNull
default Class<? extends ServerSocketChannel> serverSocketChannelClass() {
return channelClass(NettyChannelType.SERVER_SOCKET).asSubclass(ServerSocketChannel.class);
}

/**
* Returns the domain socket server channel class.
*
* @return A ServerDomainSocketChannel class.
* @throws UnsupportedOperationException if domain sockets are not supported.
* @deprecated Use {@link #channelClass(NettyChannelType)} instead
*/
@NonNull
@Deprecated(since = "4.5.0", forRemoval = true)
default Class<? extends ServerDomainSocketChannel> domainServerSocketChannelClass() throws UnsupportedOperationException {
throw new UnsupportedOperationException("Domain server socket channels not supported by this transport");
return channelClass(NettyChannelType.DOMAIN_SERVER_SOCKET).asSubclass(ServerDomainSocketChannel.class);
}

/**
Expand All @@ -143,9 +150,11 @@ default Class<? extends Channel> channelClass(NettyChannelType type) throws Unsu
*
* @param configuration The configuration
* @return A ServerSocketChannel class.
* @deprecated Use {@link #channelClass(NettyChannelType, EventLoopGroupConfiguration)} instead
*/
@Deprecated(since = "4.5.0", forRemoval = true)
default @NonNull Class<? extends ServerSocketChannel> serverSocketChannelClass(@Nullable EventLoopGroupConfiguration configuration) {
return serverSocketChannelClass();
return channelClass(NettyChannelType.SERVER_SOCKET, configuration).asSubclass(ServerSocketChannel.class);
}

/**
Expand All @@ -154,9 +163,11 @@ default Class<? extends Channel> channelClass(NettyChannelType type) throws Unsu
* @param configuration The configuration
* @return A ServerDomainSocketChannel implementation.
* @throws UnsupportedOperationException if domain sockets are not supported.
* @deprecated Use {@link #channelClass(NettyChannelType, EventLoopGroupConfiguration)} instead
*/
@Deprecated(since = "4.5.0", forRemoval = true)
default @NonNull Class<? extends ServerDomainSocketChannel> domainServerSocketChannelClass(@Nullable EventLoopGroupConfiguration configuration) {
return domainServerSocketChannelClass();
return channelClass(NettyChannelType.DOMAIN_SERVER_SOCKET, configuration).asSubclass(ServerDomainSocketChannel.class);
}

/**
Expand All @@ -181,13 +192,11 @@ default Class<? extends Channel> channelClass(NettyChannelType type) throws Unsu
*
* @param configuration The configuration
* @return A ServerSocketChannel instance.
* @deprecated Use {@link #channelInstance(NettyChannelType, EventLoopGroupConfiguration)} instead
*/
@Deprecated(since = "4.5.0", forRemoval = true)
default @NonNull ServerSocketChannel serverSocketChannelInstance(@Nullable EventLoopGroupConfiguration configuration) {
try {
return serverSocketChannelClass(configuration).getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new RuntimeException("Cannot instantiate server socket channel instance");
}
return (ServerSocketChannel) channelInstance(NettyChannelType.SERVER_SOCKET, configuration);
}

/**
Expand All @@ -196,13 +205,11 @@ default Class<? extends Channel> channelClass(NettyChannelType type) throws Unsu
* @param configuration The configuration
* @return A ServerDomainSocketChannel implementation.
* @throws UnsupportedOperationException if domain sockets are not supported.
* @deprecated Use {@link #channelInstance(NettyChannelType, EventLoopGroupConfiguration)} instead
*/
@Deprecated(since = "4.5.0", forRemoval = true)
default @NonNull ServerChannel domainServerSocketChannelInstance(@Nullable EventLoopGroupConfiguration configuration) {
try {
return domainServerSocketChannelClass(configuration).getDeclaredConstructor().newInstance();
} catch (ReflectiveOperationException e) {
throw new RuntimeException("Cannot instantiate server socket channel instance", e);
}
return (ServerChannel) channelInstance(NettyChannelType.DOMAIN_SERVER_SOCKET, configuration);
}

/**
Expand Down Expand Up @@ -256,21 +263,24 @@ default Class<? extends Channel> channelClass(NettyChannelType type) throws Unsu
*
* @param configuration The configuration
* @return A SocketChannel class.
* @deprecated Use {@link #channelClass(NettyChannelType, EventLoopGroupConfiguration)} instead
*/
@NonNull Class<? extends SocketChannel> clientSocketChannelClass(@Nullable EventLoopGroupConfiguration configuration);
@Deprecated(since = "4.5.0", forRemoval = true)
@NonNull
default Class<? extends SocketChannel> clientSocketChannelClass(@Nullable EventLoopGroupConfiguration configuration) {
return channelClass(NettyChannelType.CLIENT_SOCKET, configuration).asSubclass(SocketChannel.class);
}

/**
* Returns the client channel class instance.
*
* @param configuration The configuration
* @return A SocketChannel instance.
* @deprecated Use {@link #channelInstance(NettyChannelType, EventLoopGroupConfiguration)} instead
*/
@Deprecated(since = "4.5.0", forRemoval = true)
default @NonNull SocketChannel clientSocketChannelInstance(@Nullable EventLoopGroupConfiguration configuration) {
try {
return clientSocketChannelClass(configuration).getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new RuntimeException("Cannot instantiate server socket channel instance");
}
return (SocketChannel) channelInstance(NettyChannelType.CLIENT_SOCKET, configuration);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@
import io.micronaut.context.annotation.BootstrapContextCompatible;
import io.micronaut.context.annotation.Requires;
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.annotation.Order;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.socket.ServerSocketChannel;
import io.netty.channel.socket.SocketChannel;
import io.netty.incubator.channel.uring.IOUring;
import io.netty.incubator.channel.uring.IOUringDatagramChannel;
import io.netty.incubator.channel.uring.IOUringEventLoopGroup;
Expand Down Expand Up @@ -77,33 +74,6 @@ public EventLoopGroup createEventLoopGroup(int threads, Executor executor, @Null
return new IOUringEventLoopGroup(threads, executor);
}

/**
* Returns the server channel class.
*
* @return IOUringServerSocketChannel.
*/
@Override
public Class<? extends ServerSocketChannel> serverSocketChannelClass() {
return IOUringServerSocketChannel.class;
}

@NonNull
@Override
public IOUringServerSocketChannel serverSocketChannelInstance(@Nullable EventLoopGroupConfiguration configuration) {
return new IOUringServerSocketChannel();
}

@NonNull
@Override
public Class<? extends SocketChannel> clientSocketChannelClass(@Nullable EventLoopGroupConfiguration configuration) {
return IOUringSocketChannel.class;
}

@Override
public SocketChannel clientSocketChannelInstance(EventLoopGroupConfiguration configuration) {
return new IOUringSocketChannel();
}

@Override
public boolean isNative() {
return true;
Expand Down
Loading

0 comments on commit 38997d3

Please sign in to comment.