Skip to content

Commit

Permalink
add idle timeout parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhangYiQiu authored Jan 10, 2024
1 parent a43db02 commit 0f8afaa
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ If you've got any questions about bots or would like to report an issue with you

Please note that only global Bot API issues that affect all bots are suitable for this repository.

*This is an edit version of telegram-bot-api server to support use http proxy.*
*This is an edit version of telegram-bot-api server to support use http proxy and setup idle-timeout parameters.*


## Table of Contents
Expand Down
7 changes: 4 additions & 3 deletions telegram-bot-api/HttpServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,17 @@ namespace telegram_bot_api {

class HttpServer final : public td::TcpListener::Callback {
public:
HttpServer(td::string ip_address, int port,
HttpServer(td::string ip_address, int port, int idle_timeout,
std::function<td::ActorOwn<td::HttpInboundConnection::Callback>()> creator)
: ip_address_(std::move(ip_address)), port_(port), creator_(std::move(creator)) {
: ip_address_(std::move(ip_address)), port_(port), idle_timeout_(idle_timeout), creator_(std::move(creator)) {
flood_control_.add_limit(1, 1); // 1 in a second
flood_control_.add_limit(60, 10); // 10 in a minute
}

private:
td::string ip_address_;
td::int32 port_;
td::int32 idle_timeout_;
std::function<td::ActorOwn<td::HttpInboundConnection::Callback>()> creator_;
td::ActorOwn<td::TcpListener> listener_;
td::FloodControlFast flood_control_;
Expand All @@ -64,7 +65,7 @@ class HttpServer final : public td::TcpListener::Callback {

void accept(td::SocketFd fd) final {
td::create_actor<td::HttpInboundConnection>("HttpInboundConnection", td::BufferedFd<td::SocketFd>(std::move(fd)), 0,
50, 500, creator_(), SharedData::get_slow_incoming_http_scheduler_id())
50, idle_timeout_, creator_(), SharedData::get_slow_incoming_http_scheduler_id())
.release();
}

Expand Down
15 changes: 10 additions & 5 deletions telegram-bot-api/telegram-bot-api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ int main(int argc, char *argv[]) {
td::uint64 max_connections = 0;
td::uint64 cpu_affinity = 0;
td::uint64 main_thread_affinity = 0;
int idle_timeout = 500;
ClientManager::TokenRange token_range{0, 1};

parameters->api_id_ = [](auto x) -> td::int32 {
Expand Down Expand Up @@ -221,6 +222,8 @@ int main(int argc, char *argv[]) {
td::OptionParser::parse_string(parameters->api_hash_));
options.add_checked_option('p', "http-port", PSLICE() << "HTTP listening port (default is " << http_port << ")",
td::OptionParser::parse_integer(http_port));
options.add_checked_option('\0', "idle-timeout", PSLICE() << "idle timeout (default is " << idle_timeout << ")",
td::OptionParser::parse_integer(idle_timeout));
options.add_checked_option('s', "http-stat-port", "HTTP statistics port",
td::OptionParser::parse_integer(http_stat_port));
options.add_option('d', "dir", "server working directory", td::OptionParser::parse_string(working_directory));
Expand Down Expand Up @@ -306,10 +309,11 @@ int main(int argc, char *argv[]) {
TRY_STATUS(td::IPAddress::get_ip_address(ip_address.str()));
parameters->http_proxy_ip = ip_address.str();
return td::Status::OK();
});

options.add_checked_option('\0', "http-proxy-port", PSLICE() << "HTTP proxy port (default is " << parameters->http_proxy_port << ")",
td::OptionParser::parse_integer(parameters->http_proxy_port));
});

options.add_checked_option('\0', "http-proxy-port",
PSLICE() << "HTTP proxy port (default is " << parameters->http_proxy_port << ")",
td::OptionParser::parse_integer(parameters->http_proxy_port));
options.add_check([&] {
if (parameters->api_id_ <= 0 || parameters->api_hash_.empty()) {
return td::Status::Error("You must provide valid api-id and api-hash obtained at https://my.telegram.org");
Expand Down Expand Up @@ -486,7 +490,7 @@ int main(int argc, char *argv[]) {

sched
.create_actor_unsafe<HttpServer>(
SharedData::get_client_scheduler_id(), "HttpServer", http_ip_address, http_port,
SharedData::get_client_scheduler_id(), "HttpServer", http_ip_address, http_port, idle_timeout,
[client_manager, shared_data] {
return td::ActorOwn<td::HttpInboundConnection::Callback>(
td::create_actor<HttpConnection>("HttpConnection", client_manager, shared_data));
Expand All @@ -497,6 +501,7 @@ int main(int argc, char *argv[]) {
sched
.create_actor_unsafe<HttpServer>(
SharedData::get_client_scheduler_id(), "HttpStatsServer", http_stat_ip_address, http_stat_port,
idle_timeout,
[client_manager] {
return td::ActorOwn<td::HttpInboundConnection::Callback>(
td::create_actor<HttpStatConnection>("HttpStatConnection", client_manager));
Expand Down

0 comments on commit 0f8afaa

Please sign in to comment.