Merge pull request #90 from teloxide/pub_client_settings

Add `net::default_reqwest_settings` function
This commit is contained in:
Waffle Lapkin 2021-05-13 19:35:11 +03:00 committed by GitHub
commit 897ba7c941
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 35 deletions

View file

@ -13,11 +13,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Getters for fields nested in `Chat` ([#80][pr80])
- API errors: `ApiError::NotEnoughRightsToManagePins`, `ApiError::BotKickedFromSupergroup` ([#84][pr84])
- Telegram bot API 5.2 support ([#86][pr86])
- `net::default_reqwest_settings` function ([#90][pr90])
[pr75]: https://github.com/teloxide/teloxide-core/pull/75
[pr80]: https://github.com/teloxide/teloxide-core/pull/80
[pr84]: https://github.com/teloxide/teloxide-core/pull/84
[pr86]: https://github.com/teloxide/teloxide-core/pull/86
[pr90]: https://github.com/teloxide/teloxide-core/pull/90
### Changed

View file

@ -1,9 +1,6 @@
use std::{future::Future, sync::Arc, time::Duration};
use std::{future::Future, sync::Arc};
use reqwest::{
header::{HeaderMap, CONNECTION},
Client, ClientBuilder,
};
use reqwest::Client;
use serde::{de::DeserializeOwned, Serialize};
use crate::{
@ -17,8 +14,7 @@ mod api;
mod api_url;
mod download;
pub(crate) const TELOXIDE_TOKEN: &str = "TELOXIDE_TOKEN";
pub(crate) const TELOXIDE_PROXY: &str = "TELOXIDE_PROXY";
const TELOXIDE_TOKEN: &str = "TELOXIDE_TOKEN";
/// A requests sender.
///
@ -76,13 +72,18 @@ impl Bot {
where
S: Into<String>,
{
Self::with_client(token, build_sound_bot())
let client = net::default_reqwest_settings()
.build()
.expect("Client creation failed");
Self::with_client(token, client)
}
/// Creates a new `Bot` with the specified token and your
/// [`reqwest::Client`].
///
/// # Caution
///
/// Your custom client might not be configured correctly to be able to work
/// in long time durations, see [issue 223].
///
@ -239,30 +240,6 @@ impl Bot {
}
}
/// Returns a builder with safe settings.
///
/// By "safe settings" I mean that a client will be able to work in long time
/// durations, see the [issue 223].
///
/// [issue 223]: https://github.com/teloxide/teloxide/issues/223
pub(crate) fn sound_bot() -> ClientBuilder {
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)
}
pub(crate) fn build_sound_bot() -> Client {
sound_bot().build().expect("creating reqwest::Client")
}
fn get_env(env: &'static str) -> String {
std::env::var(env).unwrap_or_else(|_| panic!("Cannot get the {} env variable", env))
}

View file

@ -1,5 +1,7 @@
//! Network-specific API.
use std::time::Duration;
pub use self::download::{download_file, download_file_stream, Download};
pub(crate) use self::{
@ -33,19 +35,55 @@ pub const TELEGRAM_API_URL: &str = "https://api.telegram.org";
///
/// If `TELOXIDE_PROXY` exists, but isn't correct url.
pub fn client_from_env() -> reqwest::Client {
use crate::bot::{sound_bot, TELOXIDE_PROXY};
use reqwest::Proxy;
let builder = sound_bot();
const TELOXIDE_PROXY: &str = "TELOXIDE_PROXY";
let builder = default_reqwest_settings();
match std::env::var(TELOXIDE_PROXY).ok() {
Some(proxy) => builder.proxy(Proxy::all(&proxy).expect("creating reqwest::Proxy")),
Some(proxy) => builder.proxy(Proxy::all(&proxy).expect("reqwest::Proxy creation failed")),
None => builder,
}
.build()
.expect("creating reqwest::Client")
}
/// Returns a reqwest client builder with default settings.
///
/// Client built from default settings is supposed to work over long time
/// durations, see the [issue 223].
///
/// The current settings are:
/// - The `connection/keep-alive` default header.
/// - A connection timeout of 5 seconds.
/// - A timeout of 17 seconds.
/// - `tcp_nodelay` is on.
///
/// ## Notes
/// 1. The settings may change in the future.
/// 2. If you are using the polling mechanism to get updates, the timeout
/// configured in the client should be bigger than the polling timeout.
/// 3. If you alter the current settings listed above, your bot will not be
/// guaranteed to work over long time durations.
///
/// [issue 223]: https://github.com/teloxide/teloxide/issues/223
pub fn default_reqwest_settings() -> reqwest::ClientBuilder {
use reqwest::header::{HeaderMap, CONNECTION};
let mut headers = HeaderMap::new();
headers.insert(CONNECTION, "keep-alive".parse().unwrap());
let connect_timeout = Duration::from_secs(5);
let timeout = connect_timeout + Duration::from_secs(12);
reqwest::Client::builder()
.connect_timeout(connect_timeout)
.timeout(timeout)
.tcp_nodelay(true)
.default_headers(headers)
}
/// Creates URL for making HTTPS requests. See the [Telegram documentation].
///
/// [Telegram documentation]: https://core.telegram.org/bots/api#making-requests