Skip to content

Commit 53192b1

Browse files
committed
Virtual thread for Network IO
1 parent 8d8f98c commit 53192b1

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2+
From: HaHaWTH <id_cn00@outlook.com>
3+
Date: Sun, 7 Apr 2024 20:52:33 +0800
4+
Subject: [PATCH] virtual-thread-for-network-io
5+
6+
7+
diff --git a/src/main/java/com/homomc/beast/BeastConfig.java b/src/main/java/com/homomc/beast/BeastConfig.java
8+
index 85e777c8dd89e99abe55aa4785546f4e6aec565b..98baf5ea5f3691c1ffeb92dd5f3d61a0c93b3622 100644
9+
--- a/src/main/java/com/homomc/beast/BeastConfig.java
10+
+++ b/src/main/java/com/homomc/beast/BeastConfig.java
11+
@@ -156,6 +156,12 @@ public class BeastConfig {
12+
virtualThreadForScheduler = getBoolean("virtual-thread-for-scheduler", false);
13+
}
14+
15+
+ public static boolean virtualThreadForNetworkIO;
16+
+
17+
+ private static void virtualThreadForNetworkIO() {
18+
+ virtualThreadForNetworkIO = getBoolean("virtual-thread-for-network-io", false);
19+
+ }
20+
+
21+
public static int portalTravelCacheLife;
22+
public static boolean portalTravelInstantExpire;
23+
private static void portalTravel() {
24+
diff --git a/src/main/java/net/minecraft/server/ServerConnection.java b/src/main/java/net/minecraft/server/ServerConnection.java
25+
index 1f3a753bc83e62873b70857bc5a3ae227ebbdeeb..3de9be40027b8c428e4ffd8252a6dcad64b07cff 100644
26+
--- a/src/main/java/net/minecraft/server/ServerConnection.java
27+
+++ b/src/main/java/net/minecraft/server/ServerConnection.java
28+
@@ -2,6 +2,7 @@ package net.minecraft.server;
29+
30+
import com.google.common.collect.Lists;
31+
import com.google.common.util.concurrent.ThreadFactoryBuilder;
32+
+import com.homomc.beast.virtualthreads.VirtualThreadService;
33+
import io.netty.bootstrap.ServerBootstrap;
34+
import io.netty.channel.Channel;
35+
import io.netty.channel.ChannelException;
36+
@@ -23,6 +24,8 @@ import java.net.InetAddress;
37+
import java.util.Collections;
38+
import java.util.Iterator;
39+
import java.util.List;
40+
+import java.util.concurrent.ThreadFactory;
41+
+
42+
import org.apache.logging.log4j.LogManager;
43+
import org.apache.logging.log4j.Logger;
44+
45+
@@ -31,6 +34,14 @@ public class ServerConnection {
46+
private static final Logger e = LogManager.getLogger();
47+
public static final LazyInitVar<NioEventLoopGroup> a = new LazyInitVar() {
48+
protected NioEventLoopGroup a() {
49+
+ if (VirtualThreadService.getJavaMajorVersion() >= VirtualThreadService.minimumJavaMajorVersionWithoutFeaturePreview && com.homomc.beast.BeastConfig.virtualThreadForNetworkIO) {
50+
+ try {
51+
+ ThreadFactory threadFactory = VirtualThreadService.get().createFactory();
52+
+ return new NioEventLoopGroup(0, (new ThreadFactoryBuilder()).setNameFormat("Netty Server IO #%d").setDaemon(true).setThreadFactory(threadFactory).build());
53+
+ } catch (Exception ex) {
54+
+ e.error("Failed to create Virtual Thread executor! Fallback to default executor.");
55+
+ }
56+
+ }
57+
return new NioEventLoopGroup(0, (new ThreadFactoryBuilder()).setNameFormat("Netty Server IO #%d").setDaemon(true).build());
58+
}
59+
60+
@@ -40,6 +51,14 @@ public class ServerConnection {
61+
};
62+
public static final LazyInitVar<EpollEventLoopGroup> b = new LazyInitVar() {
63+
protected EpollEventLoopGroup a() {
64+
+ if (VirtualThreadService.getJavaMajorVersion() >= VirtualThreadService.minimumJavaMajorVersionWithoutFeaturePreview && com.homomc.beast.BeastConfig.virtualThreadForNetworkIO) {
65+
+ try {
66+
+ ThreadFactory threadFactory = VirtualThreadService.get().createFactory();
67+
+ return new EpollEventLoopGroup(0, (new ThreadFactoryBuilder()).setNameFormat("Netty Epoll Server IO #%d").setDaemon(true).setThreadFactory(threadFactory).build());
68+
+ } catch (Exception ex) {
69+
+ e.error("Failed to create Virtual Thread executor! Fallback to default executor.");
70+
+ }
71+
+ }
72+
return new EpollEventLoopGroup(0, (new ThreadFactoryBuilder()).setNameFormat("Netty Epoll Server IO #%d").setDaemon(true).build());
73+
}
74+
75+
@@ -49,6 +68,14 @@ public class ServerConnection {
76+
};
77+
public static final LazyInitVar<LocalEventLoopGroup> c = new LazyInitVar() {
78+
protected LocalEventLoopGroup a() {
79+
+ if (VirtualThreadService.getJavaMajorVersion() >= VirtualThreadService.minimumJavaMajorVersionWithoutFeaturePreview && com.homomc.beast.BeastConfig.virtualThreadForNetworkIO) {
80+
+ try {
81+
+ ThreadFactory threadFactory = VirtualThreadService.get().createFactory();
82+
+ return new LocalEventLoopGroup(0, (new ThreadFactoryBuilder()).setNameFormat("Netty Local Server IO #%d").setDaemon(true).setThreadFactory(threadFactory).build());
83+
+ } catch (Exception ex) {
84+
+ e.error("Failed to create Virtual Thread executor! Fallback to default executor.");
85+
+ }
86+
+ }
87+
return new LocalEventLoopGroup(0, (new ThreadFactoryBuilder()).setNameFormat("Netty Local Server IO #%d").setDaemon(true).build());
88+
}
89+

0 commit comments

Comments
 (0)