Skip to content

Commit

Permalink
[PATCH] Added patch to work with libevent2, which also required some …
Browse files Browse the repository at this point in the history
…modifications on HipHop code
  • Loading branch information
dpaneda committed Oct 20, 2011
1 parent 40934db commit 658c910
Show file tree
Hide file tree
Showing 5 changed files with 392 additions and 23 deletions.
2 changes: 1 addition & 1 deletion CMake/HPHPFindLibs.cmake
Expand Up @@ -56,7 +56,7 @@ find_package(LibEvent REQUIRED)
include_directories(${LIBEVENT_INCLUDE_DIR})

set(CMAKE_REQUIRED_LIBRARIES "${LIBEVENT_LIB}")
CHECK_FUNCTION_EXISTS("evhttp_bind_socket_with_fd" HAVE_CUSTOM_LIBEVENT)
CHECK_FUNCTION_EXISTS("evhttp_bind_socket_with_handle" HAVE_CUSTOM_LIBEVENT)
if (NOT HAVE_CUSTOM_LIBEVENT)
unset(HAVE_CUSTOM_LIBEVENT CACHE)
unset(LIBEVENT_INCLUDE_DIR CACHE)
Expand Down
15 changes: 8 additions & 7 deletions src/runtime/base/server/libevent_server.cpp
Expand Up @@ -152,6 +152,8 @@ LibEventServer::LibEventServer(const std::string &address, int port,
: Server(address, port, thread),
m_accept_sock(-1),
m_accept_sock_ssl(-1),
evhttp_sock(NULL),
evhttp_sock_ssl(NULL),
m_timeoutThreadData(thread, timeoutSeconds),
m_timeoutThread(&m_timeoutThreadData, &TimeoutThread::run),
m_dispatcher(thread, RuntimeOption::ServerThreadRoundRobin,
Expand Down Expand Up @@ -185,15 +187,14 @@ LibEventServer::~LibEventServer() {
// implementing HttpServer

int LibEventServer::getAcceptSocket() {
int ret;
const char *address = m_address.empty() ? NULL : m_address.c_str();
ret = evhttp_bind_socket_backlog_fd(m_server, address,
evhttp_sock = evhttp_bind_socket_backlog_with_handle(m_server, address,
m_port, RuntimeOption::ServerBacklog);
if (ret < 0) {
if (!evhttp_sock) {
Logger::Error("Fail to bind port %d", m_port);
return -1;
}
m_accept_sock = ret;
m_accept_sock = evhttp_bound_socket_get_fd(evhttp_sock);
return 0;
}

Expand Down Expand Up @@ -311,14 +312,14 @@ bool LibEventServer::enableSSL(void *sslCTX, int port) {

int LibEventServer::getAcceptSocketSSL() {
const char *address = m_address.empty() ? NULL : m_address.c_str();
int ret = evhttp_bind_socket_backlog_fd(m_server_ssl, address,
evhttp_sock_ssl = evhttp_bind_socket_backlog_with_handle(m_server_ssl, address,
m_port_ssl, RuntimeOption::ServerBacklog);
if (ret < 0) {
if (!evhttp_sock_ssl) {
Logger::Error("Failed to bind port %d for SSL", m_port_ssl);
return -1;
}
Logger::Info("SSL enabled");
m_accept_sock_ssl = ret;
m_accept_sock_ssl = evhttp_bound_socket_get_fd(evhttp_sock_ssl);
return 0;
}

Expand Down
3 changes: 3 additions & 0 deletions src/runtime/base/server/libevent_server.h
Expand Up @@ -173,6 +173,9 @@ class LibEventServer : public Server {

int m_accept_sock;
int m_accept_sock_ssl;
struct evhttp_bound_socket *evhttp_sock;
struct evhttp_bound_socket *evhttp_sock_ssl;

event_base *m_eventBase;
evhttp *m_server;

Expand Down
20 changes: 5 additions & 15 deletions src/runtime/base/server/libevent_server_with_takeover.cpp
Expand Up @@ -94,19 +94,13 @@ int LibEventServerWithTakeover::afdtRequest(String request, String* response) {
Logger::Info("takeover: received request");
if (request == P_VERSION C_FD_REQ) {
Logger::Info("takeover: request is a listen socket request");
int ret;
*response = P_VERSION C_FD_RESP;
// Make evhttp forget our copy of the accept socket so we don't accept any
// more connections and drop them. Keep the socket open until we get the
// shutdown request so that we can still serve AFDT requests (if the new
// server crashes or something). The downside is that it will take the LB
// longer to figure out that we are broken.
ret = evhttp_del_accept_socket(m_server, m_accept_sock);
if (ret < 0) {
// This will fail if we get a second AFDT request, but the spurious
// log message is not too harmful.
Logger::Error("Unable to delete accept socket");
}
evhttp_del_accept_socket(m_server, evhttp_sock);
return m_accept_sock;
} else if (request == P_VERSION C_TERM_REQ) {
Logger::Info("takeover: request is a terminate request");
Expand All @@ -125,11 +119,7 @@ int LibEventServerWithTakeover::afdtRequest(String request, String* response) {
// Close SSL server
if (m_server_ssl) {
ASSERT(m_accept_sock_ssl > 0);
ret = evhttp_del_accept_socket(m_server_ssl, m_accept_sock_ssl);
if (ret < 0) {
Logger::Error("Unable to delete accept socket for SSL in evhttp");
return -1;
}
evhttp_del_accept_socket(m_server_ssl, evhttp_sock_ssl);
ret = close(m_accept_sock_ssl);
if (ret < 0) {
Logger::Error("Unable to close accept socket for SSL");
Expand Down Expand Up @@ -195,11 +185,11 @@ int LibEventServerWithTakeover::getAcceptSocket() {
m_accept_sock = -1;
}

ret = evhttp_bind_socket_backlog_fd(m_server, address,
evhttp_sock = evhttp_bind_socket_backlog_with_handle(m_server, address,
m_port, RuntimeOption::ServerBacklog);
if (ret >= 0) {
if (evhttp_sock) {
Logger::Info("takeover: bound directly to port %d", m_port);
m_accept_sock = ret;
m_accept_sock = evhttp_bound_socket_get_fd(evhttp_sock);
return 0;
} else if (errno != EADDRINUSE) {
return -1;
Expand Down

0 comments on commit 658c910

Please sign in to comment.