2019-02-25 04:08:18 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2019-02-25 04:08:18 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/actor/actor.h"
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
2022-02-07 22:04:34 +01:00
|
|
|
#include "td/utils/FlatHashMap.h"
|
2022-06-27 12:30:18 +02:00
|
|
|
#include "td/utils/Promise.h"
|
2019-03-17 21:43:58 +01:00
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
#include "td/utils/Status.h"
|
2019-02-25 04:08:18 +01:00
|
|
|
|
2019-02-25 18:44:28 +01:00
|
|
|
#include <queue>
|
2019-02-25 04:08:18 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
// combines identical queries into one request
|
2021-07-04 04:58:54 +02:00
|
|
|
class QueryCombiner final : public Actor {
|
2019-02-25 04:08:18 +01:00
|
|
|
public:
|
2020-11-21 14:34:39 +01:00
|
|
|
QueryCombiner(Slice name, double min_delay);
|
2019-02-25 04:08:18 +01:00
|
|
|
|
|
|
|
void add_query(int64 query_id, Promise<Promise<Unit>> &&send_query, Promise<Unit> &&promise);
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct QueryInfo {
|
|
|
|
vector<Promise<Unit>> promises;
|
2019-02-25 17:27:06 +01:00
|
|
|
bool is_sent = false;
|
2019-02-25 18:44:28 +01:00
|
|
|
Promise<Promise<Unit>> send_query;
|
2019-02-25 04:08:18 +01:00
|
|
|
};
|
|
|
|
|
2019-02-25 18:44:28 +01:00
|
|
|
int32 query_count_ = 0;
|
|
|
|
|
2020-11-21 14:34:39 +01:00
|
|
|
double next_query_time_;
|
2019-02-25 18:44:28 +01:00
|
|
|
double min_delay_;
|
|
|
|
|
|
|
|
std::queue<int64> delayed_queries_;
|
|
|
|
|
2022-02-07 20:41:07 +01:00
|
|
|
FlatHashMap<int64, QueryInfo> queries_;
|
2019-02-25 04:08:18 +01:00
|
|
|
|
2019-02-25 18:44:28 +01:00
|
|
|
void do_send_query(int64 query_id, QueryInfo &query);
|
|
|
|
|
2019-02-25 04:08:18 +01:00
|
|
|
void on_get_query_result(int64 query_id, Result<Unit> &&result);
|
2019-02-25 18:44:28 +01:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void loop() final;
|
2019-02-25 04:08:18 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace td
|