// // Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021 // // 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" #include "telegram-bot-api/Watchdog.h" #include "td/actor/actor.h" #include "td/utils/buffer.h" #include "td/utils/common.h" #include "td/utils/Container.h" #include "td/utils/FlatHashMap.h" #include "td/utils/FloodControlFast.h" #include "td/utils/Promise.h" #include "td/utils/Slice.h" #include #include 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 parameters, TokenRange token_range) : parameters_(std::move(parameters)), token_range_(token_range) { } void dump_statistics(); void send(PromisedQueryPtr query); void get_stats(td::Promise promise, td::vector> args); void close(td::Promise &&promise); private: class ClientInfo { public: BotStatActor stat_; td::string token_; td::int64 tqueue_id_; td::ActorOwn client_; }; td::Container clients_; BotStatActor stat_{td::ActorId()}; std::shared_ptr parameters_; TokenRange token_range_; td::FlatHashMap token_to_id_; td::FlatHashMap flood_controls_; td::FlatHashMap active_client_count_; bool close_flag_ = false; td::vector> close_promises_; td::ActorOwn watchdog_id_; double last_tqueue_gc_time_ = 0.0; td::int64 tqueue_deleted_events_ = 0; td::int64 last_tqueue_deleted_events_ = 0; static constexpr double WATCHDOG_TIMEOUT = 0.5; 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 shared_data); void start_up() final; void raw_event(const td::Event::Raw &event) final; void timeout_expired() final; void hangup_shared() final; void close_db(); void finish_close(); }; } // namespace telegram_bot_api