2019-02-25 04:08:18 +01:00
|
|
|
//
|
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019
|
|
|
|
//
|
|
|
|
// 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/actor/PromiseFuture.h"
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
|
|
|
|
#include <functional>
|
2019-02-25 18:44:28 +01:00
|
|
|
#include <queue>
|
2019-02-25 04:08:18 +01:00
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
// combines identical queries into one request
|
|
|
|
class QueryCombiner : public Actor {
|
|
|
|
public:
|
2019-02-25 18:44:28 +01:00
|
|
|
explicit QueryCombiner(Slice name, double min_delay = 0) : min_delay_(min_delay) {
|
2019-02-25 04:08:18 +01:00
|
|
|
register_actor(name, this).release();
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
double next_query_time_ = 0.0;
|
|
|
|
double min_delay_;
|
|
|
|
|
|
|
|
std::queue<int64> delayed_queries_;
|
|
|
|
|
2019-02-25 04:08:18 +01:00
|
|
|
std::unordered_map<int64, QueryInfo> queries_;
|
|
|
|
|
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
|
|
|
|
|
|
|
void loop() override;
|
2019-02-25 04:08:18 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace td
|