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 <mobin@mobintestserver.ir>
  • Loading branch information
mobin-2008 committed Jul 24, 2023
1 parent 7d491be commit f818f4a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/control.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1135,7 +1135,8 @@ void control_conn_t::service_event(service_record *service, service_event_t even

bool control_conn_t::queue_packet(const char *pkt, unsigned size) noexcept
{
int in_flag = bad_conn_close ? 0 : IN_EVENTS;
// Close connection on bad con or temporarily Limit input if necessary
int in_flag = bad_conn_close || (outbuf_size >= 1000) ? 0 : IN_EVENTS;
bool was_empty = outbuf.empty();

// If the queue is empty, we can try to write the packet out now rather than queueing it.
Expand Down Expand Up @@ -1166,6 +1167,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;
iob.set_watches(in_flag | OUT_EVENTS);
return true;
}
Expand All @@ -1190,7 +1192,8 @@ bool control_conn_t::queue_packet(const char *pkt, unsigned size) noexcept
// make them extraordinary difficult to combine into a single method.
bool control_conn_t::queue_packet(std::vector<char> &&pkt) noexcept
{
int in_flag = bad_conn_close ? 0 : IN_EVENTS;
// Close connection on bad con or temporarily Limit input if necessary
int in_flag = bad_conn_close || (outbuf_size >= 1000) ? 0 : IN_EVENTS;
bool was_empty = outbuf.empty();

if (was_empty) {
Expand Down Expand Up @@ -1219,6 +1222,7 @@ bool control_conn_t::queue_packet(std::vector<char> &&pkt) noexcept

try {
outbuf.emplace_back(std::move(pkt));
outbuf_size += pkt.size();
iob.set_watches(in_flag | OUT_EVENTS);
return true;
}
Expand Down Expand Up @@ -1275,8 +1279,10 @@ bool control_conn_t::data_ready() noexcept
iob.set_watches(OUT_EVENTS);
}
else {
// Temporarily Limit input if necessary
int in_flags = (outbuf_size < 1000) ? IN_EVENTS : 0;
int out_flags = (bad_conn_close || !outbuf.empty()) ? OUT_EVENTS : 0;
iob.set_watches(IN_EVENTS | out_flags);
iob.set_watches(in_flags | out_flags);
}

return false;
Expand Down Expand Up @@ -1314,6 +1320,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 All @@ -1324,7 +1331,9 @@ bool control_conn_t::send_data() noexcept
if (bad_conn_close) {
return true;
}
iob.set_watches(IN_EVENTS);
// Temporarily Limit input if necessary
int in_flag = (outbuf_size < 1000) ? IN_EVENTS : 0;
iob.set_watches(in_flag);
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/includes/control.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ class control_conn_t : private service_listener

// Buffer for outgoing packets. Each outgoing back is represented as a vector<char>.
list<vector<char>> outbuf;
// Output buffer size. functions will increase this value when pushing something into
// outbuf and lowering this value when something get removed from outbuf.
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,6 +184,9 @@ class control_conn_t : private service_listener
bool data_ready() noexcept;

bool send_data() noexcept;

// Check the output buffer size and limit inputs if necessary
bool input_memlimit() 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.
Expand Down

0 comments on commit f818f4a

Please sign in to comment.