Skip to content

Commit c6413a9

Browse files
authored
support epoll on linux, alternative impl of PR#4493 (#5680)
1 parent e234a89 commit c6413a9

File tree

3 files changed

+77
-18
lines changed

3 files changed

+77
-18
lines changed

dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyClient.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,17 @@
3434
import io.netty.channel.ChannelFuture;
3535
import io.netty.channel.ChannelInitializer;
3636
import io.netty.channel.ChannelOption;
37-
import io.netty.channel.nio.NioEventLoopGroup;
38-
import io.netty.channel.socket.nio.NioSocketChannel;
37+
import io.netty.channel.EventLoopGroup;
38+
import io.netty.channel.socket.SocketChannel;
3939
import io.netty.handler.proxy.Socks5ProxyHandler;
4040
import io.netty.handler.timeout.IdleStateHandler;
41-
import io.netty.util.concurrent.DefaultThreadFactory;
4241

4342
import java.net.InetSocketAddress;
4443

4544
import static java.util.concurrent.TimeUnit.MILLISECONDS;
4645
import static org.apache.dubbo.common.constants.CommonConstants.SSL_ENABLED_KEY;
46+
import static org.apache.dubbo.remoting.transport.netty4.NettyEventLoopFactory.eventLoopGroup;
47+
import static org.apache.dubbo.remoting.transport.netty4.NettyEventLoopFactory.socketChannelClass;
4748

4849
/**
4950
* NettyClient.
@@ -54,7 +55,7 @@ public class NettyClient extends AbstractClient {
5455
/**
5556
* netty client bootstrap
5657
*/
57-
private static final NioEventLoopGroup nioEventLoopGroup = new NioEventLoopGroup(Constants.DEFAULT_IO_THREADS, new DefaultThreadFactory("NettyClientWorker", true));
58+
private static final EventLoopGroup eventLoopGroup = eventLoopGroup(Constants.DEFAULT_IO_THREADS, "NettyClientWorker");
5859

5960
private static final String SOCKS_PROXY_HOST = "socksProxyHost";
6061

