-
Notifications
You must be signed in to change notification settings - Fork 347
Expand file tree
/
Copy pathUnixDomainSocketFactory.java
More file actions
90 lines (78 loc) · 2.81 KB
/
Copy pathUnixDomainSocketFactory.java
File metadata and controls
90 lines (78 loc) · 2.81 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
package datadog.common.socket;
import static java.util.concurrent.TimeUnit.MINUTES;
import datadog.trace.api.Config;
import datadog.trace.api.Platform;
import datadog.trace.relocate.api.RatelimitedLogger;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import javax.net.SocketFactory;
import jnr.unixsocket.UnixSocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Impersonate TCP-style SocketFactory over UNIX domain sockets.
*
* <p>Copied from <a
* href="https://github.com/square/okhttp/blob/master/samples/unixdomainsockets/src/main/java/okhttp3/unixdomainsockets/UnixDomainSocketFactory.java">okHttp
* examples</a>.
*/
public final class UnixDomainSocketFactory extends SocketFactory {
private static final Logger log = LoggerFactory.getLogger(UnixDomainSocketFactory.class);
private static final boolean JDK_SUPPORTS_UDS = Platform.isJavaVersionAtLeast(16);
private final RatelimitedLogger rlLog = new RatelimitedLogger(log, 5, MINUTES);
private final File path;
public UnixDomainSocketFactory(final File path) {
this.path = path;
}
@Override
public Socket createSocket() throws IOException {
try {
if (JDK_SUPPORTS_UDS && Config.get().isJdkSocketEnabled()) {
try {
return new TunnelingJdkSocket(path.toPath());
} catch (Throwable ignore) {
// fall back to jnr-unixsocket library
}
}
return new TunnelingUnixSocket(path, UnixSocketChannel.open());
} catch (Throwable e) {
if (Config.get().isAgentConfiguredUsingDefault()) {
// fall back to port if we previously auto-discovered this socket file
if (log.isDebugEnabled()) {
rlLog.warn("Problem opening {}, using port instead", path, e);
} else {
rlLog.warn("Problem opening {}, using port instead: " + e, path);
}
return getDefault().createSocket();
}
throw e;
}
}
@Override
public Socket createSocket(final String host, final int port) throws IOException {
final Socket result = createSocket();
result.connect(new InetSocketAddress(host, port));
return result;
}
@Override
public Socket createSocket(
final String host, final int port, final InetAddress localHost, final int localPort)
throws IOException {
return createSocket(host, port);
}
@Override
public Socket createSocket(final InetAddress host, final int port) throws IOException {
final Socket result = createSocket();
result.connect(new InetSocketAddress(host, port));
return result;
}
@Override
public Socket createSocket(
final InetAddress host, final int port, final InetAddress localAddress, final int localPort)
throws IOException {
return createSocket(host, port);
}
}