Reorder PlatformDependent.isRoot() check

Motivation:

isRoot() is an expensive operation. We should avoid calling it if
possible.

Modifications:

Move the isRoot() checks to the end of the 'if' block, so that isRoot()
is evaluated only when really necessary.

Result:

isRoot() is evaluated only when SO_BROADCAST is set and the bind address
is anylocal address.
This commit is contained in:
Trustin Lee 2015-02-08 11:54:40 +09:00
parent 730ec357d2
commit a1efd1871b
3 changed files with 6 additions and 6 deletions

View File

@ -459,7 +459,7 @@ public final class PlatformDependent {
return false; return false;
} }
String[] ID_COMMANDS = { "/usr/bin/id", "/bin/id", "id", "/usr/xpg4/bin/id"}; String[] ID_COMMANDS = { "/usr/bin/id", "/bin/id", "/usr/xpg4/bin/id", "id"};
Pattern UID_PATTERN = Pattern.compile("^(?:0|[1-9][0-9]*)$"); Pattern UID_PATTERN = Pattern.compile("^(?:0|[1-9][0-9]*)$");
for (String idCmd: ID_COMMANDS) { for (String idCmd: ID_COMMANDS) {
Process p = null; Process p = null;

View File

@ -504,10 +504,10 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
} }
// See: https://github.com/netty/netty/issues/576 // See: https://github.com/netty/netty/issues/576
if (!PlatformDependent.isWindows() && !PlatformDependent.isRoot() && if (Boolean.TRUE.equals(config().getOption(ChannelOption.SO_BROADCAST)) &&
Boolean.TRUE.equals(config().getOption(ChannelOption.SO_BROADCAST)) &&
localAddress instanceof InetSocketAddress && localAddress instanceof InetSocketAddress &&
!((InetSocketAddress) localAddress).getAddress().isAnyLocalAddress()) { !((InetSocketAddress) localAddress).getAddress().isAnyLocalAddress() &&
!PlatformDependent.isWindows() && !PlatformDependent.isRoot()) {
// Warn a user about the fact that a non-root user can't receive a // Warn a user about the fact that a non-root user can't receive a
// broadcast packet on *nix if the socket is bound on non-wildcard address. // broadcast packet on *nix if the socket is bound on non-wildcard address.
logger.warn( logger.warn(

View File

@ -158,8 +158,8 @@ public class DefaultDatagramChannelConfig extends DefaultChannelConfig implement
try { try {
// See: https://github.com/netty/netty/issues/576 // See: https://github.com/netty/netty/issues/576
if (broadcast && if (broadcast &&
!PlatformDependent.isWindows() && !PlatformDependent.isRoot() && !javaSocket.getLocalAddress().isAnyLocalAddress() &&
!javaSocket.getLocalAddress().isAnyLocalAddress()) { !PlatformDependent.isWindows() && !PlatformDependent.isRoot()) {
// Warn a user about the fact that a non-root user can't receive a // Warn a user about the fact that a non-root user can't receive a
// broadcast packet on *nix if the socket is bound on non-wildcard address. // broadcast packet on *nix if the socket is bound on non-wildcard address.
logger.warn( logger.warn(