diff --git a/Cargo.toml b/Cargo.toml index 35d77c21..8f2a86cc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,7 +29,7 @@ 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"] } diff --git a/src/bot/mod.rs b/src/bot/mod.rs index e2201600..068ca670 100644 --- a/src/bot/mod.rs +++ b/src/bot/mod.rs @@ -1,6 +1,9 @@ use crate::types::ParseMode; -use reqwest::Client; -use std::sync::Arc; +use reqwest::{ + header::{HeaderMap, CONNECTION}, + Client, ClientBuilder, +}; +use std::{sync::Arc, time::Duration}; mod api; mod download; @@ -55,7 +58,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 @@ -76,6 +79,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/dispatching/update_listeners.rs b/src/dispatching/update_listeners.rs index 5055dc6c..0c5f6f16 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: Bot) -> 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..e376c3fd 100644 --- a/src/net/request.rs +++ b/src/net/request.rs @@ -4,6 +4,9 @@ use serde::{de::DeserializeOwned, Serialize}; use crate::{requests::ResponseResult, RequestError}; use super::{TelegramResponse, TELEGRAM_API_URL}; +use std::time::Duration; + +const DELAY_ON_SERVER_ERROR: Duration = Duration::from_secs(10); pub async fn request_multipart( client: &Client, @@ -48,6 +51,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)?, )