2020-11-03 17:34:10 +01:00
|
|
|
//
|
2021-01-06 15:24:16 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
2020-11-03 17:34:10 +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 "telegram-bot-api/Client.h"
|
|
|
|
#include "telegram-bot-api/Query.h"
|
|
|
|
#include "telegram-bot-api/Stats.h"
|
2022-10-04 23:06:48 +02:00
|
|
|
#include "telegram-bot-api/Watchdog.h"
|
2020-11-03 17:34:10 +01:00
|
|
|
|
|
|
|
#include "td/actor/actor.h"
|
|
|
|
|
|
|
|
#include "td/utils/buffer.h"
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/Container.h"
|
2022-03-16 10:41:12 +01:00
|
|
|
#include "td/utils/FlatHashMap.h"
|
2020-11-03 17:34:10 +01:00
|
|
|
#include "td/utils/FloodControlFast.h"
|
2022-06-30 19:27:08 +02:00
|
|
|
#include "td/utils/Promise.h"
|
2020-11-03 17:34:10 +01:00
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace telegram_bot_api {
|
|
|
|
|
|
|
|
struct ClientParameters;
|
|
|
|
struct SharedData;
|
|
|
|
|
|
|
|
class ClientManager final : public td::Actor {
|
|
|
|
public:
|
|
|
|
struct TokenRange {
|
|
|
|
td::uint64 rem;
|
|
|
|
td::uint64 mod;
|
|
|
|
bool operator()(td::uint64 x) {
|
|
|
|
return x % mod == rem;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
ClientManager(std::shared_ptr<const ClientParameters> parameters, TokenRange token_range)
|
|
|
|
: parameters_(std::move(parameters)), token_range_(token_range) {
|
|
|
|
}
|
|
|
|
|
2022-10-06 21:18:36 +02:00
|
|
|
void dump_statistics();
|
|
|
|
|
2020-11-03 17:34:10 +01:00
|
|
|
void send(PromisedQueryPtr query);
|
|
|
|
|
2022-06-30 18:59:30 +02:00
|
|
|
void get_stats(td::Promise<td::BufferSlice> promise, td::vector<std::pair<td::string, td::string>> args);
|
2020-11-03 17:34:10 +01:00
|
|
|
|
|
|
|
void close(td::Promise<td::Unit> &&promise);
|
|
|
|
|
|
|
|
private:
|
|
|
|
class ClientInfo {
|
|
|
|
public:
|
|
|
|
BotStatActor stat_;
|
|
|
|
td::string token_;
|
2021-09-30 22:23:41 +02:00
|
|
|
td::int64 tqueue_id_;
|
2020-11-03 17:34:10 +01:00
|
|
|
td::ActorOwn<Client> client_;
|
|
|
|
};
|
|
|
|
td::Container<ClientInfo> clients_;
|
|
|
|
BotStatActor stat_{td::ActorId<BotStatActor>()};
|
|
|
|
|
|
|
|
std::shared_ptr<const ClientParameters> parameters_;
|
|
|
|
TokenRange token_range_;
|
|
|
|
|
2022-03-16 10:41:12 +01:00
|
|
|
td::FlatHashMap<td::string, td::uint64> token_to_id_;
|
|
|
|
td::FlatHashMap<td::string, td::FloodControlFast> flood_controls_;
|
|
|
|
td::FlatHashMap<td::int64, td::uint64> active_client_count_;
|
2020-11-03 17:34:10 +01:00
|
|
|
|
|
|
|
bool close_flag_ = false;
|
|
|
|
td::vector<td::Promise<td::Unit>> close_promises_;
|
|
|
|
|
2022-10-04 23:06:48 +02:00
|
|
|
td::ActorOwn<Watchdog> watchdog_id_;
|
2022-10-13 23:46:54 +02:00
|
|
|
double next_tqueue_gc_time_ = 0.0;
|
2022-10-06 20:42:33 +02:00
|
|
|
td::int64 tqueue_deleted_events_ = 0;
|
|
|
|
td::int64 last_tqueue_deleted_events_ = 0;
|
2022-10-04 23:06:48 +02:00
|
|
|
|
|
|
|
static constexpr double WATCHDOG_TIMEOUT = 0.5;
|
|
|
|
|
2020-11-03 17:34:10 +01:00
|
|
|
static td::int64 get_tqueue_id(td::int64 user_id, bool is_test_dc);
|
|
|
|
|
|
|
|
static PromisedQueryPtr get_webhook_restore_query(td::Slice token, td::Slice webhook_info,
|
|
|
|
std::shared_ptr<SharedData> shared_data);
|
|
|
|
|
2022-01-25 16:18:44 +01:00
|
|
|
void start_up() final;
|
|
|
|
void raw_event(const td::Event::Raw &event) final;
|
2022-10-04 23:06:48 +02:00
|
|
|
void timeout_expired() final;
|
2022-01-25 16:18:44 +01:00
|
|
|
void hangup_shared() final;
|
2020-11-03 17:34:10 +01:00
|
|
|
void close_db();
|
|
|
|
void finish_close();
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace telegram_bot_api
|