Skip to content

Commit

Permalink
deduct the max number of connections using getrlimit
Browse files Browse the repository at this point in the history
instead of requiring to provide it at construction time
  • Loading branch information
Andrea Guzzo committed Apr 12, 2014
1 parent 14d066b commit ed353ca
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/iomtee.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ static void *tee_run(void *arg) {
iomtee_t *iomtee_open(int *vfd, int num_fds, ...)
{
iomtee_t *tee = calloc(1, sizeof(iomtee_t));
tee->iomux = iomux_create(0, 0, 1);
tee->iomux = iomux_create(0, 1);
int rc = pipe(tee->pipe);
if (rc != 0) {
fprintf(stderr, "Can't create pipe : %s\n", strerror(errno));
Expand Down
14 changes: 12 additions & 2 deletions src/iomux.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

#include <stdarg.h>

#include <sys/resource.h>

#if defined(HAVE_EPOLL)
#include <sys/epoll.h>
#elif defined(HAVE_KQUEUE)
Expand Down Expand Up @@ -124,7 +126,7 @@ static void set_error(iomux_t *iomux, char *fmt, ...) {
static void iomux_handle_timeout(iomux_t *iomux, void *priv);

iomux_t *
iomux_create(int max_connections, int bufsize, int threadsafe)
iomux_create(int bufsize, int threadsafe)
{
iomux_t *iomux = (iomux_t *)calloc(1, sizeof(iomux_t));

Expand All @@ -134,7 +136,15 @@ iomux_create(int max_connections, int bufsize, int threadsafe)
}

iomux->bufsize = (bufsize > 0) ? bufsize : IOMUX_CONNECTION_BUFSIZE_DEFAULT;
iomux->maxconnections = (max_connections > 0) ? max_connections : IOMUX_CONNECTIONS_MAX_DEFAULT;

struct rlimit rlim;
if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
iomux->maxconnections = rlim.rlim_max;
} else {
fprintf(stderr, "Can't get the max number of filedescriptors: %s\n",
strerror(errno));
iomux->maxconnections = IOMUX_CONNECTIONS_MAX_DEFAULT;
}

#if defined(HAVE_EPOLL)
iomux->efd = epoll_create1(0);
Expand Down
2 changes: 1 addition & 1 deletion src/iomux.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ typedef struct __iomux_callbacks {
* @brief Create a new iomux handler
* @returns A valid iomux handler
*/
iomux_t *iomux_create(int max_connections, int bufsize, int threadsafe);
iomux_t *iomux_create(int bufsize, int threadsafe);

/**
* @brief Add a filedescriptor to the mux
Expand Down
6 changes: 3 additions & 3 deletions test/iomux_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,8 @@ main(int argc, char **argv)

ut_init(basename(argv[0]));

ut_testing("iomux_create(0, 0, 0)");
mux = iomux_create(0, 0, 0);
ut_testing("iomux_create(0, 0)");
mux = iomux_create(0, 0);
if (mux)
ut_success();
else
Expand Down Expand Up @@ -361,7 +361,7 @@ main(int argc, char **argv)
};

server = open_socket("localhost", TEST_SERVER_PORT);
mux = iomux_create(0, 0, 0);
mux = iomux_create(0, 0);
iomux_add(mux, server, &cbs);
iomux_listen(mux, server);

Expand Down

0 comments on commit ed353ca

Please sign in to comment.