Use SOMAXCONN as the default backlog if possible

This commit is contained in:
Trustin Lee 2012-07-07 15:05:10 +09:00
parent ec88f6617c
commit c0f4f75c6d
3 changed files with 31 additions and 2 deletions

View File

@ -18,6 +18,8 @@ package io.netty.util;
import io.netty.logging.InternalLogger;
import io.netty.logging.InternalLoggerFactory;
import java.io.BufferedReader;
import java.io.FileReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
@ -43,6 +45,12 @@ public final class NetworkConstants {
*/
public static final NetworkInterface LOOPBACK_IF;
/**
* The SOMAXCONN value of the current machine. If failed to get the value, 3072 is used as a
* default value.
*/
public static final int SOMAXCONN;
/**
* The logger being used by this class
*/
@ -114,6 +122,25 @@ public final class NetworkConstants {
//Set the loopback interface constant
LOOPBACK_IF = loopbackInterface;
int somaxconn = 3072;
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader("/proc/sys/net/core/somaxconn"));
somaxconn = Integer.parseInt(in.readLine());
} catch (Exception e) {
// Failed to get SOMAXCONN
} finally {
if (in != null) {
try {
in.close();
} catch (Exception e) {
// Ignored.
}
}
}
SOMAXCONN = somaxconn;
}
/**

View File

@ -19,6 +19,7 @@ import static io.netty.channel.ChannelOption.*;
import io.netty.channel.ChannelException;
import io.netty.channel.ChannelOption;
import io.netty.channel.DefaultChannelConfig;
import io.netty.util.NetworkConstants;
import java.net.ServerSocket;
import java.net.SocketException;
@ -31,7 +32,7 @@ public class DefaultServerSocketChannelConfig extends DefaultChannelConfig
implements ServerSocketChannelConfig {
private final ServerSocket socket;
private volatile int backlog;
private volatile int backlog = NetworkConstants.SOMAXCONN;
/**
* Creates a new instance.

View File

@ -20,6 +20,7 @@ import io.netty.channel.ChannelException;
import io.netty.channel.ChannelOption;
import io.netty.channel.DefaultChannelConfig;
import io.netty.channel.socket.ServerSocketChannelConfig;
import io.netty.util.NetworkConstants;
import java.io.IOException;
import java.net.StandardSocketOptions;
@ -33,7 +34,7 @@ public class AioServerSocketChannelConfig extends DefaultChannelConfig
implements ServerSocketChannelConfig {
private final AsynchronousServerSocketChannel channel;
private volatile int backlog;
private volatile int backlog = NetworkConstants.SOMAXCONN;
/**
* Creates a new instance.