Skip to content

Commit 2e6d756

Browse files
author
Maciej Soltysiak
committed
Add support for SO_REUSEPORT. This is also a workaround for libevent's current lack of support for this sockopt.
1 parent 7cc77b0 commit 2e6d756

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

tcp_request.c

+10-1
Original file line numberDiff line numberDiff line change
@@ -401,10 +401,19 @@ tcp_listener_bind(struct context *c)
401401
# define LEV_OPT_DEFERRED_ACCEPT 0
402402
#endif
403403

404+
/* Until libevent gets support for SO_REUSEPORT we have to break
405+
* evconnlistener_new_bind() into a series of:
406+
* socket(), tcp_tune(), bind(), evconnlistener_new() */
404407
evutil_socket_t fd;
405408
fd = socket(c->local_sockaddr.ss_family, SOCK_STREAM, IPPROTO_TCP);
406409

407410
tcp_tune(fd);
411+
evutil_make_socket_nonblocking(fd);
412+
413+
if (bind(fd, (struct sockaddr *) &c->local_sockaddr, c->local_sockaddr_len) < 0) {
414+
logger(LOG_ERR, "Unable to bind (TCP): %s");
415+
return -1;
416+
}
408417

409418
c->tcp_conn_listener =
410419
evconnlistener_new(c->event_loop,
@@ -416,7 +425,7 @@ tcp_listener_bind(struct context *c)
416425
TCP_REQUEST_BACKLOG,
417426
fd);
418427
if (c->tcp_conn_listener == NULL) {
419-
logger(LOG_ERR, "Unable to bind (TCP)");
428+
logger(LOG_ERR, "Unable to create listener (TCP)");
420429
return -1;
421430
}
422431
if (evconnlistener_disable(c->tcp_conn_listener) != 0) {

0 commit comments

Comments
 (0)