Use snake_case for Connection etc methods
This commit is contained in:
@@ -71,7 +71,7 @@
|
||||
* 3. **Background Thread**: Can receive ownership for async processing, uses
|
||||
* arena for temporary data structures
|
||||
* 4. **Return Path**: Connection (and its arena) safely returned via
|
||||
* Server::releaseBackToServer()
|
||||
* Server::release_back_to_server()
|
||||
*
|
||||
* ### Why This Design is Thread-Safe:
|
||||
* - **Exclusive Access**: Only the current owner thread should access the arena
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
|
||||
#include "server.hpp" // Need this for releaseBackToServer implementation
|
||||
#include "server.hpp" // Need this for release_back_to_server implementation
|
||||
|
||||
// Static thread-local storage for iovec buffer
|
||||
static thread_local std::vector<struct iovec> g_iovec_buffer{IOV_MAX};
|
||||
@@ -35,7 +35,7 @@ Connection::~Connection() {
|
||||
// EINTR ignored - fd is guaranteed closed on Linux
|
||||
}
|
||||
|
||||
void Connection::appendMessage(std::string_view s, bool copy_to_arena) {
|
||||
void Connection::append_message(std::string_view s, bool copy_to_arena) {
|
||||
if (copy_to_arena) {
|
||||
char *arena_str = arena_.allocate<char>(s.size());
|
||||
std::memcpy(arena_str, s.data(), s.size());
|
||||
|
||||
@@ -83,12 +83,12 @@ struct Connection {
|
||||
*
|
||||
* Example usage:
|
||||
* ```cpp
|
||||
* conn->appendMessage("HTTP/1.1 200 OK\r\n\r\n", false); // Static string
|
||||
* conn->appendMessage(dynamic_response, true); // Dynamic data
|
||||
* conn->appendMessage(arena_allocated_data, false); // Arena data
|
||||
* conn->append_message("HTTP/1.1 200 OK\r\n\r\n", false); // Static string
|
||||
* conn->append_message(dynamic_response, true); // Dynamic data
|
||||
* conn->append_message(arena_allocated_data, false); // Arena data
|
||||
* ```
|
||||
*/
|
||||
void appendMessage(std::string_view s, bool copy_to_arena = true);
|
||||
void append_message(std::string_view s, bool copy_to_arena = true);
|
||||
|
||||
/**
|
||||
* @brief Mark the connection to be closed after sending all queued messages.
|
||||
@@ -108,11 +108,11 @@ struct Connection {
|
||||
*
|
||||
* Typical usage:
|
||||
* ```cpp
|
||||
* conn->appendMessage("HTTP/1.1 200 OK\r\n\r\nBye!");
|
||||
* conn->closeAfterSend(); // Close after sending response
|
||||
* conn->append_message("HTTP/1.1 200 OK\r\n\r\nBye!");
|
||||
* conn->close_after_send(); // Close after sending response
|
||||
* ```
|
||||
*/
|
||||
void closeAfterSend() { closeConnection_ = true; }
|
||||
void close_after_send() { closeConnection_ = true; }
|
||||
|
||||
/**
|
||||
* @brief Get access to the connection's arena allocator.
|
||||
@@ -135,7 +135,7 @@ struct Connection {
|
||||
*
|
||||
* Best practices:
|
||||
* ```cpp
|
||||
* ArenaAllocator& arena = conn->getArena();
|
||||
* ArenaAllocator& arena = conn->get_arena();
|
||||
*
|
||||
* // Allocate temporary parsing buffers
|
||||
* char* buffer = arena.allocate<char>(1024);
|
||||
@@ -147,7 +147,7 @@ struct Connection {
|
||||
* std::vector<Token, ArenaStlAllocator<Token>> tokens{&arena};
|
||||
* ```
|
||||
*/
|
||||
ArenaAllocator &getArena() { return arena_; }
|
||||
ArenaAllocator &get_arena() { return arena_; }
|
||||
|
||||
/**
|
||||
* @brief Get the unique identifier for this connection.
|
||||
@@ -168,19 +168,19 @@ struct Connection {
|
||||
*
|
||||
* Typical usage:
|
||||
* ```cpp
|
||||
* std::cout << "Processing request on connection " << conn->getId() <<
|
||||
* std::endl; logger.info("Connection {} sent {} bytes", conn->getId(),
|
||||
* std::cout << "Processing request on connection " << conn->get_id() <<
|
||||
* std::endl; logger.info("Connection {} sent {} bytes", conn->get_id(),
|
||||
* response.size());
|
||||
* ```
|
||||
*/
|
||||
int64_t getId() const { return id_; }
|
||||
int64_t get_id() const { return id_; }
|
||||
|
||||
/**
|
||||
* @brief Get the number of bytes queued for transmission.
|
||||
*
|
||||
* Returns the total number of bytes in all messages currently
|
||||
* queued for transmission to the client. This includes all data added via
|
||||
* appendMessage() that has not yet been sent over the network.
|
||||
* append_message() that has not yet been sent over the network.
|
||||
*
|
||||
* @return Total bytes queued for transmission
|
||||
*
|
||||
@@ -206,7 +206,7 @@ struct Connection {
|
||||
* }
|
||||
*
|
||||
* // Logging/monitoring
|
||||
* metrics.recordQueueDepth(conn->getId(), conn->outgoingBytesQueued());
|
||||
* metrics.recordQueueDepth(conn->get_id(), conn->outgoingBytesQueued());
|
||||
* ```
|
||||
*/
|
||||
int64_t outgoingBytesQueued() const {
|
||||
@@ -248,7 +248,7 @@ struct Connection {
|
||||
* class HttpHandler : public ConnectionHandler {
|
||||
* void on_connection_established(Connection& conn) override {
|
||||
* // Allocate HTTP state in connection's arena or heap
|
||||
* auto* state = conn.getArena().construct<HttpConnectionState>();
|
||||
* auto* state = conn.get_arena().construct<HttpConnectionState>();
|
||||
* conn.user_data = state;
|
||||
* }
|
||||
*
|
||||
@@ -299,7 +299,7 @@ struct Connection {
|
||||
* @note Ownership Transfer: To release a connection back to the server for
|
||||
* continued processing, use the static method:
|
||||
* ```cpp
|
||||
* Server::releaseBackToServer(std::move(connection_ptr));
|
||||
* Server::release_back_to_server(std::move(connection_ptr));
|
||||
* ```
|
||||
*
|
||||
* This is the correct way to return connection ownership when:
|
||||
|
||||
@@ -30,10 +30,10 @@ public:
|
||||
*
|
||||
* Implementation should:
|
||||
* - Parse incoming data using arena allocator when needed
|
||||
* - Use conn_ptr->appendMessage() to queue response data to be sent
|
||||
* - Use conn_ptr->append_message() to queue response data to be sent
|
||||
* - Handle partial messages and streaming protocols appropriately
|
||||
* - Can take ownership by calling conn_ptr.release() to pass to other threads
|
||||
* - If ownership is taken, handler must call Server::releaseBackToServer()
|
||||
* - If ownership is taken, handler must call Server::release_back_to_server()
|
||||
* when done
|
||||
* @note `data` is *not* owned by the connection arena, and its lifetime ends
|
||||
* after the call to on_data_arrived.
|
||||
|
||||
@@ -28,7 +28,7 @@ HttpConnectionState::HttpConnectionState(ArenaAllocator &arena)
|
||||
// HttpHandler implementation
|
||||
void HttpHandler::on_connection_established(Connection &conn) {
|
||||
// Allocate HTTP state in connection's arena
|
||||
ArenaAllocator &arena = conn.getArena();
|
||||
ArenaAllocator &arena = conn.get_arena();
|
||||
void *mem = arena.allocate_raw(sizeof(HttpConnectionState),
|
||||
alignof(HttpConnectionState));
|
||||
auto *state = new (mem) HttpConnectionState(arena);
|
||||
@@ -249,7 +249,7 @@ void HttpHandler::handleNotFound(Connection &conn,
|
||||
void HttpHandler::sendResponse(Connection &conn, int status_code,
|
||||
std::string_view content_type,
|
||||
std::string_view body, bool close_connection) {
|
||||
[[maybe_unused]] ArenaAllocator &arena = conn.getArena();
|
||||
[[maybe_unused]] ArenaAllocator &arena = conn.get_arena();
|
||||
|
||||
// Build HTTP response using arena
|
||||
std::string response;
|
||||
@@ -293,7 +293,7 @@ void HttpHandler::sendResponse(Connection &conn, int status_code,
|
||||
|
||||
if (close_connection) {
|
||||
response += "Connection: close\r\n";
|
||||
conn.closeAfterSend(); // Signal connection should be closed after sending
|
||||
conn.close_after_send(); // Signal connection should be closed after sending
|
||||
} else {
|
||||
response += "Connection: keep-alive\r\n";
|
||||
}
|
||||
@@ -301,7 +301,7 @@ void HttpHandler::sendResponse(Connection &conn, int status_code,
|
||||
response += "\r\n";
|
||||
response += body;
|
||||
|
||||
conn.appendMessage(response);
|
||||
conn.append_message(response);
|
||||
}
|
||||
|
||||
void HttpHandler::sendJsonResponse(Connection &conn, int status_code,
|
||||
@@ -313,7 +313,7 @@ void HttpHandler::sendJsonResponse(Connection &conn, int status_code,
|
||||
void HttpHandler::sendErrorResponse(Connection &conn, int status_code,
|
||||
std::string_view message,
|
||||
bool close_connection) {
|
||||
[[maybe_unused]] ArenaAllocator &arena = conn.getArena();
|
||||
[[maybe_unused]] ArenaAllocator &arena = conn.get_arena();
|
||||
|
||||
std::string json = R"({"error":")";
|
||||
json += message;
|
||||
|
||||
@@ -79,7 +79,7 @@ struct HttpHandler : ConnectionHandler {
|
||||
auto *state = static_cast<HttpConnectionState *>(c->user_data);
|
||||
TRACE_EVENT("http", "pipeline thread",
|
||||
perfetto::Flow::Global(state->request_id));
|
||||
Server::releaseBackToServer(std::move(c));
|
||||
Server::release_back_to_server(std::move(c));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,11 +50,11 @@ Server::Server(const weaseldb::Config &config, ConnectionHandler &handler,
|
||||
// Setup shutdown pipe for graceful shutdown
|
||||
setup_shutdown_pipe();
|
||||
|
||||
// Create epoll instances immediately for createLocalConnection() support
|
||||
// Create epoll instances immediately for create_local_connection() support
|
||||
create_epoll_instances();
|
||||
|
||||
// If empty vector provided, listen_fds_ will be empty (no listening)
|
||||
// Server works purely with createLocalConnection()
|
||||
// Server works purely with create_local_connection()
|
||||
}
|
||||
|
||||
Server::~Server() {
|
||||
@@ -137,7 +137,7 @@ void Server::shutdown() {
|
||||
}
|
||||
}
|
||||
|
||||
void Server::releaseBackToServer(std::unique_ptr<Connection> connection) {
|
||||
void Server::release_back_to_server(std::unique_ptr<Connection> connection) {
|
||||
if (!connection) {
|
||||
return; // Nothing to release
|
||||
}
|
||||
@@ -182,7 +182,7 @@ void Server::receiveConnectionBack(std::unique_ptr<Connection> connection) {
|
||||
}
|
||||
}
|
||||
|
||||
int Server::createLocalConnection() {
|
||||
int Server::create_local_connection() {
|
||||
int sockets[2];
|
||||
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) != 0) {
|
||||
perror("socketpair");
|
||||
@@ -195,12 +195,12 @@ int Server::createLocalConnection() {
|
||||
int flags = fcntl(server_fd, F_GETFL, 0);
|
||||
if (flags == -1) {
|
||||
std::fprintf(stderr,
|
||||
"Server::createLocalConnection: fcntl F_GETFL failed\n");
|
||||
"Server::create_local_connection: fcntl F_GETFL failed\n");
|
||||
std::abort();
|
||||
}
|
||||
if (fcntl(server_fd, F_SETFL, flags | O_NONBLOCK) == -1) {
|
||||
std::fprintf(stderr,
|
||||
"Server::createLocalConnection: fcntl F_SETFL failed\n");
|
||||
"Server::create_local_connection: fcntl F_SETFL failed\n");
|
||||
std::abort();
|
||||
}
|
||||
|
||||
|
||||
@@ -92,7 +92,7 @@ struct Server : std::enable_shared_from_this<Server> {
|
||||
* @return File descriptor for the client end of the socketpair, or -1 on
|
||||
* error
|
||||
*/
|
||||
int createLocalConnection();
|
||||
int create_local_connection();
|
||||
|
||||
/**
|
||||
* Release a connection back to its server for continued processing.
|
||||
@@ -105,7 +105,7 @@ struct Server : std::enable_shared_from_this<Server> {
|
||||
*
|
||||
* @param connection unique_ptr to the connection being released back
|
||||
*/
|
||||
static void releaseBackToServer(std::unique_ptr<Connection> connection);
|
||||
static void release_back_to_server(std::unique_ptr<Connection> connection);
|
||||
|
||||
private:
|
||||
friend struct Connection;
|
||||
|
||||
Reference in New Issue
Block a user