2020-11-03 17:34:10 +01:00
|
|
|
//
|
2022-12-31 22:31:16 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
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)
|
|
|
|
//
|
|
|
|
#include "telegram-bot-api/Query.h"
|
|
|
|
|
|
|
|
#include "telegram-bot-api/Stats.h"
|
|
|
|
|
|
|
|
#include "td/actor/actor.h"
|
|
|
|
|
|
|
|
#include "td/utils/format.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/misc.h"
|
|
|
|
#include "td/utils/port/IPAddress.h"
|
2021-05-20 22:51:37 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2020-11-03 17:34:10 +01:00
|
|
|
#include "td/utils/Time.h"
|
|
|
|
|
|
|
|
#include <numeric>
|
|
|
|
|
|
|
|
namespace telegram_bot_api {
|
|
|
|
|
2022-03-16 10:52:34 +01:00
|
|
|
td::FlatHashMap<td::string, td::unique_ptr<td::VirtuallyJsonable>> empty_parameters;
|
2020-11-03 17:34:10 +01:00
|
|
|
|
2020-12-12 00:45:36 +01:00
|
|
|
Query::Query(td::vector<td::BufferSlice> &&container, td::Slice token, bool is_user, bool is_test_dc, td::MutableSlice method,
|
2020-11-03 17:34:10 +01:00
|
|
|
td::vector<std::pair<td::MutableSlice, td::MutableSlice>> &&args,
|
|
|
|
td::vector<std::pair<td::MutableSlice, td::MutableSlice>> &&headers, td::vector<td::HttpFile> &&files,
|
2023-03-13 16:42:35 +01:00
|
|
|
std::shared_ptr<SharedData> shared_data, const td::IPAddress &peer_ip_address, bool is_internal)
|
2020-11-03 17:34:10 +01:00
|
|
|
: state_(State::Query)
|
|
|
|
, shared_data_(shared_data)
|
2023-03-13 16:42:35 +01:00
|
|
|
, peer_ip_address_(peer_ip_address)
|
2020-11-03 17:34:10 +01:00
|
|
|
, container_(std::move(container))
|
|
|
|
, token_(token)
|
2020-12-12 00:45:36 +01:00
|
|
|
, is_user_(is_user)
|
2020-11-03 17:34:10 +01:00
|
|
|
, is_test_dc_(is_test_dc)
|
|
|
|
, method_(method)
|
|
|
|
, args_(std::move(args))
|
|
|
|
, headers_(std::move(headers))
|
2021-06-14 21:47:01 +02:00
|
|
|
, files_(std::move(files))
|
|
|
|
, is_internal_(is_internal) {
|
2020-11-03 17:34:10 +01:00
|
|
|
if (method_.empty()) {
|
|
|
|
method_ = arg("method");
|
|
|
|
}
|
|
|
|
td::to_lower_inplace(method_);
|
|
|
|
start_timestamp_ = td::Time::now();
|
|
|
|
LOG(INFO) << "QUERY: create " << td::tag("ptr", this) << *this;
|
|
|
|
if (shared_data_) {
|
2022-10-04 17:27:50 +02:00
|
|
|
shared_data_->query_count_.fetch_add(1, std::memory_order_relaxed);
|
2020-11-03 17:34:10 +01:00
|
|
|
if (method_ != "getupdates") {
|
2022-10-04 17:27:50 +02:00
|
|
|
shared_data_->query_list_size_.fetch_add(1, std::memory_order_relaxed);
|
2020-11-03 17:34:10 +01:00
|
|
|
shared_data_->query_list_.put(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-13 16:42:35 +01:00
|
|
|
td::string Query::get_peer_ip_address() const {
|
|
|
|
if (peer_ip_address_.is_valid() && !peer_ip_address_.is_reserved()) { // external connection
|
|
|
|
return peer_ip_address_.get_ip_str().str();
|
|
|
|
} else {
|
|
|
|
// invalid peer IP address or connection from the local network
|
|
|
|
return get_header("x-real-ip").str();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 17:34:10 +01:00
|
|
|
td::int64 Query::query_size() const {
|
|
|
|
return std::accumulate(
|
|
|
|
container_.begin(), container_.end(), td::int64{0},
|
|
|
|
[](td::int64 acc, const td::BufferSlice &slice) { return static_cast<td::int64>(acc + slice.size()); });
|
|
|
|
}
|
|
|
|
|
|
|
|
td::int64 Query::files_size() const {
|
|
|
|
return std::accumulate(files_.begin(), files_.end(), td::int64{0},
|
|
|
|
[](td::int64 acc, const td::HttpFile &file) { return acc + file.size; });
|
|
|
|
}
|
|
|
|
|
|
|
|
td::int64 Query::files_max_size() const {
|
|
|
|
return std::accumulate(files_.begin(), files_.end(), td::int64{0},
|
|
|
|
[](td::int64 acc, const td::HttpFile &file) { return td::max(acc, file.size); });
|
|
|
|
}
|
|
|
|
|
2021-06-14 21:47:01 +02:00
|
|
|
void Query::set_stat_actor(td::ActorId<BotStatActor> stat_actor) {
|
|
|
|
stat_actor_ = stat_actor;
|
|
|
|
send_request_stat();
|
|
|
|
}
|
|
|
|
|
2020-11-03 17:34:10 +01:00
|
|
|
void Query::set_ok(td::BufferSlice result) {
|
|
|
|
CHECK(state_ == State::Query);
|
|
|
|
LOG(INFO) << "QUERY: got ok " << td::tag("ptr", this) << td::tag("text", result.as_slice());
|
|
|
|
answer_ = std::move(result);
|
|
|
|
state_ = State::OK;
|
|
|
|
http_status_code_ = 200;
|
|
|
|
send_response_stat();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Query::set_error(int http_status_code, td::BufferSlice result) {
|
|
|
|
LOG(INFO) << "QUERY: got error " << td::tag("ptr", this) << td::tag("code", http_status_code)
|
|
|
|
<< td::tag("text", result.as_slice());
|
|
|
|
CHECK(state_ == State::Query);
|
|
|
|
answer_ = std::move(result);
|
|
|
|
state_ = State::Error;
|
|
|
|
http_status_code_ = http_status_code;
|
|
|
|
send_response_stat();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Query::set_retry_after_error(int retry_after) {
|
|
|
|
retry_after_ = retry_after;
|
|
|
|
|
2022-03-16 10:52:34 +01:00
|
|
|
td::FlatHashMap<td::string, td::unique_ptr<td::VirtuallyJsonable>> parameters;
|
|
|
|
parameters.emplace("retry_after", td::make_unique<td::VirtuallyJsonableLong>(retry_after));
|
2020-11-03 17:34:10 +01:00
|
|
|
set_error(429, td::json_encode<td::BufferSlice>(
|
|
|
|
JsonQueryError(429, PSLICE() << "Too Many Requests: retry after " << retry_after, parameters)));
|
|
|
|
}
|
|
|
|
|
|
|
|
td::StringBuilder &operator<<(td::StringBuilder &sb, const Query &query) {
|
|
|
|
auto padded_time =
|
|
|
|
td::lpad(PSTRING() << td::format::as_time(td::Time::now_cached() - query.start_timestamp()), 10, ' ');
|
|
|
|
sb << "[bot" << td::rpad(query.token().str(), 46, ' ') << "][time:" << padded_time << ']'
|
|
|
|
<< td::tag("method", td::lpad(query.method().str(), 20, ' '));
|
|
|
|
if (!query.args().empty()) {
|
|
|
|
sb << td::oneline(PSLICE() << query.args());
|
|
|
|
}
|
|
|
|
if (!query.files().empty()) {
|
|
|
|
sb << query.files();
|
|
|
|
}
|
|
|
|
return sb;
|
|
|
|
}
|
|
|
|
|
2021-06-14 21:47:01 +02:00
|
|
|
void Query::send_request_stat() const {
|
|
|
|
if (stat_actor_.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
send_closure(stat_actor_, &BotStatActor::add_event<ServerBotStat::Request>,
|
|
|
|
ServerBotStat::Request{query_size(), file_count(), files_size(), files_max_size()}, td::Time::now());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Query::send_response_stat() const {
|
2021-06-14 21:58:23 +02:00
|
|
|
auto now = td::Time::now();
|
2021-07-27 00:54:11 +02:00
|
|
|
if (now - start_timestamp_ >= 100.0 && !is_internal_) {
|
2021-06-14 21:58:23 +02:00
|
|
|
LOG(WARNING) << "Answer too old query with code " << http_status_code_ << " and answer size " << answer_.size()
|
|
|
|
<< ": " << *this;
|
|
|
|
}
|
|
|
|
|
2020-11-03 17:34:10 +01:00
|
|
|
if (stat_actor_.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
send_closure(stat_actor_, &BotStatActor::add_event<ServerBotStat::Response>,
|
2022-11-14 13:35:43 +01:00
|
|
|
ServerBotStat::Response{state_ == State::OK, answer_.size(), file_count(), files_size()}, now);
|
2020-11-03 17:34:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace telegram_bot_api
|