下面列出了怎么用io.netty.channel.epoll.EpollMode的API类实例代码及写法,或者点击链接到github查看源代码。
private synchronized ServerBootstrap createServerBootstrap(final ChannelPipelineInitializer<?> initializer) {
final ServerBootstrap serverBootstrap = new ServerBootstrap();
if (Epoll.isAvailable()) {
serverBootstrap.channel(EpollServerSocketChannel.class);
serverBootstrap.childOption(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED);
} else {
serverBootstrap.channel(NioServerSocketChannel.class);
}
final ChannelHandler serverChannelHandler = BGPChannel.createServerChannelHandler(initializer);
serverBootstrap.childHandler(serverChannelHandler);
serverBootstrap.option(ChannelOption.SO_BACKLOG, SOCKET_BACKLOG_SIZE);
serverBootstrap.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
serverBootstrap.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, WATER_MARK);
// Make sure we are doing round-robin processing
serverBootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, RECV_ALLOCATOR);
if (serverBootstrap.config().group() == null) {
serverBootstrap.group(this.bossGroup, this.workerGroup);
}
return serverBootstrap;
}
public Bootstrap createBooStrap(String serviceName, final CommunicationOptions communicationOptions) {
// init netty bootstrap
Bootstrap bootstrap = new Bootstrap();
if (communicationOptions.getIoEventType() == BrpcConstants.IO_EVENT_NETTY_EPOLL) {
bootstrap.channel(EpollSocketChannel.class);
bootstrap.option(EpollChannelOption.EPOLL_MODE, EpollMode.EDGE_TRIGGERED);
} else {
bootstrap.channel(NioSocketChannel.class);
}
bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, communicationOptions.getConnectTimeoutMillis());
bootstrap.option(ChannelOption.SO_KEEPALIVE, communicationOptions.isKeepAlive());
bootstrap.option(ChannelOption.SO_REUSEADDR, communicationOptions.isReuseAddr());
bootstrap.option(ChannelOption.TCP_NODELAY, communicationOptions.isTcpNoDelay());
bootstrap.option(ChannelOption.SO_RCVBUF, communicationOptions.getReceiveBufferSize());
bootstrap.option(ChannelOption.SO_SNDBUF, communicationOptions.getSendBufferSize());
BrpcThreadPoolManager threadPoolManager = BrpcThreadPoolManager.getInstance();
boolean isSharing = communicationOptions.isGlobalThreadPoolSharing();
ThreadPool workThreadPool = threadPoolManager.getOrCreateClientWorkThreadPool(
serviceName, isSharing, communicationOptions.getWorkThreadNum());
ExecutorService exceptionThreadPool = threadPoolManager.getExceptionThreadPool();
final RpcClientHandler rpcClientHandler = new RpcClientHandler(workThreadPool, exceptionThreadPool);
final ChannelInitializer<SocketChannel> initializer = new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
if (communicationOptions.getChannelType() == ChannelType.SINGLE_CONNECTION) {
ch.pipeline().addLast(new IdleStateHandler(
0, 0, communicationOptions.getKeepAliveTime()));
ch.pipeline().addLast(new IdleChannelHandler());
}
ch.pipeline().addLast(rpcClientHandler);
}
};
EventLoopGroup ioThreadPool = threadPoolManager.getOrCreateClientIoThreadPool(
serviceName, isSharing, communicationOptions.getIoThreadNum(), communicationOptions.getIoEventType());
bootstrap.group(ioThreadPool).handler(initializer);
return bootstrap;
}
/**
* Use {@link EpollMode#LEVEL_TRIGGERED} for server bootstrap if level trigger enabled by system properties,
* otherwise use {@link EpollMode#EDGE_TRIGGERED}.
* @param serverBootstrap server bootstrap
*/
public static void enableTriggeredMode(ServerBootstrap serverBootstrap) {
if (epollEnabled) {
if (ConfigManager.netty_epoll_lt_enabled()) {
serverBootstrap.childOption(EpollChannelOption.EPOLL_MODE,
EpollMode.LEVEL_TRIGGERED);
} else {
serverBootstrap
.childOption(EpollChannelOption.EPOLL_MODE, EpollMode.EDGE_TRIGGERED);
}
}
}
private static void setChannelFactory(final Bootstrap bootstrap, final KeyMapping keys) {
if (Epoll.isAvailable()) {
bootstrap.channel(EpollSocketChannel.class);
bootstrap.option(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED);
} else {
bootstrap.channel(NioSocketChannel.class);
}
if (!keys.isEmpty()) {
if (Epoll.isAvailable()) {
bootstrap.option(EpollChannelOption.TCP_MD5SIG, keys);
} else {
throw new UnsupportedOperationException(Epoll.unavailabilityCause().getCause());
}
}
}
synchronized ServerBootstrap createServerBootstrap(final ChannelPipelineInitializer initializer) {
final ServerBootstrap b = new ServerBootstrap();
b.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(final SocketChannel ch) {
initializer.initializeChannel(ch, new DefaultPromise<>(PCEPDispatcherImpl.this.executor));
}
});
b.option(ChannelOption.SO_BACKLOG, SOCKET_BACKLOG_SIZE);
b.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
if (Epoll.isAvailable()) {
b.channel(EpollServerSocketChannel.class);
b.childOption(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED);
} else {
b.channel(NioServerSocketChannel.class);
}
if (!this.keys.isEmpty()) {
if (Epoll.isAvailable()) {
b.option(EpollChannelOption.TCP_MD5SIG, this.keys);
} else {
throw new UnsupportedOperationException(Epoll.unavailabilityCause().getCause());
}
}
// Make sure we are doing round-robin processing
b.childOption(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(1));
if (b.config().group() == null) {
b.group(this.bossGroup, this.workerGroup);
}
return b;
}
private synchronized Bootstrap createClientBootStrap(final KeyMapping keys, final boolean reuseAddress,
final InetSocketAddress localAddress) {
final Bootstrap bootstrap = new Bootstrap();
if (Epoll.isAvailable()) {
bootstrap.channel(EpollSocketChannel.class);
bootstrap.option(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED);
} else {
bootstrap.channel(NioSocketChannel.class);
}
if (keys != null && !keys.isEmpty()) {
if (Epoll.isAvailable()) {
bootstrap.option(EpollChannelOption.TCP_MD5SIG, keys);
} else {
throw new UnsupportedOperationException(Epoll.unavailabilityCause().getCause());
}
}
// Make sure we are doing round-robin processing
bootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, RECV_ALLOCATOR);
bootstrap.option(ChannelOption.SO_KEEPALIVE, Boolean.TRUE);
bootstrap.option(ChannelOption.WRITE_BUFFER_WATER_MARK, WATER_MARK);
bootstrap.option(ChannelOption.SO_REUSEADDR, reuseAddress);
if (bootstrap.config().group() == null) {
bootstrap.group(this.workerGroup);
}
bootstrap.localAddress(localAddress);
return bootstrap;
}
@Default
default EpollMode epollMode() {
return EpollMode.EDGE_TRIGGERED;
}
public static void enableTriggeredMode(ServerBootstrap bootstrap) {
if (Epoll.isAvailable()) {
bootstrap.childOption(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED);
}
}
@Override
protected void setOptions() {
super.setOptions();
ServerBootstrap boot = bootstrap();
// parent options
NettyConfig.NettyTcpConfigGroup.ParentConfig parent = configGroup.parent();
boot.option(ChannelOption.SO_BACKLOG, parent.getBacklog())
.option(ChannelOption.SO_REUSEADDR, parent.isReuseAddress())
.option(EpollChannelOption.SO_REUSEPORT, parent.isReusePort())
.option(EpollChannelOption.IP_FREEBIND, parent.isIpFreeBind())
.option(EpollChannelOption.IP_TRANSPARENT, parent.isIpTransparent());
if (parent.getRcvBuf() > 0) {
boot.option(ChannelOption.SO_RCVBUF, parent.getRcvBuf());
}
if (parent.getPendingFastOpenRequestsThreshold() > 0) {
boot.option(EpollChannelOption.TCP_FASTOPEN, parent.getPendingFastOpenRequestsThreshold());
}
if (parent.getTcpDeferAccept() > 0) {
boot.option(EpollChannelOption.TCP_DEFER_ACCEPT, parent.getTcpDeferAccept());
}
if (parent.isEdgeTriggered()) {
boot.option(EpollChannelOption.EPOLL_MODE, EpollMode.EDGE_TRIGGERED);
} else {
boot.option(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED);
}
// child options
NettyConfig.NettyTcpConfigGroup.ChildConfig child = configGroup.child();
WriteBufferWaterMark waterMark =
createWriteBufferWaterMark(child.getWriteBufferLowWaterMark(), child.getWriteBufferHighWaterMark());
boot.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, waterMark)
.childOption(ChannelOption.SO_REUSEADDR, child.isReuseAddress())
.childOption(ChannelOption.SO_KEEPALIVE, child.isKeepAlive())
.childOption(ChannelOption.TCP_NODELAY, child.isTcpNoDelay())
.childOption(ChannelOption.ALLOW_HALF_CLOSURE, child.isAllowHalfClosure());
if (child.getRcvBuf() > 0) {
boot.childOption(ChannelOption.SO_RCVBUF, child.getRcvBuf());
}
if (child.getSndBuf() > 0) {
boot.childOption(ChannelOption.SO_SNDBUF, child.getSndBuf());
}
if (child.getLinger() > 0) {
boot.childOption(ChannelOption.SO_LINGER, child.getLinger());
}
if (child.getIpTos() > 0) {
boot.childOption(ChannelOption.IP_TOS, child.getIpTos());
}
if (child.getTcpNotSentLowAt() > 0) {
boot.childOption(EpollChannelOption.TCP_NOTSENT_LOWAT, child.getTcpNotSentLowAt());
}
if (child.getTcpKeepCnt() > 0) {
boot.childOption(EpollChannelOption.TCP_KEEPCNT, child.getTcpKeepCnt());
}
if (child.getTcpUserTimeout() > 0) {
boot.childOption(EpollChannelOption.TCP_USER_TIMEOUT, child.getTcpUserTimeout());
}
if (child.getTcpKeepIdle() > 0) {
boot.childOption(EpollChannelOption.TCP_KEEPIDLE, child.getTcpKeepIdle());
}
if (child.getTcpKeepInterval() > 0) {
boot.childOption(EpollChannelOption.TCP_KEEPINTVL, child.getTcpKeepInterval());
}
if (SocketChannelProvider.SocketType.NATIVE_EPOLL == socketType()) {
boot.childOption(EpollChannelOption.TCP_CORK, child.isTcpCork())
.childOption(EpollChannelOption.TCP_QUICKACK, child.isTcpQuickAck())
.childOption(EpollChannelOption.IP_TRANSPARENT, child.isIpTransparent());
if (child.isTcpFastOpenConnect()) {
// Requires Linux kernel 4.11 or later
boot.childOption(EpollChannelOption.TCP_FASTOPEN_CONNECT, child.isTcpFastOpenConnect());
}
if (child.isEdgeTriggered()) {
boot.childOption(EpollChannelOption.EPOLL_MODE, EpollMode.EDGE_TRIGGERED);
} else {
boot.childOption(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED);
}
}
}
@Override
protected void setOptions() {
super.setOptions();
Bootstrap boot = bootstrap();
// child options
NettyConfig.NettyTcpConfigGroup.ChildConfig child = childConfig;
WriteBufferWaterMark waterMark =
createWriteBufferWaterMark(child.getWriteBufferLowWaterMark(), child.getWriteBufferHighWaterMark());
boot.option(ChannelOption.WRITE_BUFFER_WATER_MARK, waterMark)
.option(ChannelOption.SO_REUSEADDR, child.isReuseAddress())
.option(ChannelOption.SO_KEEPALIVE, child.isKeepAlive())
.option(ChannelOption.TCP_NODELAY, child.isTcpNoDelay())
.option(ChannelOption.ALLOW_HALF_CLOSURE, child.isAllowHalfClosure());
if (child.getRcvBuf() > 0) {
boot.option(ChannelOption.SO_RCVBUF, child.getRcvBuf());
}
if (child.getSndBuf() > 0) {
boot.option(ChannelOption.SO_SNDBUF, child.getSndBuf());
}
if (child.getLinger() > 0) {
boot.option(ChannelOption.SO_LINGER, child.getLinger());
}
if (child.getIpTos() > 0) {
boot.option(ChannelOption.IP_TOS, child.getIpTos());
}
if (child.getConnectTimeoutMillis() > 0) {
boot.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, child.getConnectTimeoutMillis());
}
if (child.getTcpNotSentLowAt() > 0) {
boot.option(EpollChannelOption.TCP_NOTSENT_LOWAT, child.getTcpNotSentLowAt());
}
if (child.getTcpKeepCnt() > 0) {
boot.option(EpollChannelOption.TCP_KEEPCNT, child.getTcpKeepCnt());
}
if (child.getTcpUserTimeout() > 0) {
boot.option(EpollChannelOption.TCP_USER_TIMEOUT, child.getTcpUserTimeout());
}
if (child.getTcpKeepIdle() > 0) {
boot.option(EpollChannelOption.TCP_KEEPIDLE, child.getTcpKeepIdle());
}
if (child.getTcpKeepInterval() > 0) {
boot.option(EpollChannelOption.TCP_KEEPINTVL, child.getTcpKeepInterval());
}
if (SocketChannelProvider.SocketType.NATIVE_EPOLL == socketType()) {
boot.option(EpollChannelOption.TCP_CORK, child.isTcpCork())
.option(EpollChannelOption.TCP_QUICKACK, child.isTcpQuickAck())
.option(EpollChannelOption.IP_TRANSPARENT, child.isIpTransparent());
if (child.isTcpFastOpenConnect()) {
// Requires Linux kernel 4.11 or later
boot.option(EpollChannelOption.TCP_FASTOPEN_CONNECT, child.isTcpFastOpenConnect());
}
if (child.isEdgeTriggered()) {
boot.option(EpollChannelOption.EPOLL_MODE, EpollMode.EDGE_TRIGGERED);
} else {
boot.option(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED);
}
}
}