From 3866fd0d7ec7ec461861699aba7c541e22c48f97 Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi Date: Wed, 8 Jul 2020 05:52:42 +0600 Subject: [PATCH 01/10] Fix bot's inability to respond in long time periods Closes https://github.com/teloxide/teloxide/issues/223. --- src/dispatching/update_listeners.rs | 4 ++-- src/net/request.rs | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/dispatching/update_listeners.rs b/src/dispatching/update_listeners.rs index 840ea90a..66c72b67 100644 --- a/src/dispatching/update_listeners.rs +++ b/src/dispatching/update_listeners.rs @@ -120,11 +120,11 @@ pub trait UpdateListener: Stream> { } impl UpdateListener for S where S: Stream> {} -/// Returns a long polling update listener with `timeout` of 1 minute. +/// Returns a long polling update listener with `timeout` of 10 seconds. /// /// See also: [`polling`](polling). pub fn polling_default(bot: Arc) -> impl UpdateListener { - polling(bot, Some(Duration::from_secs(60)), None, None) + polling(bot, Some(Duration::from_secs(10)), None, None) } /// Returns a long/short polling update listener with some additional options. diff --git a/src/net/request.rs b/src/net/request.rs index 9f8d7069..171f923e 100644 --- a/src/net/request.rs +++ b/src/net/request.rs @@ -4,6 +4,7 @@ use serde::{de::DeserializeOwned, Serialize}; use crate::{requests::ResponseResult, RequestError}; use super::{TelegramResponse, TELEGRAM_API_URL}; +use reqwest::header::CONNECTION; pub async fn request_multipart( client: &Client, @@ -16,6 +17,7 @@ where { let response = client .post(&super::method_url(TELEGRAM_API_URL, token, method_name)) + .header(CONNECTION, "keep-alive") .multipart(params) .send() .await @@ -36,6 +38,7 @@ where { let response = client .post(&super::method_url(TELEGRAM_API_URL, token, method_name)) + .header(CONNECTION, "keep-alive") .json(params) .send() .await From f5941973db037c36f590f957312e3b5e98aff887 Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi Date: Wed, 8 Jul 2020 06:19:47 +0600 Subject: [PATCH 02/10] Wait 10 seconds if a server error occurs --- src/net/request.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/net/request.rs b/src/net/request.rs index 171f923e..8678dd45 100644 --- a/src/net/request.rs +++ b/src/net/request.rs @@ -5,6 +5,9 @@ use crate::{requests::ResponseResult, RequestError}; use super::{TelegramResponse, TELEGRAM_API_URL}; use reqwest::header::CONNECTION; +use std::time::Duration; + +const DELAY_ON_SERVER_ERROR: Duration = Duration::from_secs(10); pub async fn request_multipart( client: &Client, @@ -51,6 +54,10 @@ async fn process_response(response: Response) -> ResponseResult where T: DeserializeOwned, { + if response.status().is_server_error() { + tokio::time::delay_for(DELAY_ON_SERVER_ERROR).await; + } + serde_json::from_str::>( &response.text().await.map_err(RequestError::NetworkError)?, ) From 981e79e1c7d98ca49f02a26e7066a82b599b5cc5 Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi Date: Wed, 8 Jul 2020 06:21:40 +0600 Subject: [PATCH 03/10] Add 'Connection: keep-alive' to net::download --- src/net/download.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/net/download.rs b/src/net/download.rs index 05f08f5d..400cae86 100644 --- a/src/net/download.rs +++ b/src/net/download.rs @@ -4,6 +4,7 @@ use tokio::io::{AsyncWrite, AsyncWriteExt}; use crate::errors::DownloadError; use super::TELEGRAM_API_URL; +use reqwest::header::CONNECTION; pub async fn download_file( client: &Client, @@ -16,6 +17,7 @@ where { let mut res = client .get(&super::file_url(TELEGRAM_API_URL, token, path)) + .header(CONNECTION, "keep-alive") .send() .await? .error_for_status()?; @@ -35,6 +37,7 @@ pub async fn download_file_stream( ) -> Result>, reqwest::Error> { let res = client .get(&super::file_url(TELEGRAM_API_URL, token, path)) + .header(CONNECTION, "keep-alive") .send() .await? .error_for_status()?; From 178b56ab1c0eb9ec5a0754a2910eeb9d6e9d8cbe Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi Date: Sat, 11 Jul 2020 02:06:27 +0600 Subject: [PATCH 04/10] Download reqwest from gh --- Cargo.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 5387a2ae..e91252be 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,7 +35,8 @@ serde = { version = "1.0.114", features = ["derive"] } tokio = { version = "0.2.21", features = ["full"] } tokio-util = { version = "0.3.1", features = ["full"] } -reqwest = { version = "0.10.6", features = ["json", "stream"] } +//reqwest = { version = "0.10.6", features = ["json", "stream"] } TODO: uncomment this line +reqwest = { git = "https://github.com/seanmonstar/reqwest", branch = "master", features = ["json", "stream"] } log = "0.4.8" lockfree = "0.5.1" bytes = "0.5.5" From 8059f64044ee5ee743007fedee404d4ef2ecc84c Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi Date: Sat, 11 Jul 2020 02:07:12 +0600 Subject: [PATCH 05/10] Update Cargo.toml --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index e91252be..eb4af37d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,7 +35,7 @@ serde = { version = "1.0.114", features = ["derive"] } tokio = { version = "0.2.21", features = ["full"] } tokio-util = { version = "0.3.1", features = ["full"] } -//reqwest = { version = "0.10.6", features = ["json", "stream"] } TODO: uncomment this line +#reqwest = { version = "0.10.6", features = ["json", "stream"] } TODO: uncomment this line reqwest = { git = "https://github.com/seanmonstar/reqwest", branch = "master", features = ["json", "stream"] } log = "0.4.8" lockfree = "0.5.1" From f3e482d47efa0a5dccc613cb0abce7a7bf64705a Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi Date: Sat, 11 Jul 2020 02:10:52 +0600 Subject: [PATCH 06/10] serde_json = 1.0.55 --- Cargo.toml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index eb4af37d..c62a0bbf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,14 +29,13 @@ cbor-serializer = ["serde_cbor"] bincode-serializer = ["bincode"] [dependencies] -serde_json = "1.0.56" +serde_json = "1.0.55" serde = { version = "1.0.114", features = ["derive"] } tokio = { version = "0.2.21", features = ["full"] } tokio-util = { version = "0.3.1", features = ["full"] } -#reqwest = { version = "0.10.6", features = ["json", "stream"] } TODO: uncomment this line -reqwest = { git = "https://github.com/seanmonstar/reqwest", branch = "master", features = ["json", "stream"] } +reqwest = { version = "0.10.6", features = ["json", "stream"] } log = "0.4.8" lockfree = "0.5.1" bytes = "0.5.5" From 9dbff2b5228d25c3d5660663584affee47aa86cf Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi Date: Thu, 16 Jul 2020 21:04:11 +0600 Subject: [PATCH 07/10] Move more settings into Bot --- src/bot/mod.rs | 28 ++++++++++++++++++++++++---- src/net/download.rs | 3 --- src/net/request.rs | 3 --- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/bot/mod.rs b/src/bot/mod.rs index 433a6cab..b9af4d19 100644 --- a/src/bot/mod.rs +++ b/src/bot/mod.rs @@ -1,5 +1,8 @@ -use reqwest::Client; -use std::sync::Arc; +use reqwest::{ + header::{HeaderMap, CONNECTION}, + Client, ClientBuilder, +}; +use std::{sync::Arc, time::Duration}; mod api; mod download; @@ -20,7 +23,7 @@ impl Bot { /// /// [`reqwest::Client`]: https://docs.rs/reqwest/0.10.1/reqwest/struct.Client.html pub fn from_env() -> Arc { - Self::from_env_with_client(Client::new()) + Self::from_env_with_client(sound_bot()) } /// Creates a new `Bot` with the `TELOXIDE_TOKEN` environmental variable (a @@ -46,7 +49,7 @@ impl Bot { where S: Into, { - Self::with_client(token, Client::new()) + Self::with_client(token, sound_bot()) } /// Creates a new `Bot` with the specified token and your @@ -61,6 +64,23 @@ impl Bot { } } +// See https://github.com/teloxide/teloxide/issues/223. +fn sound_bot() -> Client { + let mut headers = HeaderMap::new(); + headers.insert(CONNECTION, "keep-alive".parse().unwrap()); + + let connect_timeout = Duration::from_secs(5); + let timeout = 10; + + ClientBuilder::new() + .connect_timeout(connect_timeout) + .timeout(Duration::from_secs(connect_timeout.as_secs() + timeout + 2)) + .tcp_nodelay_(true) + .default_headers(headers) + .build() + .expect("Cannot build reqwest::Client") +} + impl Bot { // TODO: const fn pub fn token(&self) -> &str { diff --git a/src/net/download.rs b/src/net/download.rs index 400cae86..05f08f5d 100644 --- a/src/net/download.rs +++ b/src/net/download.rs @@ -4,7 +4,6 @@ use tokio::io::{AsyncWrite, AsyncWriteExt}; use crate::errors::DownloadError; use super::TELEGRAM_API_URL; -use reqwest::header::CONNECTION; pub async fn download_file( client: &Client, @@ -17,7 +16,6 @@ where { let mut res = client .get(&super::file_url(TELEGRAM_API_URL, token, path)) - .header(CONNECTION, "keep-alive") .send() .await? .error_for_status()?; @@ -37,7 +35,6 @@ pub async fn download_file_stream( ) -> Result>, reqwest::Error> { let res = client .get(&super::file_url(TELEGRAM_API_URL, token, path)) - .header(CONNECTION, "keep-alive") .send() .await? .error_for_status()?; diff --git a/src/net/request.rs b/src/net/request.rs index 8678dd45..e376c3fd 100644 --- a/src/net/request.rs +++ b/src/net/request.rs @@ -4,7 +4,6 @@ use serde::{de::DeserializeOwned, Serialize}; use crate::{requests::ResponseResult, RequestError}; use super::{TelegramResponse, TELEGRAM_API_URL}; -use reqwest::header::CONNECTION; use std::time::Duration; const DELAY_ON_SERVER_ERROR: Duration = Duration::from_secs(10); @@ -20,7 +19,6 @@ where { let response = client .post(&super::method_url(TELEGRAM_API_URL, token, method_name)) - .header(CONNECTION, "keep-alive") .multipart(params) .send() .await @@ -41,7 +39,6 @@ where { let response = client .post(&super::method_url(TELEGRAM_API_URL, token, method_name)) - .header(CONNECTION, "keep-alive") .json(params) .send() .await From 4a314c894d15febb8dc93ccc299f6707f1610e73 Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi Date: Fri, 24 Jul 2020 16:31:43 +0600 Subject: [PATCH 08/10] Fix rustfmt check --- src/bot/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bot/mod.rs b/src/bot/mod.rs index 8cbb7655..068ca670 100644 --- a/src/bot/mod.rs +++ b/src/bot/mod.rs @@ -1,9 +1,9 @@ +use crate::types::ParseMode; use reqwest::{ header::{HeaderMap, CONNECTION}, Client, ClientBuilder, }; use std::{sync::Arc, time::Duration}; -use crate::types::ParseMode; mod api; mod download; From 5db72a26a2e3209093d047aec5f68da519d0fdcb Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi Date: Fri, 24 Jul 2020 16:38:12 +0600 Subject: [PATCH 09/10] Import Arc into update_listeners.rs --- src/dispatching/update_listeners.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dispatching/update_listeners.rs b/src/dispatching/update_listeners.rs index 7b073afb..78a0001e 100644 --- a/src/dispatching/update_listeners.rs +++ b/src/dispatching/update_listeners.rs @@ -112,7 +112,7 @@ use crate::{ RequestError, }; -use std::{convert::TryInto, time::Duration}; +use std::{convert::TryInto, sync::Arc, time::Duration}; /// A generic update listener. pub trait UpdateListener: Stream> { From e14b2b9fd0c73a29dc10827a3391fb71b0053038 Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi Date: Fri, 24 Jul 2020 16:54:56 +0600 Subject: [PATCH 10/10] Fix compilation --- src/dispatching/update_listeners.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/dispatching/update_listeners.rs b/src/dispatching/update_listeners.rs index 78a0001e..0c5f6f16 100644 --- a/src/dispatching/update_listeners.rs +++ b/src/dispatching/update_listeners.rs @@ -112,7 +112,7 @@ use crate::{ RequestError, }; -use std::{convert::TryInto, sync::Arc, time::Duration}; +use std::{convert::TryInto, time::Duration}; /// A generic update listener. pub trait UpdateListener: Stream> { @@ -123,7 +123,7 @@ impl UpdateListener for S where S: Stream> {} /// Returns a long polling update listener with `timeout` of 10 seconds. /// /// See also: [`polling`](polling). -pub fn polling_default(bot: Arc) -> impl UpdateListener { +pub fn polling_default(bot: Bot) -> impl UpdateListener { polling(bot, Some(Duration::from_secs(10)), None, None) }