Skip to content

Commit

Permalink
[#8470] Remove unnecessary reflection to support reuseport
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Dec 8, 2021
1 parent 840e9dd commit 0059b00
Showing 1 changed file with 18 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@

package com.navercorp.pinpoint.collector.receiver.thrift.udp;

import com.navercorp.pinpoint.common.util.OsType;
import com.navercorp.pinpoint.common.util.OsUtils;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.DatagramSocket;
import java.net.SocketOption;
import java.net.StandardSocketOptions;

/**
Expand All @@ -33,9 +32,7 @@ public class ReusePortSocketOptionApplier {

private static final Logger LOGGER = LogManager.getLogger(ReusePortSocketOptionApplier.class);

private static final String FIELD_NAME_SO_REUSEPORT = "SO_REUSEPORT";

private static final SocketOption REUSE_PORT_SOCKET_OPTION = getReusePortSocketOption();
private static final OsType[] UNSUPPORTED_OS = new OsType[]{OsType.WINDOW, OsType.SOLARIS};

private final boolean reusePortEnable;
private final int socketCount;
Expand All @@ -50,14 +47,9 @@ public void apply(DatagramSocket socket) throws IOException {
return;
}
try {
Method setOptionMethod = DatagramSocket.class.getDeclaredMethod("setOption", SocketOption.class, Object.class);
setOptionMethod.invoke(socket, REUSE_PORT_SOCKET_OPTION, true);
} catch (Exception e) {
socket.setOption(StandardSocketOptions.SO_REUSEPORT, true);
} catch (IOException e) {
LOGGER.warn("setOption invoke error", e);
if (e instanceof IOException) {
throw (IOException)e;
}
throw new IOException("setOption invoke error", e);
}
}

Expand All @@ -70,31 +62,24 @@ public int getSocketCount() {
}

public static ReusePortSocketOptionApplier create(boolean reusePort, int socketCount) {
if (REUSE_PORT_SOCKET_OPTION != null) {
return new ReusePortSocketOptionApplier(reusePort, socketCount);
}
if (reusePort) {
LOGGER.warn("ReusePort not supported, Please use Jvm9+ for using ReusePort SocketOption");
if (isUnsupportedOS()) {
if (reusePort) {
LOGGER.warn("ReusePort not supported, OS:{}", OsUtils.getType());
}
return new ReusePortSocketOptionApplier(false, socketCount);
}
return new ReusePortSocketOptionApplier(false, socketCount);

return new ReusePortSocketOptionApplier(reusePort, socketCount);
}

private static SocketOption getReusePortSocketOption() {
try {
Field[] declaredFields = StandardSocketOptions.class.getDeclaredFields();
for (Field declaredField : declaredFields) {
if (declaredField.getName().equals(FIELD_NAME_SO_REUSEPORT)) {
Object socketOption = declaredField.get(null);
if (socketOption instanceof SocketOption) {
LOGGER.info("{} option found", FIELD_NAME_SO_REUSEPORT);
return (SocketOption) socketOption;
}
}
private static boolean isUnsupportedOS() {
final OsType osType = OsUtils.getType();
for (OsType unsupportedO : UNSUPPORTED_OS) {
if (osType.equals(unsupportedO)) {
return true;
}
} catch (Exception ignore) {
// ignores
}
return null;
return false;
}

@Override
Expand Down

0 comments on commit 0059b00

Please sign in to comment.