Skip to content

Commit e1381d2

Browse files
committed
netty: finish 4.2 upgrade
1 parent 10012f8 commit e1381d2

File tree

5 files changed

+47
-48
lines changed

5 files changed

+47
-48
lines changed

modules/jooby-netty/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@
4949
</dependency>
5050

5151
<dependency>
52-
<groupId>io.netty.incubator</groupId>
53-
<artifactId>netty-incubator-transport-native-io_uring</artifactId>
54-
<version>0.0.26.Final</version>
52+
<groupId>io.netty</groupId>
53+
<artifactId>netty-transport-native-io_uring</artifactId>
54+
<version>${netty.version}</version>
5555
<optional>true</optional>
5656
</dependency>
5757

modules/jooby-netty/src/main/java/io/jooby/internal/netty/HttpChunkContentCompressor.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@ public HttpChunkContentCompressor(int compressionLevel) {
2121
@Override
2222
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
2323
throws Exception {
24-
if (msg instanceof ByteBuf) {
24+
if (msg instanceof ByteBuf buff) {
2525
// convert ByteBuf to HttpContent to make it work with compression. This is needed as we use
2626
// the
2727
// ChunkedWriteHandler to send files when compression is enabled.
28-
ByteBuf buff = (ByteBuf) msg;
2928
if (buff.isReadable()) {
3029
// We only encode non empty buffers, as empty buffers can be used for determining when
3130
// the content has been flushed and it confuses the HttpContentCompressor

modules/jooby-netty/src/main/java/io/jooby/internal/netty/NettyTransport.java

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,18 @@
88
import io.netty.bootstrap.ServerBootstrap;
99
import io.netty.channel.ChannelOption;
1010
import io.netty.channel.EventLoopGroup;
11-
import io.netty.channel.epoll.Epoll;
12-
import io.netty.channel.epoll.EpollChannelOption;
13-
import io.netty.channel.epoll.EpollEventLoopGroup;
14-
import io.netty.channel.epoll.EpollServerSocketChannel;
11+
import io.netty.channel.MultiThreadIoEventLoopGroup;
12+
import io.netty.channel.epoll.*;
1513
import io.netty.channel.kqueue.KQueue;
16-
import io.netty.channel.kqueue.KQueueEventLoopGroup;
14+
import io.netty.channel.kqueue.KQueueChannelOption;
15+
import io.netty.channel.kqueue.KQueueIoHandler;
1716
import io.netty.channel.kqueue.KQueueServerSocketChannel;
18-
import io.netty.channel.nio.NioEventLoopGroup;
17+
import io.netty.channel.nio.NioIoHandler;
1918
import io.netty.channel.socket.nio.NioServerSocketChannel;
20-
import io.netty.incubator.channel.uring.IOUring;
21-
import io.netty.incubator.channel.uring.IOUringChannelOption;
22-
import io.netty.incubator.channel.uring.IOUringEventLoopGroup;
23-
import io.netty.incubator.channel.uring.IOUringServerSocketChannel;
19+
import io.netty.channel.uring.IoUring;
20+
import io.netty.channel.uring.IoUringChannelOption;
21+
import io.netty.channel.uring.IoUringIoHandler;
22+
import io.netty.channel.uring.IoUringServerSocketChannel;
2423
import io.netty.util.concurrent.DefaultThreadFactory;
2524

2625
public abstract class NettyTransport {
@@ -36,7 +35,7 @@ public ServerBootstrap configure(EventLoopGroup acceptor, EventLoopGroup eventlo
3635
public abstract EventLoopGroup createEventLoop(int threads, String threadName, int ioRatio);
3736

3837
public static NettyTransport transport(ClassLoader loader) {
39-
if (isIOUring(loader)) {
38+
if (isIoUring(loader)) {
4039
return ioUring();
4140
} else if (isEpoll(loader)) {
4241
return epoll();
@@ -55,13 +54,13 @@ private static NettyTransport epoll() {
5554
}
5655

5756
private static NettyTransport ioUring() {
58-
return new IOUringTransport();
57+
return new IoUringTransport();
5958
}
6059

61-
private static boolean isIOUring(ClassLoader loader) {
60+
private static boolean isIoUring(ClassLoader loader) {
6261
try {
63-
loader.loadClass("io.netty.incubator.channel.uring.IOUring");
64-
return IOUring.isAvailable();
62+
loader.loadClass("io.netty.channel.uring.IoUring");
63+
return IoUring.isAvailable();
6564
} catch (ClassNotFoundException x) {
6665
return false;
6766
}
@@ -92,10 +91,8 @@ private static NettyTransport nio() {
9291
private static class JDKTransport extends NettyTransport {
9392
@Override
9493
public EventLoopGroup createEventLoop(int threads, String threadName, int ioRatio) {
95-
NioEventLoopGroup loopGroup =
96-
new NioEventLoopGroup(threads, new DefaultThreadFactory(threadName));
97-
loopGroup.setIoRatio(ioRatio);
98-
return loopGroup;
94+
return new MultiThreadIoEventLoopGroup(
95+
threads, new DefaultThreadFactory(threadName), NioIoHandler.newFactory());
9996
}
10097

10198
@Override
@@ -104,29 +101,29 @@ public ServerBootstrap configure(EventLoopGroup acceptor, EventLoopGroup eventlo
104101
}
105102
}
106103

107-
private static class IOUringTransport extends NettyTransport {
104+
private static class IoUringTransport extends NettyTransport {
108105

109106
@Override
110107
public EventLoopGroup createEventLoop(int threads, String threadName, int ioRatio) {
111-
IOUringEventLoopGroup loopGroup =
112-
new IOUringEventLoopGroup(threads, new DefaultThreadFactory(threadName + "-io-uring"));
113-
return loopGroup;
108+
return new MultiThreadIoEventLoopGroup(
109+
threads,
110+
new DefaultThreadFactory(threadName + "-io-uring"),
111+
IoUringIoHandler.newFactory());
114112
}
115113

116114
@Override
117115
public ServerBootstrap configure(EventLoopGroup acceptor, EventLoopGroup eventloop) {
118116
return super.configure(acceptor, eventloop)
119-
.channel(IOUringServerSocketChannel.class)
120-
.option(IOUringChannelOption.SO_REUSEPORT, true);
117+
.channel(IoUringServerSocketChannel.class)
118+
.option(IoUringChannelOption.SO_REUSEPORT, true);
121119
}
122120
}
123121

124122
private static class EpollTransport extends NettyTransport {
125123
@Override
126124
public EventLoopGroup createEventLoop(int threads, String threadName, int ioRatio) {
127-
EpollEventLoopGroup loopGroup =
128-
new EpollEventLoopGroup(threads, new DefaultThreadFactory(threadName + "-epoll"));
129-
return loopGroup;
125+
return new MultiThreadIoEventLoopGroup(
126+
threads, new DefaultThreadFactory(threadName + "-epoll"), EpollIoHandler.newFactory());
130127
}
131128

132129
@Override
@@ -140,15 +137,15 @@ public ServerBootstrap configure(EventLoopGroup acceptor, EventLoopGroup eventlo
140137
private static class KQueueTransport extends NettyTransport {
141138
@Override
142139
public EventLoopGroup createEventLoop(int threads, String threadName, int ioRatio) {
143-
KQueueEventLoopGroup loopGroup =
144-
new KQueueEventLoopGroup(threads, new DefaultThreadFactory(threadName + "-kqueue"));
145-
loopGroup.setIoRatio(ioRatio);
146-
return loopGroup;
140+
return new MultiThreadIoEventLoopGroup(
141+
threads, new DefaultThreadFactory(threadName + "-kqueue"), KQueueIoHandler.newFactory());
147142
}
148143

149144
@Override
150145
public ServerBootstrap configure(EventLoopGroup acceptor, EventLoopGroup eventloop) {
151-
return super.configure(acceptor, eventloop).channel(KQueueServerSocketChannel.class);
146+
return super.configure(acceptor, eventloop)
147+
.channel(KQueueServerSocketChannel.class)
148+
.option(KQueueChannelOption.SO_REUSEPORT, true);
152149
}
153150
}
154151
}

modules/jooby-netty/src/main/java/io/jooby/netty/NettyServer.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
import java.net.BindException;
1313
import java.util.Arrays;
1414
import java.util.List;
15-
import java.util.concurrent.ExecutionException;
16-
import java.util.concurrent.ExecutorService;
15+
import java.util.concurrent.*;
1716

1817
import javax.net.ssl.SSLContext;
1918

@@ -55,7 +54,7 @@ public class NettyServer extends Server.Base {
5554

5655
private EventLoopGroup acceptorloop;
5756
private EventLoopGroup eventloop;
58-
private EventLoopGroup dateLoop;
57+
private ScheduledExecutorService dateLoop;
5958
private ExecutorService worker;
6059

6160
private NettyDataBufferFactory bufferFactory;
@@ -142,7 +141,7 @@ public Server start(@NonNull Jooby application) {
142141

143142
/* Event loop: processing connections, parsing messages and doing engine's internal work */
144143
this.eventloop = transport.createEventLoop(options.getIoThreads(), "eventloop", _100);
145-
this.dateLoop = transport.createEventLoop(1, "date-service", _100);
144+
this.dateLoop = Executors.newSingleThreadScheduledExecutor();
146145
var dateService = new NettyDateService(dateLoop);
147146

148147
/* File data factory: */
@@ -235,19 +234,22 @@ public synchronized Server stop() {
235234
// only for jooby build where close events may take longer.
236235
NettyWebSocket.all.clear();
237236

238-
shutdown(acceptorloop);
239-
shutdown(eventloop);
240-
shutdown(dateLoop);
237+
// required after Netty 4.2
238+
shutdown(acceptorloop, 0);
239+
shutdown(eventloop, 2);
240+
if (dateLoop != null) {
241+
dateLoop.shutdown();
242+
}
241243
if (worker != null) {
242244
worker.shutdown();
243245
worker = null;
244246
}
245247
return this;
246248
}
247249

248-
private void shutdown(EventLoopGroup eventLoopGroup) {
250+
private void shutdown(EventLoopGroup eventLoopGroup, int quietPeriod) {
249251
if (eventLoopGroup != null) {
250-
eventLoopGroup.shutdownGracefully();
252+
eventLoopGroup.shutdownGracefully(quietPeriod, 15, TimeUnit.SECONDS);
251253
}
252254
}
253255

modules/jooby-netty/src/main/java/module-info.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424
requires io.netty.common;
2525
requires io.netty.buffer;
2626
requires io.netty.codec;
27+
requires io.netty.codec.compression;
2728
requires static io.netty.transport.classes.epoll;
2829
requires static io.netty.transport.classes.kqueue;
29-
requires static io.netty.incubator.transport.classes.io_uring;
30+
requires static io.netty.transport.classes.io_uring;
3031

3132
provides Server with
3233
NettyServer;

0 commit comments

Comments
 (0)