Skip to content

Commit

Permalink
perf: reuse_port must be called explicitly
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
  • Loading branch information
zhaojh329 committed Nov 2, 2021
1 parent a1f0e81 commit ef0fc98
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
2 changes: 2 additions & 0 deletions example/multi_process_server_reuseport.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ static void start_server(const char *addr, const char *docroot)
if (!srv)
return;

srv->reuse_port(srv, true);

if (srv->listen(srv, addr, false) < 0)
return;

Expand Down
15 changes: 13 additions & 2 deletions src/uhttpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,13 @@ static void uh_set_loop(struct uh_server *srv, struct ev_loop *loop)
srvi->loop = loop;
}

static void uh_reuse_port(struct uh_server *srv, bool val)
{
struct uh_server_internal *srvi = (struct uh_server_internal *)srv;

srvi->reuse_port = val;
}

static int parse_address(const char *addr, char **host, char **port)
{
static char buf[256];
Expand Down Expand Up @@ -420,11 +427,14 @@ static int uh_server_listen(struct uh_server *srv, const char *addr, bool ssl)

/* required to get parallel v4 + v6 working */
if (p->ai_family == AF_INET6 && setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(int)) < 0) {
log_err("setsockopt: %s\n", strerror(errno));
log_err("setsockopt: IPV6_V6ONLY: %s\n", strerror(errno));
goto err;
}

setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &on, sizeof(int));
if (srvi->reuse_port && setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &on, sizeof(int))) {
log_err("setsockopt: SO_REUSEPORT: %s\n", strerror(errno));
goto err;
}

if (bind(sock, p->ai_addr, p->ai_addrlen) < 0) {
log_err("bind: %s\n", strerror(errno));
Expand Down Expand Up @@ -492,6 +502,7 @@ void uh_server_init(struct uh_server *srv, struct ev_loop *loop)
srv->set_loop = uh_set_loop;
srv->free = uh_server_free;

srv->reuse_port = uh_reuse_port;
srv->listen = uh_server_listen;

#ifdef SSL_SUPPORT
Expand Down
1 change: 1 addition & 0 deletions src/uhttpd.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ struct uh_server {
/* Replace the existing loop. Can only be called before calling the listen */
void (*set_loop)(struct uh_server *srv, struct ev_loop *loop);
void (*free)(struct uh_server *srv);
void (*reuse_port)(struct uh_server *srv, bool val);
/*
** listen an address, multiple call allowed
** returns the number of successful listen
Expand Down
1 change: 1 addition & 0 deletions src/uhttpd_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ struct uh_server_internal {
struct uh_server com;
char *docroot;
char *index_page;
bool reuse_port;
bool https_redirect;
struct ev_loop *loop;
void (*conn_closed_cb)(struct uh_connection *conn);
Expand Down

0 comments on commit ef0fc98

Please sign in to comment.