/
EpollTransport.java
151 lines (132 loc) · 4.98 KB
/
EpollTransport.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Copyright (c) 2011-2017 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package io.vertx.core.net.impl.transport;
import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.epoll.Epoll;
import io.netty.channel.epoll.EpollChannelOption;
import io.netty.channel.epoll.EpollDatagramChannel;
import io.netty.channel.epoll.EpollDomainSocketChannel;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.epoll.EpollServerDomainSocketChannel;
import io.netty.channel.epoll.EpollServerSocketChannel;
import io.netty.channel.epoll.EpollSocketChannel;
import io.netty.channel.socket.DatagramChannel;
import io.netty.channel.socket.InternetProtocolFamily;
import io.netty.channel.unix.DomainSocketAddress;
import io.vertx.core.datagram.DatagramSocketOptions;
import io.vertx.core.net.ClientOptionsBase;
import io.vertx.core.net.NetServerOptions;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.concurrent.ThreadFactory;
/**
* @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
*/
class EpollTransport extends Transport {
private static volatile int pendingFastOpenRequestsThreshold = 256;
/**
* Return the number of of pending TFO connections in SYN-RCVD state for TCP_FASTOPEN.
*
* {@see #setPendingFastOpenRequestsThreshold}
*/
public static int getPendingFastOpenRequestsThreshold() {
return pendingFastOpenRequestsThreshold;
}
/**
* Set the number of of pending TFO connections in SYN-RCVD state for TCP_FASTOPEN
* <p/>
* If this value goes over a certain limit the server disables all TFO connections.
*/
public static void setPendingFastOpenRequestsThreshold(int value) {
if (value < 0) {
throw new IllegalArgumentException("Invalid " + value);
}
pendingFastOpenRequestsThreshold = value;
}
EpollTransport() {
}
@Override
public SocketAddress convert(io.vertx.core.net.SocketAddress address, boolean resolved) {
if (address.path() != null) {
return new DomainSocketAddress(address.path());
} else {
if (resolved) {
return new InetSocketAddress(address.host(), address.port());
} else {
return InetSocketAddress.createUnresolved(address.host(), address.port());
}
}
}
@Override
public boolean isAvailable() {
return Epoll.isAvailable();
}
@Override
public Throwable unavailabilityCause() {
return Epoll.unavailabilityCause();
}
@Override
public EventLoopGroup eventLoopGroup(int nThreads, ThreadFactory threadFactory, int ioRatio) {
EpollEventLoopGroup eventLoopGroup = new EpollEventLoopGroup(nThreads, threadFactory);
eventLoopGroup.setIoRatio(ioRatio);
return eventLoopGroup;
}
@Override
public DatagramChannel datagramChannel() {
return new EpollDatagramChannel();
}
@Override
public DatagramChannel datagramChannel(InternetProtocolFamily family) {
return new EpollDatagramChannel();
}
@Override
public ChannelFactory<? extends Channel> channelFactory(boolean domainSocket) {
if (domainSocket) {
return EpollDomainSocketChannel::new;
} else {
return EpollSocketChannel::new;
}
}
public ChannelFactory<? extends ServerChannel> serverChannelFactory(boolean domainSocket) {
if (domainSocket) {
return EpollServerDomainSocketChannel::new;
}
return EpollServerSocketChannel::new;
}
@Override
public void configure(DatagramChannel channel, DatagramSocketOptions options) {
channel.config().setOption(EpollChannelOption.SO_REUSEPORT, options.isReusePort());
super.configure(channel, options);
}
@Override
public void configure(NetServerOptions options, boolean domainSocket, ServerBootstrap bootstrap) {
if (!domainSocket) {
bootstrap.option(EpollChannelOption.SO_REUSEPORT, options.isReusePort());
}
if (options.isTcpFastOpen()) {
bootstrap.option(EpollChannelOption.TCP_FASTOPEN, options.isTcpFastOpen() ? pendingFastOpenRequestsThreshold : 0);
}
bootstrap.childOption(EpollChannelOption.TCP_QUICKACK, options.isTcpQuickAck());
bootstrap.childOption(EpollChannelOption.TCP_CORK, options.isTcpCork());
super.configure(options, domainSocket, bootstrap);
}
@Override
public void configure(ClientOptionsBase options, boolean domainSocket, Bootstrap bootstrap) {
if (options.isTcpFastOpen()) {
bootstrap.option(EpollChannelOption.TCP_FASTOPEN_CONNECT, options.isTcpFastOpen());
}
bootstrap.option(EpollChannelOption.TCP_QUICKACK, options.isTcpQuickAck());
bootstrap.option(EpollChannelOption.TCP_CORK, options.isTcpCork());
super.configure(options, domainSocket, bootstrap);
}
}