Add Connection::reset

There was an issue where the connection deque was referring to old arena memory
This commit is contained in:
2025-08-19 16:03:33 -04:00
parent 05e22fbac2
commit 450bf6dbf1
3 changed files with 10 additions and 7 deletions

View File

@@ -121,7 +121,6 @@ Key features:
Key features:
- Arena allocator per connection for efficient memory management
- **Request/Response arena lifecycle**: Arena resets after each complete request/response cycle
- Weak reference to server for safe cleanup after server destruction
- Private networking details accessible only to Server via friend relationship
- Public handler interface: appendMessage(), closeAfterSend(), getArena(), getId()
@@ -180,9 +179,10 @@ Arena Memory Lifecycle:
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
4. **Response Writing**: Server writes all queued messages to socket via `writeBytes()`
5. **Arena Reset**: After successful write completion, arena resets to reclaim all memory from the request/response cycle
This design assumes request/response pairs (HTTP-like protocols) but works for any protocol where there's a clear completion point for memory reclamation.
The user is responsible for calling `conn->reset()` periodically to reclaim the
arena memory. A good time to do this for a request/response protocol would be
after all outgoing bytes have been written.
### API Design

View File

@@ -118,10 +118,6 @@ bool Connection::writeBytes() {
}
assert(messages_.empty());
// Reset arena after completing request/response cycle
// This reclaims memory from request parsing and response generation
arena_.reset();
return false;
}

View File

@@ -52,6 +52,13 @@ struct Connection {
return result;
}
void *user_data = nullptr;
void reset() {
assert(messages_.empty());
arena_.reset();
messages_ =
std::deque<std::string_view, ArenaStlAllocator<std::string_view>>{
ArenaStlAllocator<std::string_view>{&arena_}};
}
// Note: To release connection back to server, use
// Server::releaseBackToServer(std::move(connection_ptr))