Skip to content

Commit

Permalink
If binding a TCP socket with SO_REUSEPORT fails, retry without it
Browse files Browse the repository at this point in the history
This is required at least on alpine/armhf
  • Loading branch information
jedisct1 committed Jan 17, 2017
1 parent 94aa601 commit d1c1916
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/proxy/tcp_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -559,20 +559,23 @@ tcp_listener_bind(ProxyContext * const proxy_context)
# define LEV_OPT_DEFERRED_ACCEPT 0
#endif
if (proxy_context->tcp_listener_handle == -1) {
proxy_context->tcp_conn_listener =
evconnlistener_new_bind(proxy_context->event_loop,
tcp_connection_cb, proxy_context,
LEV_OPT_CLOSE_ON_FREE |
LEV_OPT_CLOSE_ON_EXEC |
LEV_OPT_REUSEABLE |
#ifndef NO_REUSEPORT
LEV_OPT_REUSEABLE_PORT |
#endif
LEV_OPT_DEFERRED_ACCEPT,
TCP_REQUEST_BACKLOG,
(struct sockaddr *)
&proxy_context->local_sockaddr,
(int) proxy_context->local_sockaddr_len);
unsigned int flags = LEV_OPT_CLOSE_ON_FREE | LEV_OPT_CLOSE_ON_EXEC |
LEV_OPT_REUSEABLE | LEV_OPT_REUSEABLE_PORT |
LEV_OPT_DEFERRED_ACCEPT;
for (;;) {
proxy_context->tcp_conn_listener =
evconnlistener_new_bind(proxy_context->event_loop,
tcp_connection_cb, proxy_context,
flags, TCP_REQUEST_BACKLOG,
(struct sockaddr *)
&proxy_context->local_sockaddr,
(int) proxy_context->local_sockaddr_len);
if (proxy_context->tcp_conn_listener != NULL ||
(flags & LEV_OPT_REUSEABLE_PORT) == 0U) {
break;
}
flags &= ~LEV_OPT_REUSEABLE_PORT;
}
} else {
evutil_make_socket_closeonexec(proxy_context->tcp_listener_handle);
evutil_make_socket_nonblocking(proxy_context->tcp_listener_handle);
Expand Down

0 comments on commit d1c1916

Please sign in to comment.