下面列出了io.netty.channel.epoll.Epoll#isAvailable ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static NettyChannelBuilder newNettyChannelBuilder(String targetUrl, String proxy)
throws IOException {
if (Strings.isNullOrEmpty(proxy)) {
return NettyChannelBuilder.forTarget(targetUrl).defaultLoadBalancingPolicy("round_robin");
}
if (!proxy.startsWith("unix:")) {
throw new IOException("Remote proxy unsupported: " + proxy);
}
DomainSocketAddress address = new DomainSocketAddress(proxy.replaceFirst("^unix:", ""));
NettyChannelBuilder builder =
NettyChannelBuilder.forAddress(address).overrideAuthority(targetUrl);
if (KQueue.isAvailable()) {
return builder
.channelType(KQueueDomainSocketChannel.class)
.eventLoopGroup(new KQueueEventLoopGroup());
}
if (Epoll.isAvailable()) {
return builder
.channelType(EpollDomainSocketChannel.class)
.eventLoopGroup(new EpollEventLoopGroup());
}
throw new IOException("Unix domain sockets are unsupported on this platform");
}
public static Provider create() {
Supplier<String> nettyProvider = ConfigService.supplier("iris.gateway.provider", String.class, "");
switch (nettyProvider.get()) {
case "epoll":
if (Epoll.isAvailable()) {
log.debug("using netty epoll provider for gateway connection");
return epoll();
} else {
if (!"".equals(nettyProvider.get())) {
log.warn("netty epoll provider requested but not available, using nio for gateway connection:", Epoll.unavailabilityCause());
} else {
log.debug("using netty nio provider for gateway connection");
}
return nio();
}
case "":
case "nio":
log.debug("using netty nio provider for gateway connection");
return nio();
default:
log.warn("unknown netty provider, using nio by default");
return nio();
}
}
private CompletableFuture<Bootstrap> setupNetty(InetSocketAddress address, ChannelHandler handler) {
var future = new CompletableFuture<Bootstrap>();
var bootstrap = new Bootstrap()
.group(vertx.nettyEventLoopGroup());
if (Epoll.isAvailable()) {
logger.info("epoll support is available, using it for UDP connections.");
bootstrap.channel(EpollDatagramChannel.class);
} else {
logger.info("epoll unavailable, falling back to NIO.");
bootstrap.channel(NioDatagramChannel.class);
}
bootstrap.option(ChannelOption.SO_REUSEADDR, true);
bootstrap.option(ChannelOption.IP_TOS, 0x10 | 0x08); // IPTOS_LOWDELAY | IPTOS_THROUGHPUT
bootstrap.handler(handler).connect(address).addListener(res -> {
if (res.isSuccess()) {
future.complete(bootstrap);
} else {
future.completeExceptionally(res.cause());
}
});
return future;
}
private void setChannelInfo() {
if (Epoll.isAvailable()) {
this.group = new EpollEventLoopGroup();
this.channelType = EpollDomainSocketChannel.class;
LOGGER.info("Using epoll for Netty transport.");
} else {
if (!KQueue.isAvailable()) {
throw new RuntimeException("Unsupported OS '" + System.getProperty("os.name") + "', only Unix and Mac are supported");
}
this.group = new KQueueEventLoopGroup();
this.channelType = KQueueDomainSocketChannel.class;
LOGGER.info("Using KQueue for Netty transport.");
}
}
/**
* @return an EventLoopGroup suitable for the current platform
*/
public static EventLoopGroup newEventLoopGroup(int nThreads, ThreadFactory threadFactory) {
if (Epoll.isAvailable()) {
return new EpollEventLoopGroup(nThreads, threadFactory);
} else {
// Fallback to NIO
return new NioEventLoopGroup(nThreads, threadFactory);
}
}
public AbstractNettyServer(String preName,InetSocketAddress address) {
super();
this.enableEpoll = Epoll.isAvailable();
this.serverAddress = address;
this.name = NamespaceUtil.newIdName(preName,getClass());
if(enableEpoll) {
logger.info("enable epoll server = {}",this);
}
}
/**
* Determine if native Epoll for netty is available
*
* @return
*/
protected boolean isEpollSupported() {
final boolean available = Epoll.isAvailable();
if (available) {
log.info("Using native epoll");
}
return available;
}
public BGPDispatcherImpl(final MessageRegistry messageRegistry, final EventLoopGroup bossGroup,
final EventLoopGroup workerGroup, final BGPPeerRegistry bgpPeerRegistry) {
if (Epoll.isAvailable()) {
this.bossGroup = new EpollEventLoopGroup();
this.workerGroup = new EpollEventLoopGroup();
} else {
this.bossGroup = requireNonNull(bossGroup);
this.workerGroup = requireNonNull(workerGroup);
}
this.bgpPeerRegistry = requireNonNull(bgpPeerRegistry);
this.handlerFactory = new BGPHandlerFactory(messageRegistry);
}
private TransportTypeHolder(int workerThreads) {
if (Epoll.isAvailable()) {
log.info("Using native epoll transport.");
bossGroup = new EpollEventLoopGroup(1);
workerGroup = new EpollEventLoopGroup(workerThreads);
channelClass = EpollServerSocketChannel.class;
} else {
bossGroup = new NioEventLoopGroup(1);
workerGroup = new NioEventLoopGroup(workerThreads);
channelClass = NioServerSocketChannel.class;
}
}
private static boolean isEpollAvailable() {
if (SystemInfo.isLinux()) {
// Netty epoll transport does not work with WSL (Windows Sybsystem for Linux) yet.
// TODO(trustin): Re-enable on WSL if https://github.com/Microsoft/WSL/issues/1982 is resolved.
return Epoll.isAvailable() && !HAS_WSLENV;
}
return false;
}
public static HttpBlobStore create(
DomainSocketAddress domainSocketAddress,
URI uri,
int timeoutMillis,
int remoteMaxConnections,
@Nullable final Credentials creds)
throws ConfigurationException, URISyntaxException, SSLException {
if (KQueue.isAvailable()) {
return new HttpBlobStore(
KQueueEventLoopGroup::new,
KQueueDomainSocketChannel.class,
uri,
timeoutMillis,
remoteMaxConnections,
creds,
domainSocketAddress);
} else if (Epoll.isAvailable()) {
return new HttpBlobStore(
EpollEventLoopGroup::new,
EpollDomainSocketChannel.class,
uri,
timeoutMillis,
remoteMaxConnections,
creds,
domainSocketAddress);
} else {
throw new ConfigurationException("Unix domain sockets are unsupported on this platform");
}
}
public PCCDispatcherImpl(final @NonNull MessageRegistry registry) {
if (Epoll.isAvailable()) {
this.workerGroup = new EpollEventLoopGroup();
} else {
this.workerGroup = new NioEventLoopGroup();
}
this.factory = new PCEPHandlerFactory(registry);
}
protected EventLoopGroup newIoEventGroup() {
NamedThreadFactory threadFactory = new NamedThreadFactory(config.getIoThreadName());
if (Epoll.isAvailable()) {
return new EpollEventLoopGroup(config.getIoThread(), threadFactory);
} else {
return new NioEventLoopGroup(config.getIoThread(), threadFactory);
}
}
public static boolean isAvailable(TransportOptions transportOptions) {
try {
return transportOptions.isUseEpoll() && Epoll.isAvailable();
} catch (NoClassDefFoundError ncdfe) {
LOG.debug("Unable to check for Epoll support due to missing class definition", ncdfe);
return false;
}
}
@Override
protected List<ConfigIssue> init() {
List<ConfigIssue> issues = new ArrayList<>();
config.dataFormatConfig.stringBuilderPoolSize = config.numThreads;
if (config.enableEpoll && !Epoll.isAvailable()) {
issues.add(getContext().createConfigIssue(Groups.TCP.name(), CONF_PREFIX + "enableEpoll", Errors.TCP_05));
}
final String portsField = "ports";
if (config.ports.isEmpty()) {
issues.add(getContext().createConfigIssue(Groups.TCP.name(), portsField, Errors.TCP_02));
} else {
for (String candidatePort : config.ports) {
try {
int port = Integer.parseInt(candidatePort.trim());
if (port > 0 && port < 65536) {
if (port < 1024) {
privilegedPortUsage = true; // only for error handling purposes
}
addresses.add(new InetSocketAddress(port));
} else {
issues.add(getContext().createConfigIssue(Groups.TCP.name(), portsField, Errors.TCP_03, port));
}
} catch (NumberFormatException ex) {
issues.add(getContext().createConfigIssue(Groups.TCP.name(), portsField, Errors.TCP_03, candidatePort));
}
}
}
if (issues.isEmpty()) {
if (addresses.isEmpty()) {
issues.add(getContext().createConfigIssue(Groups.TCP.name(), portsField, Errors.TCP_09));
} else {
if (config.tlsConfigBean.isEnabled()) {
boolean tlsValid = config.tlsConfigBean.init(
getContext(),
Groups.TLS.name(),
CONF_PREFIX + "tlsConfigBean.",
issues
);
if (!tlsValid) {
return issues;
}
}
final boolean elValid = validateEls(issues);
if (!elValid) {
return issues;
}
validateTcpConfigs(issues);
if (issues.size() > 0) {
return issues;
}
if (config.tcpMode == TCPMode.FLUME_AVRO_IPC) {
config.dataFormatConfig.init(
getContext(),
config.dataFormat,
Groups.TCP.name(),
CONF_PREFIX + "dataFormatConfig",
config.maxMessageSize,
issues
);
parserFactory = config.dataFormatConfig.getParserFactory();
final int avroIpcPort = Integer.parseInt(config.ports.get(0));
final SpecificResponder avroIpcResponder = new SpecificResponder(
AvroSourceProtocol.class,
new SDCFlumeAvroIpcProtocolHandler(getContext(), parserFactory, pipelineIdsToFail::put)
);
// this uses Netty 3.x code to help avoid rewriting a lot in our own stage lib
// Netty 3.x and 4.x (which we use for the other modes) can coexist on the same classpath, so should be OK
avroIpcServer = new NettyServer(
avroIpcResponder,
new InetSocketAddress(config.bindAddress, avroIpcPort)
);
avroIpcServer.start();
} else {
createAndStartTCPServer(issues, portsField);
}
}
}
return issues;
}
private boolean useEpoll() {
return RemotingUtil.isLinuxPlatform()
&& Epoll.isAvailable();
}
public static EventLoopGroup newBossEventLoopGroup() {
if (Epoll.isAvailable()) {
return new EpollEventLoopGroup();
}
return new NioEventLoopGroup();
}
public Connector(int port) {
this(port, useNativeIo && (Epoll.isAvailable() || KQueue.isAvailable()));
}
public List<Stage.ConfigIssue> init(Stage.Context context) {
List<Stage.ConfigIssue> issues = new ArrayList<>();
addresses = new ArrayList<>();
for (String candidatePort : ports) {
try {
int port = Integer.parseInt(candidatePort.trim());
if (port > 0 && port < 65536) {
if (port < 1024) {
privilegedPortUsage = true; // only for error handling purposes
}
addresses.add(new InetSocketAddress(port));
} else {
issues.add(context.createConfigIssue(Groups.UDP.name(), "ports", Errors.UDP_KAFKA_ORIG_00, port));
}
} catch (NumberFormatException ex) {
issues.add(context.createConfigIssue(Groups.UDP.name(),
"ports",
Errors.UDP_KAFKA_ORIG_00,
candidatePort
));
}
}
if (addresses.isEmpty()) {
issues.add(context.createConfigIssue(Groups.UDP.name(), "ports", Errors.UDP_KAFKA_ORIG_03));
}
if (acceptThreads < 1 || acceptThreads > 32) {
issues.add(context.createConfigIssue(Groups.ADVANCED.name(), "acceptThreads", Errors.UDP_KAFKA_ORIG_05));
}
if (concurrency < 1 || concurrency > 2048) {
issues.add(context.createConfigIssue(Groups.UDP.name(), "concurrency", Errors.UDP_KAFKA_ORIG_04));
}
if (enableEpoll && !Epoll.isAvailable()) {
issues.add(context.createConfigIssue(Groups.UDP.name(), "enableEpoll", Errors.UDP_KAFKA_ORIG_06));
}
// Force threads to 1 if epoll not enabled
if (!enableEpoll) {
acceptThreads = 1;
}
return issues;
}
private boolean useEpoll() {
return RemotingUtil.isLinuxPlatform()
&& nettyServerConfig.isUseEpollNativeSelector()
&& Epoll.isAvailable();
}