Don't allow TQueue::run_gc to run for more than 50 milliseconds.

This commit is contained in:
levlam 2022-10-13 16:04:24 +03:00
parent ec4ae065b0
commit ec86b34d55
2 changed files with 16 additions and 3 deletions

View File

@ -15,6 +15,7 @@
#include "td/utils/misc.h"
#include "td/utils/Random.h"
#include "td/utils/StorerBase.h"
#include "td/utils/Time.h"
#include "td/utils/tl_helpers.h"
#include "td/utils/tl_parsers.h"
#include "td/utils/tl_storers.h"
@ -234,8 +235,10 @@ class TQueueImpl final : public TQueue {
return get_size(q);
}
int64 run_gc(int32 unix_time_now) final {
std::pair<int64, bool> run_gc(int32 unix_time_now) final {
int64 deleted_events = 0;
auto max_finish_time = Time::now() + 0.05;
int64 counter = 0;
while (!queue_gc_at_.empty()) {
auto it = queue_gc_at_.begin();
if (it->first >= unix_time_now) {
@ -250,6 +253,12 @@ class TQueueImpl final : public TQueue {
size_t size_before = get_size(q);
for (auto event_it = q.events.begin(); event_it != q.events.end();) {
auto &event = event_it->second;
if ((++counter & 128) == 0 && Time::now() >= max_finish_time) {
if (new_gc_at == 0) {
new_gc_at = event.expires_at;
}
break;
}
if (event.expires_at < unix_time_now || event.data.empty()) {
pop(q, queue_id, event_it, q.tail_id);
} else {
@ -265,8 +274,11 @@ class TQueueImpl final : public TQueue {
deleted_events += size_before - size_after;
}
schedule_queue_gc(queue_id, q, new_gc_at);
if (Time::now() >= max_finish_time) {
return {deleted_events, false};
}
return deleted_events;
}
return {deleted_events, true};
}
size_t get_size(QueueId queue_id) const final {

View File

@ -112,7 +112,8 @@ class TQueue {
virtual size_t get_size(QueueId queue_id) const = 0;
virtual int64 run_gc(int32 unix_time_now) = 0;
// returns number of deleted events and whether garbage collection was completed
virtual std::pair<int64, bool> run_gc(int32 unix_time_now) = 0;
virtual void close(Promise<> promise) = 0;
};