@@ -90,18 +91,18 @@ public NettyClient(final URL url, final ChannelHandler handler) throws RemotingE
9091
protected void doOpen() throws Throwable {
9192
final NettyClientHandler nettyClientHandler = new NettyClientHandler(getUrl(), this);
9293
bootstrap = new Bootstrap();
93-
bootstrap.group(nioEventLoopGroup)
94+
bootstrap.group(eventLoopGroup)
9495
.option(ChannelOption.SO_KEEPALIVE, true)
9596
.option(ChannelOption.TCP_NODELAY, true)
9697
.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
9798
//.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, getTimeout())
98-
.channel(NioSocketChannel.class);
99+
.channel(socketChannelClass());
99100

100101
bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, Math.max(3000, getConnectTimeout()));
101-
bootstrap.handler(new ChannelInitializer() {
102+
bootstrap.handler(new ChannelInitializer<SocketChannel>() {
102103

103104
@Override
104-
protected void initChannel(Channel ch) throws Exception {
105+
protected void initChannel(SocketChannel ch) throws Exception {
105106
int heartbeatInterval = UrlUtils.getHeartbeat(getUrl());
106107

107108
if (getUrl().getParameter(SSL_ENABLED_KEY, false)) {
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.dubbo.remoting.transport.netty4;
18+
19+
import org.apache.dubbo.common.config.Configuration;
20+
import org.apache.dubbo.rpc.model.ApplicationModel;
21+
22+
import io.netty.channel.EventLoopGroup;
23+
import io.netty.channel.epoll.Epoll;
24+
import io.netty.channel.epoll.EpollEventLoopGroup;
25+
import io.netty.channel.epoll.EpollServerSocketChannel;
26+
import io.netty.channel.epoll.EpollSocketChannel;
27+
import io.netty.channel.nio.NioEventLoopGroup;
28+
import io.netty.channel.socket.ServerSocketChannel;
29+
import io.netty.channel.socket.SocketChannel;
30+
import io.netty.channel.socket.nio.NioServerSocketChannel;
31+
import io.netty.channel.socket.nio.NioSocketChannel;
32+
import io.netty.util.concurrent.DefaultThreadFactory;
33+
34+
import java.util.concurrent.ThreadFactory;
35+
36+
public class NettyEventLoopFactory {
37+
public static EventLoopGroup eventLoopGroup(int threads, String threadFactoryName) {
38+
ThreadFactory threadFactory = new DefaultThreadFactory(threadFactoryName, true);
39+
return shouldEpoll() ? new EpollEventLoopGroup(threads, threadFactory) :
40+
new NioEventLoopGroup(threads, threadFactory);
41+
}
42+
43+
public static Class<? extends SocketChannel> socketChannelClass() {
44+
return shouldEpoll() ? EpollSocketChannel.class : NioSocketChannel.class;
45+
}
46+
47+
public static Class<? extends ServerSocketChannel> serverSocketChannelClass() {
48+
return shouldEpoll() ? EpollServerSocketChannel.class : NioServerSocketChannel.class;
49+
}
50+
51+
private static boolean shouldEpoll() {
52+
Configuration configuration = ApplicationModel.getEnvironment().getConfiguration();
53+
if (configuration.getBoolean("netty.epoll.enable", false)) {
54+
String osName = configuration.getString("os.name");
55+
return osName.toLowerCase().contains("linux") && Epoll.isAvailable();
56+
}
57+
58+
return false;
59+
}
60+
}

dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyServer.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.dubbo.remoting.transport.netty4;
1818

19+
import io.netty.channel.socket.SocketChannel;
1920
import org.apache.dubbo.common.URL;
2021
import org.apache.dubbo.common.logger.Logger;
2122
import org.apache.dubbo.common.logger.LoggerFactory;
@@ -36,11 +37,7 @@
3637
import io.netty.channel.ChannelInitializer;
3738
import io.netty.channel.ChannelOption;
3839
import io.netty.channel.EventLoopGroup;
39-
import io.netty.channel.nio.NioEventLoopGroup;
40-
import io.netty.channel.socket.nio.NioServerSocketChannel;
41-
import io.netty.channel.socket.nio.NioSocketChannel;
4240
import io.netty.handler.timeout.IdleStateHandler;
43-
import io.netty.util.concurrent.DefaultThreadFactory;
4441

4542
import java.net.InetSocketAddress;
4643
import java.util.Collection;
@@ -89,21 +86,22 @@ public NettyServer(URL url, ChannelHandler handler) throws RemotingException {
8986
protected void doOpen() throws Throwable {
9087
bootstrap = new ServerBootstrap();
9188

92-
bossGroup = new NioEventLoopGroup(1, new DefaultThreadFactory("NettyServerBoss", true));
93-
workerGroup = new NioEventLoopGroup(getUrl().getPositiveParameter(IO_THREADS_KEY, Constants.DEFAULT_IO_THREADS),
94-
new DefaultThreadFactory("NettyServerWorker", true));
89+
bossGroup = NettyEventLoopFactory.eventLoopGroup(1, "NettyServerBoss");
90+
workerGroup = NettyEventLoopFactory.eventLoopGroup(
91+
getUrl().getPositiveParameter(IO_THREADS_KEY, Constants.DEFAULT_IO_THREADS),
92+
"NettyServerWorker");
9593

9694
final NettyServerHandler nettyServerHandler = new NettyServerHandler(getUrl(), this);
9795
channels = nettyServerHandler.getChannels();
9896

9997
bootstrap.group(bossGroup, workerGroup)
100-
.channel(NioServerSocketChannel.class)
98+
.channel(NettyEventLoopFactory.serverSocketChannelClass())
10199
.option(ChannelOption.SO_REUSEADDR, Boolean.TRUE)
102100
.childOption(ChannelOption.TCP_NODELAY, Boolean.TRUE)
103101
.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
104-
.childHandler(new ChannelInitializer<NioSocketChannel>() {
102+
.childHandler(new ChannelInitializer<SocketChannel>() {
105103
@Override
106-
protected void initChannel(NioSocketChannel ch) throws Exception {
104+
protected void initChannel(SocketChannel ch) throws Exception {
107105
// FIXME: should we use getTimeout()?
108106
int idleTimeout = UrlUtils.getIdleTimeout(getUrl());
109107
NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec(), getUrl(), NettyServer.this);

0 commit comments

Comments
 (0)