aboutsummaryrefslogtreecommitdiffhomepage
path: root/td/telegram/QueryCombiner.h
blob: b2cc45ee6ccd9d9f278d053bd71e0ef0a15a97ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2026
//
// 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"
#include "td/utils/FlatHashMap.h"
#include "td/utils/Promise.h"
#include "td/utils/Slice.h"
#include "td/utils/Status.h"

#include <queue>

namespace td {

// combines identical queries into one request
class QueryCombiner final : public Actor {
 public:
  QueryCombiner(Slice name, double min_delay);

  void add_query(int64 query_id, Promise<Promise<Unit>> &&send_query, Promise<Unit> &&promise);

 private:
  struct QueryInfo {
    vector<Promise<Unit>> promises;
    bool is_sent = false;
    Promise<Promise<Unit>> send_query;
  };

  int32 query_count_ = 0;

  double next_query_time_;
  double min_delay_;

  std::queue<int64> delayed_queries_;

  FlatHashMap<int64, QueryInfo> queries_;

  void do_send_query(int64 query_id, QueryInfo &query);

  void on_get_query_result(int64 query_id, Result<Unit> &&result);

  void loop() final;
};

}  // namespace td