Skip to content

Commit

Permalink
fix: avoid TIME_WAIT connections to accumulate on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Apr 24, 2024
1 parent 64e1074 commit cb99dba
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/jsonrpc-remote-machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1784,6 +1784,7 @@ static json jsonrpc_machine_verify_send_cmio_response_state_transition_handler(c
/// \param j JSON response object
void jsonrpc_http_reply(mg_connection *con, http_handler_data *h, const json &j) {
SLOG(trace) << h->server_address << " response is " << j.dump().data();
con->is_draining = 1;
return mg_http_reply(con, 200, "Access-Control-Allow-Origin: *\r\nContent-Type: application/json\r\n", "%s",
j.dump().data());
}
Expand All @@ -1792,6 +1793,7 @@ void jsonrpc_http_reply(mg_connection *con, http_handler_data *h, const json &j)
/// \param con Mongoose connection
void jsonrpc_send_empty_reply(mg_connection *con, http_handler_data *h) {
SLOG(trace) << h->server_address << " response is empty";
con->is_draining = 1;
return mg_http_reply(con, 200, "Access-Control-Allow-Origin: *\r\nContent-Type: application/json\r\n", "");
}

Expand Down Expand Up @@ -1897,6 +1899,7 @@ static void http_handler(mg_connection *con, int ev, void *ev_data, void *h_data
headers += "Access-Control-Allow-Methods: *\r\n";
headers += "Access-Control-Allow-Headers: *\r\n";
headers += "Access-Control-Max-Age: 0\r\n";
con->is_draining = 1;
mg_http_reply(con, 204, headers.c_str(), "");
return;
}
Expand All @@ -1905,6 +1908,7 @@ static void http_handler(mg_connection *con, int ev, void *ev_data, void *h_data
std::string headers;
headers += "Access-Control-Allow-Origin: *\r\n";
SLOG(trace) << h->server_address << " rejected unexpected \"" << method << "\" request";
con->is_draining = 1;
mg_http_reply(con, 405, headers.c_str(), "method not allowed");
return;
}
Expand All @@ -1914,6 +1918,7 @@ static void http_handler(mg_connection *con, int ev, void *ev_data, void *h_data
if (uri != "/") {
// anything else
SLOG(trace) << h->server_address << " rejected unexpected \"" << uri << "\" uri";
con->is_draining = 1;
mg_http_reply(con, 404, "Access-Control-Allow-Origin: *\r\n", "not found");
return;
}
Expand Down
2 changes: 0 additions & 2 deletions src/jsonrpc-virtual-machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ struct http_request_data {

// Performs additional client socket configuration
static void setup_client_socket(struct mg_connection *c) {
#if defined(__APPLE__)
#if defined(SO_LINGER)
// Minimize socket close time by setting the linger time to 0.
// On macOS, it avoids accumulating socket in TIME_WAIT state, after rapid successive requests, which can consume
Expand All @@ -73,7 +72,6 @@ static void setup_client_socket(struct mg_connection *c) {
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
(void) setsockopt(socket, SOL_SOCKET, SO_LINGER, reinterpret_cast<char *>(&so_linger), sizeof(so_linger));
#endif
#endif
}

// Print HTTP response and signal that we're done
Expand Down

0 comments on commit cb99dba

Please sign in to comment.