Skip to content

Commit

Permalink
control: Limit memory use by control connections
Browse files Browse the repository at this point in the history
Signed-off-by: Mobin "Hojjat" Aydinfar <[email protected]>
  • Loading branch information
mobin-2008 committed Jul 25, 2023
1 parent 19e4523 commit e8f89c5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/control.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,7 @@ bool control_conn_t::queue_packet(const char *pkt, unsigned size) noexcept
// Create a vector out of the (remaining part of the) packet:
try {
outbuf.emplace_back(pkt, pkt + size);
outbuf_size += size;
return true;
}
catch (std::bad_alloc &baexc) {
Expand Down Expand Up @@ -1202,6 +1203,7 @@ bool control_conn_t::queue_packet(std::vector<char> &&pkt) noexcept

try {
outbuf.emplace_back(std::move(pkt));
outbuf_size += pkt.size();
return true;
}
catch (std::bad_alloc &baexc) {
Expand Down Expand Up @@ -1290,6 +1292,7 @@ bool control_conn_t::send_data() noexcept
outpkt_index += written;
if (outpkt_index == pkt.size()) {
// We've finished this packet, move on to the next:
outbuf_size -= pkt.size();
outbuf.pop_front();
outpkt_index = 0;
if (oom_close) {
Expand Down
6 changes: 4 additions & 2 deletions src/includes/control.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ class control_conn_t : private service_listener

// Buffer for outgoing packets. Each outgoing back is represented as a vector<char>.
list<vector<char>> outbuf;
// Current output buffer size in bytes.
unsigned outbuf_size = 0;
// Current index within the first outgoing packet (all previous bytes have been sent).
unsigned outpkt_index = 0;

Expand Down Expand Up @@ -181,7 +183,7 @@ class control_conn_t : private service_listener
bool data_ready() noexcept;

bool send_data() noexcept;

// Check if any dependents will be affected by stopping a service, generate a response packet if so.
// had_dependents will be set true if the service should not be stopped, false otherwise.
// Returns false if the connection must be closed, true otherwise.
Expand Down Expand Up @@ -250,7 +252,7 @@ inline dasynq::rearm control_conn_cb(eventloop_t * loop, control_conn_watcher *
}

int watch_flags = 0;
if (!conn->bad_conn_close) {
if (!conn->bad_conn_close || conn->outbuf_size < 16384) {
watch_flags |= dasynq::IN_EVENTS;
}
if (!conn->outbuf.empty() || conn->bad_conn_close) {
Expand Down

0 comments on commit e8f89c5

Please sign in to comment.