Add Connection::reset
There was an issue where the connection deque was referring to old arena memory
This commit is contained in:
@@ -121,7 +121,6 @@ Key features:
|
|||||||
|
|
||||||
Key features:
|
Key features:
|
||||||
- Arena allocator per connection for efficient memory management
|
- 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
|
- Weak reference to server for safe cleanup after server destruction
|
||||||
- Private networking details accessible only to Server via friend relationship
|
- Private networking details accessible only to Server via friend relationship
|
||||||
- Public handler interface: appendMessage(), closeAfterSend(), getArena(), getId()
|
- 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.)
|
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->appendMessage()` which copies data to arena-backed message queue
|
||||||
4. **Response Writing**: Server writes all queued messages to socket via `writeBytes()`
|
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
|
### API Design
|
||||||
|
|
||||||
|
|||||||
@@ -118,10 +118,6 @@ bool Connection::writeBytes() {
|
|||||||
}
|
}
|
||||||
assert(messages_.empty());
|
assert(messages_.empty());
|
||||||
|
|
||||||
// Reset arena after completing request/response cycle
|
|
||||||
// This reclaims memory from request parsing and response generation
|
|
||||||
arena_.reset();
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -52,6 +52,13 @@ struct Connection {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
void *user_data = nullptr;
|
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
|
// Note: To release connection back to server, use
|
||||||
// Server::releaseBackToServer(std::move(connection_ptr))
|
// Server::releaseBackToServer(std::move(connection_ptr))
|
||||||
|
|||||||
Reference in New Issue
Block a user