Use snake_case for Connection etc methods
This commit is contained in:
28
design.md
28
design.md
@@ -193,14 +193,14 @@ CommitRequest {
|
||||
1. **Creation**: Accept threads create connections, transfer to epoll as raw pointers
|
||||
2. **Processing**: Network threads claim ownership by wrapping in unique_ptr
|
||||
3. **Handler Transfer**: Handlers can take ownership for async processing via unique_ptr.release()
|
||||
4. **Return Path**: Handlers use Server::releaseBackToServer() to return connections
|
||||
4. **Return Path**: Handlers use Server::release_back_to_server() to return connections
|
||||
5. **Safety**: All transfers use weak_ptr to server for safe cleanup
|
||||
6. **Cleanup**: RAII ensures proper resource cleanup in all scenarios
|
||||
|
||||
#### Arena Memory Lifecycle
|
||||
1. **Request Processing**: Handler uses `conn->getArena()` to allocate memory for parsing request data
|
||||
1. **Request Processing**: Handler uses `conn->get_arena()` to allocate memory for parsing request data
|
||||
2. **Response Generation**: Handler uses arena for temporary response construction (headers, JSON, etc.)
|
||||
3. **Response Queuing**: Handler calls `conn->appendMessage()` which copies data to arena-backed message queue
|
||||
3. **Response Queuing**: Handler calls `conn->append_message()` which copies data to arena-backed message queue
|
||||
4. **Response Writing**: Server writes all queued messages to socket via `writeBytes()`
|
||||
|
||||
> **Note**: Call `conn->reset()` periodically to reclaim arena memory. Best practice is after all outgoing bytes have been written.
|
||||
@@ -262,7 +262,7 @@ See [style.md](style.md) for comprehensive C++ coding standards and conventions.
|
||||
- **Connection Ownership**: Use unique_ptr semantics for safe ownership transfer between components
|
||||
- **Arena Allocator Pattern**: Always use `ArenaAllocator` for temporary allocations within request processing
|
||||
- **String View Usage**: Prefer `std::string_view` over `std::string` when pointing to arena-allocated memory
|
||||
- **Ownership Transfer**: Use `Server::releaseBackToServer()` for returning connections to server from handlers
|
||||
- **Ownership Transfer**: Use `Server::release_back_to_server()` for returning connections to server from handlers
|
||||
- **JSON Token Lookup**: Use the gperf-generated perfect hash table in `json_tokens.hpp` for O(1) key recognition
|
||||
- **Base64 Handling**: Always use simdutf for base64 encoding/decoding for performance
|
||||
- **Thread Safety**: Connection ownership transfers are designed to be thread-safe with proper RAII cleanup
|
||||
@@ -280,9 +280,9 @@ See [style.md](style.md) for comprehensive C++ coding standards and conventions.
|
||||
#### Adding New Protocol Handlers
|
||||
1. Inherit from `ConnectionHandler` in `src/connection_handler.hpp`
|
||||
2. Implement `on_data_arrived()` with proper ownership semantics
|
||||
3. Use connection's arena allocator for temporary allocations: `conn->getArena()`
|
||||
3. Use connection's arena allocator for temporary allocations: `conn->get_arena()`
|
||||
4. Handle partial messages and streaming protocols appropriately
|
||||
5. Use `Server::releaseBackToServer()` if taking ownership for async processing
|
||||
5. Use `Server::release_back_to_server()` if taking ownership for async processing
|
||||
6. Add corresponding test cases and integration tests
|
||||
7. Consider performance implications of ownership transfers
|
||||
|
||||
@@ -345,10 +345,10 @@ class HttpHandler : public ConnectionHandler {
|
||||
public:
|
||||
void on_data_arrived(std::string_view data, std::unique_ptr<Connection>& conn_ptr) override {
|
||||
// Parse HTTP request using connection's arena
|
||||
ArenaAllocator& arena = conn_ptr->getArena();
|
||||
ArenaAllocator& arena = conn_ptr->get_arena();
|
||||
|
||||
// Generate response
|
||||
conn_ptr->appendMessage("HTTP/1.1 200 OK\r\n\r\nHello World");
|
||||
conn_ptr->append_message("HTTP/1.1 200 OK\r\n\r\nHello World");
|
||||
|
||||
// Server retains ownership
|
||||
}
|
||||
@@ -365,10 +365,10 @@ public:
|
||||
|
||||
work_queue.push([connection = std::move(connection)](std::string_view data) mutable {
|
||||
// Process asynchronously
|
||||
connection->appendMessage("Async response");
|
||||
connection->append_message("Async response");
|
||||
|
||||
// Return ownership to server when done
|
||||
Server::releaseBackToServer(std::move(connection));
|
||||
Server::release_back_to_server(std::move(connection));
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -419,7 +419,7 @@ class YesHandler : public ConnectionHandler {
|
||||
public:
|
||||
void on_connection_established(Connection &conn) override {
|
||||
// Write an initial "y\n"
|
||||
conn.appendMessage("y\n");
|
||||
conn.append_message("y\n");
|
||||
}
|
||||
|
||||
void on_write_progress(std::unique_ptr<Connection> &conn) override {
|
||||
@@ -427,7 +427,7 @@ public:
|
||||
// Don't use an unbounded amount of memory
|
||||
conn->reset();
|
||||
// Write "y\n" repeatedly
|
||||
conn->appendMessage("y\n");
|
||||
conn->append_message("y\n");
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -452,10 +452,10 @@ Connection* raw_conn = conn_ptr.release();
|
||||
// Process on worker thread
|
||||
background_processor.submit([raw_conn]() {
|
||||
// Do work...
|
||||
raw_conn->appendMessage("Background result");
|
||||
raw_conn->append_message("Background result");
|
||||
|
||||
// Return to server safely (handles server destruction)
|
||||
Server::releaseBackToServer(std::unique_ptr<Connection>(raw_conn));
|
||||
Server::release_back_to_server(std::unique_ptr<Connection>(raw_conn));
|
||||
});
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user