mirror of
https://github.com/tdlight-team/tdlight-telegram-bot-api.git
synced 2025-01-23 18:27:34 +01:00
Use LambdaPromise instead of PromiseActor.
This commit is contained in:
parent
4f3105f4a4
commit
3de310c951
2
td
2
td
@ -1 +1 @@
|
|||||||
Subproject commit b393215d6671863b6baf2a589d343cff9474f6ba
|
Subproject commit 9a061c30c1f1928f12bdb3feb2108a8052b102be
|
@ -145,7 +145,7 @@ void ClientManager::send(PromisedQueryPtr query) {
|
|||||||
std::move(query)); // will send 429 if the client is already closed
|
std::move(query)); // will send 429 if the client is already closed
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClientManager::get_stats(td::PromiseActor<td::BufferSlice> promise,
|
void ClientManager::get_stats(td::Promise<td::BufferSlice> promise,
|
||||||
td::vector<std::pair<td::string, td::string>> args) {
|
td::vector<std::pair<td::string, td::string>> args) {
|
||||||
if (close_flag_) {
|
if (close_flag_) {
|
||||||
promise.set_value(td::BufferSlice("Closing"));
|
promise.set_value(td::BufferSlice("Closing"));
|
||||||
@ -394,7 +394,7 @@ PromisedQueryPtr ClientManager::get_webhook_restore_query(td::Slice token, td::S
|
|||||||
auto query = td::make_unique<Query>(std::move(containers), token, is_test_dc, method, std::move(args),
|
auto query = td::make_unique<Query>(std::move(containers), token, is_test_dc, method, std::move(args),
|
||||||
td::vector<std::pair<td::MutableSlice, td::MutableSlice>>(),
|
td::vector<std::pair<td::MutableSlice, td::MutableSlice>>(),
|
||||||
td::vector<td::HttpFile>(), std::move(shared_data), td::IPAddress(), true);
|
td::vector<td::HttpFile>(), std::move(shared_data), td::IPAddress(), true);
|
||||||
return PromisedQueryPtr(query.release(), PromiseDeleter(td::PromiseActor<td::unique_ptr<Query>>()));
|
return PromisedQueryPtr(query.release(), PromiseDeleter(td::Promise<td::unique_ptr<Query>>()));
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClientManager::raw_event(const td::Event::Raw &event) {
|
void ClientManager::raw_event(const td::Event::Raw &event) {
|
||||||
|
@ -43,7 +43,7 @@ class ClientManager final : public td::Actor {
|
|||||||
|
|
||||||
void send(PromisedQueryPtr query);
|
void send(PromisedQueryPtr query);
|
||||||
|
|
||||||
void get_stats(td::PromiseActor<td::BufferSlice> promise, td::vector<std::pair<td::string, td::string>> args);
|
void get_stats(td::Promise<td::BufferSlice> promise, td::vector<std::pair<td::string, td::string>> args);
|
||||||
|
|
||||||
void close(td::Promise<td::Unit> &&promise);
|
void close(td::Promise<td::Unit> &&promise);
|
||||||
|
|
||||||
|
@ -48,22 +48,17 @@ void HttpConnection::handle(td::unique_ptr<td::HttpQuery> http_query,
|
|||||||
std::move(http_query->args_), std::move(http_query->headers_),
|
std::move(http_query->args_), std::move(http_query->headers_),
|
||||||
std::move(http_query->files_), shared_data_, http_query->peer_address_, false);
|
std::move(http_query->files_), shared_data_, http_query->peer_address_, false);
|
||||||
|
|
||||||
td::PromiseActor<td::unique_ptr<Query>> promise;
|
auto promise = td::PromiseCreator::lambda([actor_id = actor_id(this)](td::Result<td::unique_ptr<Query>> r_query) {
|
||||||
td::FutureActor<td::unique_ptr<Query>> future;
|
send_closure(actor_id, &HttpConnection::on_query_finished, std::move(r_query));
|
||||||
td::init_promise_future(&promise, &future);
|
});
|
||||||
future.set_event(td::EventCreator::yield(actor_id()));
|
|
||||||
auto promised_query = PromisedQueryPtr(query.release(), PromiseDeleter(std::move(promise)));
|
auto promised_query = PromisedQueryPtr(query.release(), PromiseDeleter(std::move(promise)));
|
||||||
send_closure(client_manager_, &ClientManager::send, std::move(promised_query));
|
send_closure(client_manager_, &ClientManager::send, std::move(promised_query));
|
||||||
result_ = std::move(future);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpConnection::wakeup() {
|
void HttpConnection::on_query_finished(td::Result<td::unique_ptr<Query>> r_query) {
|
||||||
if (result_.empty()) {
|
LOG_CHECK(r_query.is_ok()) << r_query.error();
|
||||||
return;
|
|
||||||
}
|
|
||||||
LOG_CHECK(result_.is_ok()) << result_.move_as_error();
|
|
||||||
|
|
||||||
auto query = result_.move_as_ok();
|
auto query = r_query.move_as_ok();
|
||||||
send_response(query->http_status_code(), std::move(query->answer()), query->retry_after());
|
send_response(query->http_status_code(), std::move(query->answer()), query->retry_after());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,10 +32,7 @@ class HttpConnection final : public td::HttpInboundConnection::Callback {
|
|||||||
|
|
||||||
void handle(td::unique_ptr<td::HttpQuery> http_query, td::ActorOwn<td::HttpInboundConnection> connection) final;
|
void handle(td::unique_ptr<td::HttpQuery> http_query, td::ActorOwn<td::HttpInboundConnection> connection) final;
|
||||||
|
|
||||||
void wakeup() final;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
td::FutureActor<td::unique_ptr<Query>> result_;
|
|
||||||
td::ActorId<ClientManager> client_manager_;
|
td::ActorId<ClientManager> client_manager_;
|
||||||
td::ActorOwn<td::HttpInboundConnection> connection_;
|
td::ActorOwn<td::HttpInboundConnection> connection_;
|
||||||
std::shared_ptr<SharedData> shared_data_;
|
std::shared_ptr<SharedData> shared_data_;
|
||||||
@ -45,6 +42,8 @@ class HttpConnection final : public td::HttpInboundConnection::Callback {
|
|||||||
stop();
|
stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void on_query_finished(td::Result<td::unique_ptr<Query>> r_query);
|
||||||
|
|
||||||
void send_response(int http_status_code, td::BufferSlice &&content, int retry_after);
|
void send_response(int http_status_code, td::BufferSlice &&content, int retry_after);
|
||||||
|
|
||||||
void send_http_error(int http_status_code, td::Slice description);
|
void send_http_error(int http_status_code, td::Slice description);
|
||||||
|
@ -18,22 +18,20 @@ void HttpStatConnection::handle(td::unique_ptr<td::HttpQuery> http_query,
|
|||||||
CHECK(connection_->empty());
|
CHECK(connection_->empty());
|
||||||
connection_ = std::move(connection);
|
connection_ = std::move(connection);
|
||||||
|
|
||||||
td::PromiseActor<td::BufferSlice> promise;
|
auto promise = td::PromiseCreator::lambda([actor_id = actor_id(this)](td::Result<td::BufferSlice> result) {
|
||||||
td::FutureActor<td::BufferSlice> future;
|
send_closure(actor_id, &HttpStatConnection::on_result, std::move(result));
|
||||||
init_promise_future(&promise, &future);
|
});
|
||||||
future.set_event(td::EventCreator::yield(actor_id()));
|
|
||||||
LOG(DEBUG) << "SEND";
|
|
||||||
send_closure(client_manager_, &ClientManager::get_stats, std::move(promise), http_query->get_args());
|
send_closure(client_manager_, &ClientManager::get_stats, std::move(promise), http_query->get_args());
|
||||||
result_ = std::move(future);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpStatConnection::wakeup() {
|
void HttpStatConnection::on_result(td::Result<td::BufferSlice> result) {
|
||||||
if (result_.empty()) {
|
if (result.is_error()) {
|
||||||
|
send_closure(connection_.release(), &td::HttpInboundConnection::write_error,
|
||||||
|
td::Status::Error(500, "Internal Server Error: closing"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
LOG_CHECK(result_.is_ok()) << result_.move_as_error();
|
|
||||||
|
|
||||||
auto content = result_.move_as_ok();
|
auto content = result.move_as_ok();
|
||||||
td::HttpHeaderCreator hc;
|
td::HttpHeaderCreator hc;
|
||||||
hc.init_status_line(200);
|
hc.init_status_line(200);
|
||||||
hc.set_keep_alive();
|
hc.set_keep_alive();
|
||||||
|
@ -12,9 +12,9 @@
|
|||||||
#include "td/net/HttpQuery.h"
|
#include "td/net/HttpQuery.h"
|
||||||
|
|
||||||
#include "td/actor/actor.h"
|
#include "td/actor/actor.h"
|
||||||
#include "td/actor/PromiseFuture.h"
|
|
||||||
|
|
||||||
#include "td/utils/buffer.h"
|
#include "td/utils/buffer.h"
|
||||||
|
#include "td/utils/Status.h"
|
||||||
|
|
||||||
namespace telegram_bot_api {
|
namespace telegram_bot_api {
|
||||||
|
|
||||||
@ -22,15 +22,15 @@ class HttpStatConnection final : public td::HttpInboundConnection::Callback {
|
|||||||
public:
|
public:
|
||||||
explicit HttpStatConnection(td::ActorId<ClientManager> client_manager) : client_manager_(client_manager) {
|
explicit HttpStatConnection(td::ActorId<ClientManager> client_manager) : client_manager_(client_manager) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void handle(td::unique_ptr<td::HttpQuery> http_query, td::ActorOwn<td::HttpInboundConnection> connection) final;
|
void handle(td::unique_ptr<td::HttpQuery> http_query, td::ActorOwn<td::HttpInboundConnection> connection) final;
|
||||||
|
|
||||||
void wakeup() final;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
td::FutureActor<td::BufferSlice> result_;
|
|
||||||
td::ActorId<ClientManager> client_manager_;
|
td::ActorId<ClientManager> client_manager_;
|
||||||
td::ActorOwn<td::HttpInboundConnection> connection_;
|
td::ActorOwn<td::HttpInboundConnection> connection_;
|
||||||
|
|
||||||
|
void on_result(td::Result<td::BufferSlice> result);
|
||||||
|
|
||||||
void hangup() final {
|
void hangup() final {
|
||||||
connection_.release();
|
connection_.release();
|
||||||
stop();
|
stop();
|
||||||
|
@ -227,7 +227,7 @@ class JsonQueryError final : public td::Jsonable {
|
|||||||
|
|
||||||
class PromiseDeleter {
|
class PromiseDeleter {
|
||||||
public:
|
public:
|
||||||
explicit PromiseDeleter(td::PromiseActor<td::unique_ptr<Query>> &&promise) : promise_(std::move(promise)) {
|
explicit PromiseDeleter(td::Promise<td::unique_ptr<Query>> &&promise) : promise_(std::move(promise)) {
|
||||||
}
|
}
|
||||||
PromiseDeleter() = default;
|
PromiseDeleter() = default;
|
||||||
PromiseDeleter(const PromiseDeleter &) = delete;
|
PromiseDeleter(const PromiseDeleter &) = delete;
|
||||||
@ -236,7 +236,7 @@ class PromiseDeleter {
|
|||||||
PromiseDeleter &operator=(PromiseDeleter &&) = default;
|
PromiseDeleter &operator=(PromiseDeleter &&) = default;
|
||||||
void operator()(Query *raw_ptr) {
|
void operator()(Query *raw_ptr) {
|
||||||
td::unique_ptr<Query> query(raw_ptr); // now I cannot forget to delete this pointer
|
td::unique_ptr<Query> query(raw_ptr); // now I cannot forget to delete this pointer
|
||||||
if (!promise_.empty_promise()) {
|
if (promise_) {
|
||||||
if (!query->is_ready()) {
|
if (!query->is_ready()) {
|
||||||
query->set_retry_after_error(5);
|
query->set_retry_after_error(5);
|
||||||
}
|
}
|
||||||
@ -245,11 +245,11 @@ class PromiseDeleter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
~PromiseDeleter() {
|
~PromiseDeleter() {
|
||||||
CHECK(promise_.empty());
|
CHECK(!promise_);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
td::PromiseActor<td::unique_ptr<Query>> promise_;
|
td::Promise<td::unique_ptr<Query>> promise_;
|
||||||
};
|
};
|
||||||
using PromisedQueryPtr = std::unique_ptr<Query, PromiseDeleter>;
|
using PromisedQueryPtr = std::unique_ptr<Query, PromiseDeleter>;
|
||||||
|
|
||||||
|
@ -82,7 +82,7 @@ void WebhookActor::relax_wakeup_at(double wakeup_at, const char *source) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void WebhookActor::resolve_ip_address() {
|
void WebhookActor::resolve_ip_address() {
|
||||||
if (fix_ip_address_) {
|
if (fix_ip_address_ || is_ip_address_being_resolved_) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (td::Time::now() < next_ip_address_resolve_time_) {
|
if (td::Time::now() < next_ip_address_resolve_time_) {
|
||||||
@ -90,23 +90,27 @@ void WebhookActor::resolve_ip_address() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool future_created = false;
|
is_ip_address_being_resolved_ = true;
|
||||||
if (future_ip_address_.empty()) {
|
auto promise = td::PromiseCreator::lambda([actor_id = actor_id(this)](td::Result<td::IPAddress> r_ip_address) {
|
||||||
td::PromiseActor<td::IPAddress> promise;
|
send_closure(actor_id, &WebhookActor::on_resolved_ip_address, std::move(r_ip_address));
|
||||||
init_promise_future(&promise, &future_ip_address_);
|
});
|
||||||
future_created = true;
|
|
||||||
send_closure(parameters_->get_host_by_name_actor_id_, &td::GetHostByNameActor::run, url_.host_, url_.port_, false,
|
send_closure(parameters_->get_host_by_name_actor_id_, &td::GetHostByNameActor::run, url_.host_, url_.port_, false,
|
||||||
td::PromiseCreator::from_promise_actor(std::move(promise)));
|
std::move(promise));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (future_ip_address_.is_ready()) {
|
void WebhookActor::on_resolved_ip_address(td::Result<td::IPAddress> r_ip_address) {
|
||||||
|
CHECK(is_ip_address_being_resolved_);
|
||||||
|
is_ip_address_being_resolved_ = false;
|
||||||
|
|
||||||
next_ip_address_resolve_time_ =
|
next_ip_address_resolve_time_ =
|
||||||
td::Time::now() + IP_ADDRESS_CACHE_TIME + td::Random::fast(0, IP_ADDRESS_CACHE_TIME / 10);
|
td::Time::now() + IP_ADDRESS_CACHE_TIME + td::Random::fast(0, IP_ADDRESS_CACHE_TIME / 10);
|
||||||
relax_wakeup_at(next_ip_address_resolve_time_, "resolve_ip_address");
|
relax_wakeup_at(next_ip_address_resolve_time_, "on_resolved_ip_address");
|
||||||
|
|
||||||
|
SCOPE_EXIT {
|
||||||
|
loop();
|
||||||
|
};
|
||||||
|
|
||||||
auto r_ip_address = future_ip_address_.move_as_result();
|
|
||||||
if (r_ip_address.is_error()) {
|
if (r_ip_address.is_error()) {
|
||||||
CHECK(!(r_ip_address.error() == td::Status::Error<td::FutureActor<td::IPAddress>::HANGUP_ERROR_CODE>()));
|
|
||||||
return on_error(r_ip_address.move_as_error());
|
return on_error(r_ip_address.move_as_error());
|
||||||
}
|
}
|
||||||
auto new_ip_address = r_ip_address.move_as_ok();
|
auto new_ip_address = r_ip_address.move_as_ok();
|
||||||
@ -122,11 +126,6 @@ void WebhookActor::resolve_ip_address() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
VLOG(webhook) << "IP address was verified";
|
VLOG(webhook) << "IP address was verified";
|
||||||
} else {
|
|
||||||
if (future_created) {
|
|
||||||
future_ip_address_.set_event(td::EventCreator::yield(actor_id()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
td::Status WebhookActor::create_connection() {
|
td::Status WebhookActor::create_connection() {
|
||||||
@ -605,8 +604,7 @@ void WebhookActor::handle(td::unique_ptr<td::HttpQuery> response) {
|
|||||||
td::MutableSlice(), std::move(response->args_),
|
td::MutableSlice(), std::move(response->args_),
|
||||||
std::move(response->headers_), std::move(response->files_),
|
std::move(response->headers_), std::move(response->files_),
|
||||||
parameters_->shared_data_, response->peer_address_, false);
|
parameters_->shared_data_, response->peer_address_, false);
|
||||||
auto promised_query =
|
auto promised_query = PromisedQueryPtr(query.release(), PromiseDeleter(td::Promise<td::unique_ptr<Query>>()));
|
||||||
PromisedQueryPtr(query.release(), PromiseDeleter(td::PromiseActor<td::unique_ptr<Query>>()));
|
|
||||||
send_closure(callback_, &Callback::send, std::move(promised_query));
|
send_closure(callback_, &Callback::send, std::move(promised_query));
|
||||||
}
|
}
|
||||||
first_error_410_time_ = 0;
|
first_error_410_time_ = 0;
|
||||||
|
@ -137,7 +137,7 @@ class WebhookActor final : public td::HttpOutboundConnection::Callback {
|
|||||||
td::IPAddress ip_address_;
|
td::IPAddress ip_address_;
|
||||||
td::int32 ip_generation_ = 0;
|
td::int32 ip_generation_ = 0;
|
||||||
double next_ip_address_resolve_time_ = 0;
|
double next_ip_address_resolve_time_ = 0;
|
||||||
td::FutureActor<td::IPAddress> future_ip_address_;
|
bool is_ip_address_being_resolved_ = false;
|
||||||
|
|
||||||
class Connection final : public td::ListNode {
|
class Connection final : public td::ListNode {
|
||||||
public:
|
public:
|
||||||
@ -175,6 +175,7 @@ class WebhookActor final : public td::HttpOutboundConnection::Callback {
|
|||||||
void relax_wakeup_at(double wakeup_at, const char *source);
|
void relax_wakeup_at(double wakeup_at, const char *source);
|
||||||
|
|
||||||
void resolve_ip_address();
|
void resolve_ip_address();
|
||||||
|
void on_resolved_ip_address(td::Result<td::IPAddress> r_ip_address);
|
||||||
|
|
||||||
td::Result<td::SslStream> create_ssl_stream();
|
td::Result<td::SslStream> create_ssl_stream();
|
||||||
td::Status create_connection() TD_WARN_UNUSED_RESULT;
|
td::Status create_connection() TD_WARN_UNUSED_RESULT;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user