-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathUnixStreamClientChannel.java
More file actions
311 lines (275 loc) · 10.6 KB
/
Copy pathUnixStreamClientChannel.java
File metadata and controls
311 lines (275 loc) · 10.6 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package com.timgroup.statsd;
import java.io.IOException;
import java.net.SocketAddress;
import java.net.StandardSocketOptions;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import jnr.unixsocket.UnixSocketAddress;
import jnr.unixsocket.UnixSocketChannel;
import jnr.unixsocket.UnixSocketOptions;
/** A ClientChannel for Unix domain sockets. */
public class UnixStreamClientChannel implements ClientChannel {
private final SocketAddress address;
private final int timeout;
private final int connectionTimeout;
private final int bufferSize;
private final boolean enableJdkSocket;
private SocketChannel delegate;
private final ByteBuffer delimiterBuffer =
ByteBuffer.allocateDirect(Integer.SIZE / Byte.SIZE).order(ByteOrder.LITTLE_ENDIAN);
/**
* Creates a new NamedPipeClientChannel with the given address.
*
* @param address Location of named pipe
*/
UnixStreamClientChannel(
SocketAddress address,
int timeout,
int connectionTimeout,
int bufferSize,
boolean enableJdkSocket)
throws IOException {
this.delegate = null;
this.address = address;
this.timeout = timeout;
this.connectionTimeout = connectionTimeout;
this.bufferSize = bufferSize;
this.enableJdkSocket = enableJdkSocket;
}
@Override
public boolean isOpen() {
return delegate.isConnected();
}
@Override
public synchronized int write(ByteBuffer src) throws IOException {
connectIfNeeded();
int size = src.remaining();
int written = 0;
if (size == 0) {
return 0;
}
delimiterBuffer.clear();
delimiterBuffer.putInt(size);
delimiterBuffer.flip();
try {
long deadline = timeout > 0 ? System.nanoTime() + timeout * 1_000_000L : 0;
written = writeAll(delimiterBuffer, true, deadline);
if (written > 0) {
written += writeAll(src, false, deadline);
}
} catch (IOException e) {
// If we get an exception, it's unrecoverable, we close the channel and try to reconnect
disconnect();
throw e;
}
// If we haven't written anything, we have a timeout
if (written == 0) {
throw new IOException("Write timed out");
}
return size;
}
/**
* Writes all bytes from the given buffer to the channel.
*
* @param bb buffer to write
* @param canReturnOnTimeout if true, we return if the channel is blocking and we haven't
* written anything yet
* @param deadline deadline for the write
* @return number of bytes written
* @throws IOException if the channel is closed or an error occurs
*/
public int writeAll(ByteBuffer bb, boolean canReturnOnTimeout, long deadline)
throws IOException {
int remaining = bb.remaining();
int written = 0;
long timeoutMs = timeout;
while (remaining > 0) {
int read = delegate.write(bb);
if (read > 0) {
remaining -= read;
written += read;
if (deadline > 0 && System.nanoTime() >= deadline) {
throw new IOException("Write timed out");
}
continue;
}
if (read == 0) {
if (delegate.isBlocking()) {
if (canReturnOnTimeout && written == 0) {
return written;
}
throw new IOException("Write timed out");
}
try (Selector selector = Selector.open()) {
delegate.register(selector, SelectionKey.OP_WRITE);
long selectTimeout = timeoutMs;
if (deadline > 0) {
long remainingNs = deadline - System.nanoTime();
if (remainingNs <= 0) {
throw new IOException("Write timed out");
}
long remainingMs = Math.max(1L, remainingNs / 1_000_000L);
selectTimeout =
timeoutMs > 0 ? Math.min(timeoutMs, remainingMs) : remainingMs;
}
int selected =
selectTimeout > 0 ? selector.select(selectTimeout) : selector.select();
if (selected == 0) {
throw new IOException("Write timed out after " + selectTimeout + "ms");
}
}
}
}
return written;
}
private void connectIfNeeded() throws IOException {
if (delegate == null) {
connect();
}
}
private void disconnect() throws IOException {
if (delegate != null) {
delegate.close();
delegate = null;
}
}
private void connect() throws IOException {
if (this.delegate != null) {
try {
disconnect();
} catch (IOException e) {
// ignore to be sure we don't stay with a broken delegate forever.
}
}
// Use native JDK support for UDS on Java 16+ and jnr-unixsocket otherwise
if (VersionUtils.isJavaVersionAtLeast(16) && enableJdkSocket && connectWithJdkSocket()) {
return;
}
// Default to jnr-unixsocket if Java version is < 16 or native support is disabled
UnixSocketChannel channel = UnixSocketChannel.create();
long deadline = System.nanoTime() + connectionTimeout * 1_000_000L;
if (connectionTimeout > 0) {
// Set connect timeout, this should work at least on linux
// https://elixir.bootlin.com/linux/v5.7.4/source/net/unix/af_unix.c#L1696
channel.setOption(UnixSocketOptions.SO_SNDTIMEO, connectionTimeout);
}
try {
UnixSocketAddress unixAddress;
if (address instanceof UnixSocketAddress) {
unixAddress = (UnixSocketAddress) address;
} else {
unixAddress = new UnixSocketAddress(address.toString());
}
if (!channel.connect(unixAddress)) {
if (connectionTimeout > 0 && System.nanoTime() > deadline) {
throw new IOException("Connection timed out");
}
if (!channel.finishConnect()) {
throw new IOException("Connection failed");
}
}
channel.setOption(UnixSocketOptions.SO_SNDTIMEO, Math.max(timeout, 0));
if (bufferSize > 0) {
channel.setOption(UnixSocketOptions.SO_SNDBUF, bufferSize);
}
} catch (Exception e) {
try {
channel.close();
} catch (IOException __) {
// ignore
}
throw e;
}
this.delegate = channel;
}
private boolean connectWithJdkSocket() throws IOException {
SocketChannel channel = null;
SocketAddress connectAddress;
try {
// Only SocketChannel.open(ProtocolFamily) needs reflection; connect/finishConnect
// have existed since Java 1.4 and are called directly once we have the channel.
channel = openJdkSocketChannel();
connectAddress = nativeSocketAddress(address);
if (bufferSize > 0) {
channel.setOption(StandardSocketOptions.SO_SNDBUF, bufferSize);
}
} catch (Exception | LinkageError e) {
closeQuietly(channel);
return false;
}
try {
if (connectionTimeout <= 0) {
channel.configureBlocking(true);
channel.connect(connectAddress);
channel.configureBlocking(false);
} else {
channel.configureBlocking(false);
long deadline = System.nanoTime() + connectionTimeout * 1_000_000L;
if (!channel.connect(connectAddress)) {
try (Selector selector = Selector.open()) {
channel.register(selector, SelectionKey.OP_CONNECT);
while (!channel.finishConnect()) {
long remainingNs = deadline - System.nanoTime();
if (remainingNs <= 0) {
throw new IOException(
"Connection timed out after " + connectionTimeout + "ms");
}
long selectTimeout = Math.max(1L, remainingNs / 1_000_000L);
if (selector.select(selectTimeout) == 0
&& System.nanoTime() >= deadline) {
throw new IOException(
"Connection timed out after " + connectionTimeout + "ms");
}
selector.selectedKeys().clear();
}
}
}
}
} catch (IOException | RuntimeException e) {
closeQuietly(channel);
throw e;
}
this.delegate = channel;
return true;
}
SocketChannel openJdkSocketChannel() throws IOException {
return VersionUtils.openUnixSocketChannel();
}
private static SocketAddress nativeSocketAddress(SocketAddress address) {
if (address instanceof UnixSocketAddressWithTransport) {
address = ((UnixSocketAddressWithTransport) address).getAddress();
}
if (address instanceof UnixSocketAddress) {
return VersionUtils.newUnixDomainSocketAddress(((UnixSocketAddress) address).path());
}
return address;
}
private static void closeQuietly(SocketChannel channel) {
if (channel != null) {
try {
channel.close();
} catch (IOException ignored) {
// ignore
}
}
}
@Override
public void close() throws IOException {
disconnect();
}
@Override
public String getTransportType() {
return "uds-stream";
}
@Override
public String toString() {
return "[" + getTransportType() + "] " + address;
}
@Override
public int getMaxPacketSizeBytes() {
return NonBlockingStatsDClient.DEFAULT_UDS_MAX_PACKET_SIZE_BYTES;
}
}