Skip to content

Commit c69cefd

Browse files
Support netty 4.2.1, add system property for opt-in io_uring (#897)
* Support netty 4.2.1, add system property for opt-in io_uring * check for io.netty.channel.MultiThreadIoEventLoopGroup instead * use Boolean.getBoolean instead
1 parent 30e2bad commit c69cefd

File tree

5 files changed

+37
-28
lines changed

5 files changed

+37
-28
lines changed

gradle/libs.versions.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ cloudburstnbt = "3.0.0.Final"
77
slf4j = "2.0.9"
88
math = "2.0"
99
fastutil-maps = "8.5.3"
10-
netty = "4.1.103.Final"
11-
netty-io_uring = "0.0.24.Final"
10+
netty = "4.2.1.Final"
1211
gson = "2.11.0"
1312
minecraftauth = "4.1.1"
1413
checkerframework = "3.42.0"
@@ -37,7 +36,6 @@ fastutil-int2object-maps = { module = "com.nukkitx.fastutil:fastutil-int-object-
3736
fastutil-int2int-maps = { module = "com.nukkitx.fastutil:fastutil-int-int-maps", version.ref = "fastutil-maps" }
3837

3938
netty-all = { module = "io.netty:netty-all", version.ref = "netty" }
40-
netty-incubator-transport-native-io_uring = { module = "io.netty.incubator:netty-incubator-transport-native-io_uring", version.ref = "netty-io_uring" }
4139

4240
gson = { module = "com.google.code.gson:gson", version.ref = "gson" }
4341

@@ -57,4 +55,3 @@ lombok = { module = "io.freefair.gradle:lombok-plugin", version.ref = "lombok-pl
5755
adventure = ["adventure-text-serializer-gson", "adventure-text-serializer-json-legacy-impl"]
5856
math = ["math-api", "math-immutable"]
5957
fastutil = ["fastutil-object2int-maps", "fastutil-int2object-maps", "fastutil-int2int-maps"]
60-
netty = ["netty-all", "netty-incubator-transport-native-io_uring"]

protocol/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ dependencies {
2929
api(libs.bundles.fastutil)
3030

3131
// Netty
32-
api(libs.bundles.netty)
32+
api(libs.netty.all)
3333

3434
// Checker Framework
3535
api(libs.checkerframework.qual)

protocol/src/main/java/org/geysermc/mcprotocollib/network/helper/TransportHelper.java

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,39 @@
22

33
import io.netty.channel.ChannelFactory;
44
import io.netty.channel.EventLoopGroup;
5+
import io.netty.channel.MultiThreadIoEventLoopGroup;
56
import io.netty.channel.epoll.Epoll;
67
import io.netty.channel.epoll.EpollDatagramChannel;
78
import io.netty.channel.epoll.EpollEventLoopGroup;
9+
import io.netty.channel.epoll.EpollIoHandler;
810
import io.netty.channel.epoll.EpollServerSocketChannel;
911
import io.netty.channel.epoll.EpollSocketChannel;
1012
import io.netty.channel.kqueue.KQueue;
1113
import io.netty.channel.kqueue.KQueueDatagramChannel;
1214
import io.netty.channel.kqueue.KQueueEventLoopGroup;
15+
import io.netty.channel.kqueue.KQueueIoHandler;
1316
import io.netty.channel.kqueue.KQueueServerSocketChannel;
1417
import io.netty.channel.kqueue.KQueueSocketChannel;
1518
import io.netty.channel.nio.NioEventLoopGroup;
19+
import io.netty.channel.nio.NioIoHandler;
1620
import io.netty.channel.socket.DatagramChannel;
1721
import io.netty.channel.socket.ServerSocketChannel;
1822
import io.netty.channel.socket.SocketChannel;
1923
import io.netty.channel.socket.nio.NioDatagramChannel;
2024
import io.netty.channel.socket.nio.NioServerSocketChannel;
2125
import io.netty.channel.socket.nio.NioSocketChannel;
22-
import io.netty.incubator.channel.uring.IOUring;
23-
import io.netty.incubator.channel.uring.IOUringDatagramChannel;
24-
import io.netty.incubator.channel.uring.IOUringEventLoopGroup;
25-
import io.netty.incubator.channel.uring.IOUringServerSocketChannel;
26-
import io.netty.incubator.channel.uring.IOUringSocketChannel;
26+
import io.netty.channel.uring.IoUring;
27+
import io.netty.channel.uring.IoUringDatagramChannel;
28+
import io.netty.channel.uring.IoUringIoHandler;
29+
import io.netty.channel.uring.IoUringServerSocketChannel;
30+
import io.netty.channel.uring.IoUringSocketChannel;
2731

2832
import java.util.concurrent.ThreadFactory;
29-
import java.util.function.Function;
33+
import java.util.function.BiFunction;
3034

3135
public class TransportHelper {
3236
public static final TransportHelper.TransportType TRANSPORT_TYPE = TransportHelper.determineTransportMethod();
37+
public static final boolean NEW_NETTY = isClassAvailable("io.netty.channel.MultiThreadIoEventLoopGroup");
3338

3439
public enum TransportMethod {
3540
NIO, EPOLL, KQUEUE, IO_URING
@@ -42,24 +47,28 @@ public record TransportType(TransportMethod method,
4247
ChannelFactory<? extends SocketChannel> socketChannelFactory,
4348
Class<? extends DatagramChannel> datagramChannelClass,
4449
ChannelFactory<? extends DatagramChannel> datagramChannelFactory,
45-
Function<ThreadFactory, EventLoopGroup> eventLoopGroupFactory,
50+
BiFunction<Integer, ThreadFactory, EventLoopGroup> eventLoopGroupFactory,
4651
boolean supportsTcpFastOpenServer,
4752
boolean supportsTcpFastOpenClient) {
4853
}
4954

55+
@SuppressWarnings("deprecation")
5056
private static TransportType determineTransportMethod() {
51-
if (isClassAvailable("io.netty.incubator.channel.uring.IOUring") && IOUring.isAvailable()) {
57+
if (isClassAvailable("io.netty.channel.uring.IoUring")
58+
&& IoUring.isAvailable()
59+
&& Boolean.getBoolean("Mcpl.io_uring")
60+
) {
5261
return new TransportType(
5362
TransportMethod.IO_URING,
54-
IOUringServerSocketChannel.class,
55-
IOUringServerSocketChannel::new,
56-
IOUringSocketChannel.class,
57-
IOUringSocketChannel::new,
58-
IOUringDatagramChannel.class,
59-
IOUringDatagramChannel::new,
60-
factory -> new IOUringEventLoopGroup(0, factory),
61-
IOUring.isTcpFastOpenServerSideAvailable(),
62-
IOUring.isTcpFastOpenClientSideAvailable()
63+
IoUringServerSocketChannel.class,
64+
IoUringServerSocketChannel::new,
65+
IoUringSocketChannel.class,
66+
IoUringSocketChannel::new,
67+
IoUringDatagramChannel.class,
68+
IoUringDatagramChannel::new,
69+
(threads, factory) -> new MultiThreadIoEventLoopGroup(threads, factory, IoUringIoHandler.newFactory()),
70+
IoUring.isTcpFastOpenServerSideAvailable(),
71+
IoUring.isTcpFastOpenClientSideAvailable()
6372
);
6473
}
6574

@@ -72,7 +81,8 @@ private static TransportType determineTransportMethod() {
7281
EpollSocketChannel::new,
7382
EpollDatagramChannel.class,
7483
EpollDatagramChannel::new,
75-
factory -> new EpollEventLoopGroup(0, factory),
84+
NEW_NETTY ? (threads, factory) ->
85+
new MultiThreadIoEventLoopGroup(threads, factory, EpollIoHandler.newFactory()) : EpollEventLoopGroup::new,
7686
Epoll.isTcpFastOpenServerSideAvailable(),
7787
Epoll.isTcpFastOpenClientSideAvailable()
7888
);
@@ -87,7 +97,8 @@ private static TransportType determineTransportMethod() {
8797
KQueueSocketChannel::new,
8898
KQueueDatagramChannel.class,
8999
KQueueDatagramChannel::new,
90-
factory -> new KQueueEventLoopGroup(0, factory),
100+
NEW_NETTY ? (threads, factory) ->
101+
new MultiThreadIoEventLoopGroup(threads, factory, KQueueIoHandler.newFactory()) : KQueueEventLoopGroup::new,
91102
KQueue.isTcpFastOpenServerSideAvailable(),
92103
KQueue.isTcpFastOpenClientSideAvailable()
93104
);
@@ -101,7 +112,8 @@ private static TransportType determineTransportMethod() {
101112
NioSocketChannel::new,
102113
NioDatagramChannel.class,
103114
NioDatagramChannel::new,
104-
factory -> new NioEventLoopGroup(0, factory),
115+
NEW_NETTY ? (threads, factory) ->
116+
new MultiThreadIoEventLoopGroup(threads, factory, NioIoHandler.newFactory()) : NioEventLoopGroup::new,
105117
false,
106118
false
107119
);

protocol/src/main/java/org/geysermc/mcprotocollib/network/server/NetworkServer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ protected ChannelFactory<? extends ServerChannel> getChannelFactory() {
8787
}
8888

8989
protected EventLoopGroup createBossEventLoopGroup() {
90-
return TransportHelper.TRANSPORT_TYPE.eventLoopGroupFactory().apply(null);
90+
return TransportHelper.TRANSPORT_TYPE.eventLoopGroupFactory().apply(0, null);
9191
}
9292

9393
protected EventLoopGroup createWorkerEventLoopGroup() {
94-
return TransportHelper.TRANSPORT_TYPE.eventLoopGroupFactory().apply(null);
94+
return TransportHelper.TRANSPORT_TYPE.eventLoopGroupFactory().apply(0, null);
9595
}
9696

9797
protected void setOptions(ServerBootstrap bootstrap) {

protocol/src/main/java/org/geysermc/mcprotocollib/network/session/ClientNetworkSession.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ private static void createEventLoopGroup() {
127127
return;
128128
}
129129

130-
EVENT_LOOP_GROUP = TransportHelper.TRANSPORT_TYPE.eventLoopGroupFactory().apply(newThreadFactory());
130+
EVENT_LOOP_GROUP = TransportHelper.TRANSPORT_TYPE.eventLoopGroupFactory().apply(0, newThreadFactory());
131131

132132
Runtime.getRuntime().addShutdownHook(new Thread(
133133
() -> EVENT_LOOP_GROUP.shutdownGracefully(SHUTDOWN_QUIET_PERIOD_MS, SHUTDOWN_TIMEOUT_MS, TimeUnit.MILLISECONDS)));

0 commit comments

Comments
 (0)