Skip to content

Commit

Permalink
Merge pull request #2837 from 4144/concount
Browse files Browse the repository at this point in the history
Add configure option for set max connections count
  • Loading branch information
MishimaHaruna committed Sep 20, 2020
2 parents b2cf4bc + 229dc7d commit 059804b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 34 deletions.
13 changes: 6 additions & 7 deletions configure
@@ -1,5 +1,5 @@
#! /bin/sh
# From configure.ac 6d5542195.
# From configure.ac eaac1e714.
# Guess values for system-dependent variables and create Makefiles.
# Generated by GNU Autoconf 2.69.
#
Expand Down Expand Up @@ -1407,7 +1407,8 @@ Optional Packages:
--with-key3[=ARG] Set the third obfuscation key (ignored unless the
other two are also specified)
--with-maxconn[=ARG] optionally set the maximum connections the core can
handle (default: 16384) NOT USED YET - EXPERIMENTAL
handle (Without epoll enabled, default: 1024. With
epol enabled: 3072)
--with-mysql[=ARG] optionally specify the path to the mysql_config
executable
--with-MYSQL_CFLAGS=ARG specify MYSQL_CFLAGS manually (instead of using
Expand Down Expand Up @@ -4016,10 +4017,7 @@ fi
# Check whether --with-maxconn was given.
if test "${with_maxconn+set}" = set; then :
withval=$with_maxconn;
if test "$withval" == "no"; then
CPPFLAGS="$CPPFLAGS -DMAXCONN=16384"
else
if test "$withval" != "no"; then
if ! test "$withval" -ge 0 -o "$withval" -lt 0 2>&- ; then
as_fn_error $? "Invalid argument --with-maxconn=$withval ... stopping" "$LINENO" 5
else
Expand All @@ -4029,7 +4027,8 @@ if test "${with_maxconn+set}" = set; then :
else
CPPFLAGS="$CPPFLAGS -DMAXCONN=16384"
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: Use default MAXCONN value." >&5
$as_echo "Use default MAXCONN value." >&6; }
fi
Expand Down
9 changes: 3 additions & 6 deletions configure.ac
Expand Up @@ -566,13 +566,10 @@ AC_ARG_WITH(
[maxconn],
AC_HELP_STRING(
[--with-maxconn@<:@=ARG@:>@],
[optionally set the maximum connections the core can handle (default: 16384) NOT USED YET - EXPERIMENTAL]
[optionally set the maximum connections the core can handle (Without epoll enabled, default: 1024. With epol enabled: 3072)]
),
[
if test "$withval" == "no"; then
CPPFLAGS="$CPPFLAGS -DMAXCONN=16384"
else
if test "$withval" != "no"; then
if ! test "$withval" -ge 0 -o "$withval" -lt 0 2>&- ; then
AC_MSG_ERROR([Invalid argument --with-maxconn=$withval ... stopping])
else
Expand All @@ -581,7 +578,7 @@ AC_ARG_WITH(
fi
],
[
CPPFLAGS="$CPPFLAGS -DMAXCONN=16384"
AC_MSG_RESULT([Use default MAXCONN value.])
]
)

Expand Down
50 changes: 29 additions & 21 deletions src/common/socket.c
Expand Up @@ -39,6 +39,14 @@
#include <stdlib.h>
#include <sys/types.h>

#ifndef MAXCONN
#ifdef SOCKET_EPOLL
#define MAXCONN 3072
#else // SOCKET_EPOLL
#define MAXCONN FD_SETSIZE
#endif // SOCKET_EPOLL
#endif // MAXCONN

#ifdef SOCKET_EPOLL
#include <sys/epoll.h>
#endif // SOCKET_EPOLL
Expand Down Expand Up @@ -106,7 +114,7 @@ typedef int socklen_t;

// global array of sockets (emulating linux)
// fd is the position in the array
static SOCKET sock_arr[FD_SETSIZE];
static SOCKET sock_arr[MAXCONN];
static int sock_arr_len = 0;

/// Returns the socket associated with the target fd.
Expand Down Expand Up @@ -137,7 +145,7 @@ static int sock2fd(SOCKET s)
/// Returns a new fd associated with the socket.
/// If there are too many sockets it closes the socket, sets an error and
// returns -1 instead.
/// Since fd 0 is reserved, it returns values in the range [1,FD_SETSIZE[.
/// Since fd 0 is reserved, it returns values in the range [1,MAXCONN[.
///
/// @param s Socket
/// @return New fd or -1
Expand Down Expand Up @@ -260,7 +268,7 @@ static fd_set readfds;

#else // SOCKET_EPOLL
// Epoll based Event Dispatcher:
static int epoll_maxevents = (FD_SETSIZE / 2);
static int epoll_maxevents = (MAXCONN / 2);
static int epfd = SOCKET_ERROR;
static struct epoll_event epevent;
static struct epoll_event *epevents = NULL;
Expand Down Expand Up @@ -293,9 +301,9 @@ static time_t socket_data_last_tick = 0;
#define WFIFO_MAX (1*1024*1024)

#ifdef SEND_SHORTLIST
static int send_shortlist_array[FD_SETSIZE];// we only support FD_SETSIZE sockets, limit the array to that
static int send_shortlist_array[MAXCONN]; // we only support MAXCONN sockets, limit the array to that
static int send_shortlist_count = 0;// how many fd's are in the shortlist
static uint32 send_shortlist_set[(FD_SETSIZE+31)/32];// to know if specific fd's are already in the shortlist
static uint32 send_shortlist_set[(MAXCONN + 31) / 32]; // to know if specific fd's are already in the shortlist
#endif // SEND_SHORTLIST

static int create_session(int fd, RecvFunc func_recv, SendFunc func_send, ParseFunc func_parse);
Expand Down Expand Up @@ -545,8 +553,8 @@ static int connect_client(int listen_fd)
sClose(fd);
return -1;
}
if( fd >= FD_SETSIZE ) { // socket number too big
ShowError("connect_client: New socket #%d is greater than can we handle! Increase the value of FD_SETSIZE (currently %d) for your OS to fix this!\n", fd, FD_SETSIZE);
if (fd >= MAXCONN) { // socket number too big
ShowError("connect_client: New socket #%d is greater than can we handle! Increase the value of MAXCONN (currently %d) for your OS to fix this!\n", fd, MAXCONN);
sClose(fd);
return -1;
}
Expand Down Expand Up @@ -602,8 +610,8 @@ static int make_listen_bind(uint32 ip, uint16 port)
sClose(fd);
return -1;
}
if( fd >= FD_SETSIZE ) { // socket number too big
ShowError("make_listen_bind: New socket #%d is greater than can we handle! Increase the value of FD_SETSIZE (currently %d) for your OS to fix this!\n", fd, FD_SETSIZE);
if (fd >= MAXCONN) { // socket number too big
ShowError("make_listen_bind: New socket #%d is greater than can we handle! Increase the value of MAXCONN (currently %d) for your OS to fix this!\n", fd, MAXCONN);
sClose(fd);
return -1;
}
Expand Down Expand Up @@ -671,8 +679,8 @@ static int make_connection(uint32 ip, uint16 port, struct hSockOpt *opt)
sClose(fd);
return -1;
}
if( fd >= FD_SETSIZE ) {// socket number too big
ShowError("make_connection: New socket #%d is greater than can we handle! Increase the value of FD_SETSIZE (currently %d) for your OS to fix this!\n", fd, FD_SETSIZE);
if (fd >= MAXCONN) {// socket number too big
ShowError("make_connection: New socket #%d is greater than can we handle! Increase the value of MAXCONN (currently %d) for your OS to fix this!\n", fd, MAXCONN);
sClose(fd);
return -1;
}
Expand Down Expand Up @@ -1557,7 +1565,7 @@ static void socket_final(void)
/// Closes a socket.
static void socket_close(int fd)
{
if( fd <= 0 ||fd >= FD_SETSIZE )
if (fd <= 0 ||fd >= MAXCONN)
return;// invalid

sockt->flush(fd); // Try to send what's left (although it might not succeed since it's a nonblocking socket)
Expand Down Expand Up @@ -1662,7 +1670,7 @@ static int socket_getips(uint32 *ips, int max)

static void socket_init(void)
{
uint64 rlim_cur = FD_SETSIZE;
uint64 rlim_cur = MAXCONN;

#ifdef WIN32
{// Start up windows networking
Expand All @@ -1682,14 +1690,14 @@ static void socket_init(void)
#elif defined(HAVE_SETRLIMIT) && !defined(CYGWIN)
// NOTE: getrlimit and setrlimit have bogus behavior in cygwin.
// "Number of fds is virtually unlimited in cygwin" (sys/param.h)
{// set socket limit to FD_SETSIZE
{// set socket limit to MAXCONN
struct rlimit rlp;
if( 0 == getrlimit(RLIMIT_NOFILE, &rlp) )
{
rlp.rlim_cur = FD_SETSIZE;
rlp.rlim_cur = MAXCONN;
if( 0 != setrlimit(RLIMIT_NOFILE, &rlp) )
{// failed, try setting the maximum too (permission to change system limits is required)
rlp.rlim_max = FD_SETSIZE;
rlp.rlim_max = MAXCONN;
if( 0 != setrlimit(RLIMIT_NOFILE, &rlp) )
{// failed
const char *errmsg = error_msg();
Expand All @@ -1702,7 +1710,7 @@ static void socket_init(void)
// report limit
getrlimit(RLIMIT_NOFILE, &rlp);
rlim_cur = rlp.rlim_cur;
ShowWarning("socket_init: failed to set socket limit to %d, setting to maximum allowed (original limit=%d, current limit=%d, maximum allowed=%d, %s).\n", FD_SETSIZE, rlim_ori, (int)rlp.rlim_cur, (int)rlp.rlim_max, errmsg);
ShowWarning("socket_init: failed to set socket limit to %d, setting to maximum allowed (original limit=%d, current limit=%d, maximum allowed=%d, %s).\n", MAXCONN, rlim_ori, (int)rlp.rlim_cur, (int)rlp.rlim_max, errmsg);
}
}
}
Expand All @@ -1724,7 +1732,7 @@ static void socket_init(void)

#else // SOCKET_EPOLL
// Epoll based Event Dispatcher:
epfd = epoll_create(FD_SETSIZE); // 2.6.8 or newer ignores the expected socket amount argument
epfd = epoll_create(MAXCONN); // 2.6.8 or newer ignores the expected socket amount argument
if(epfd == SOCKET_ERROR){
ShowError("Failed to Create Epoll Event Dispatcher: %s\n", error_msg());
exit(EXIT_FAILURE);
Expand All @@ -1741,7 +1749,7 @@ static void socket_init(void)
memset(send_shortlist_set, 0, sizeof(send_shortlist_set));
#endif // defined(SEND_SHORTLIST)

CREATE(sockt->session, struct socket_data *, FD_SETSIZE);
CREATE(sockt->session, struct socket_data *, MAXCONN);

// initialize last send-receive tick
sockt->last_tick = time(NULL);
Expand All @@ -1760,7 +1768,7 @@ static void socket_init(void)

static bool session_is_valid(int fd)
{
return ( fd > 0 && fd < FD_SETSIZE && sockt->session[fd] != NULL );
return ( fd > 0 && fd < MAXCONN && sockt->session[fd] != NULL );
}

static bool session_is_active(int fd)
Expand Down Expand Up @@ -1920,7 +1928,7 @@ static void send_shortlist_do_sends(void)
send_shortlist_array[i] = send_shortlist_array[send_shortlist_count];
send_shortlist_array[send_shortlist_count] = 0;

if( fd <= 0 || fd >= FD_SETSIZE )
if (fd <= 0 || fd >= MAXCONN)
{
ShowDebug("send_shortlist_do_sends: fd is out of range, corrupted memory? (fd=%d)\n", fd);
continue;
Expand Down

0 comments on commit 059804b

Please sign in to comment.