【Netty源码解读和权威指南】第68篇:Netty KQueue传输——macOS BSD下的原生传输
·
上一篇【第67篇】Netty Epoll传输——Linux下性能更好的原生传输层
下一篇【第69篇】Netty与gRPC——高性能RPC框架的底层网络秘密
一、KQueue vs NIO
| 平台 | NIO | 原生 |
|---|---|---|
| Linux | NioEventLoop | EpollEventLoop |
| macOS | NioEventLoop | KQueueEventLoop |
二、KQueue使用
import io.netty.channel.kqueue.KQueue;
import io.netty.channel.kqueue.KQueueEventLoopGroup;
import io.netty.channel.kqueue.KQueueServerSocketChannel;
if (KQueue.isAvailable()) {
EventLoopGroup boss = new KQueueEventLoopGroup(1);
EventLoopGroup worker = new KQueueEventLoopGroup();
ServerBootstrap b = new ServerBootstrap()
.group(boss, worker)
.channel(KQueueServerSocketChannel.class);
}
三、跨平台自动选择
public static Class<? extends ServerChannel> getBestServerChannel() {
if (Epoll.isAvailable()) return EpollServerSocketChannel.class;
if (KQueue.isAvailable()) return KQueueServerSocketChannel.class;
return NioServerSocketChannel.class;
}
public static EventLoopGroup getBestEventLoopGroup(int threads) {
if (Epoll.isAvailable()) return new EpollEventLoopGroup(threads);
if (KQueue.isAvailable()) return new KQueueEventLoopGroup(threads);
return new NioEventLoopGroup(threads);
}
四、macOS开发建议
| 环境 | 传输层 | 理由 |
|---|---|---|
| 开发(macOS) | NIO | 兼容性最好 |
| 开发(macOS) | KQueue | 功能类似Epoll |
| 生产(Linux) | Epoll | 性能最佳 |
上一篇【第67篇】Netty Epoll传输——Linux下性能更好的原生传输层
下一篇【第69篇】Netty与gRPC——高性能RPC框架的底层网络秘密
更多推荐




所有评论(0)