Skip to content

Commit

Permalink
feat(client): An asynchronous DNS resolver is used by default (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
0x676e67 authored Feb 20, 2025
1 parent e833c89 commit 6c6f994
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 17 deletions.
1 change: 0 additions & 1 deletion examples/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ async def main():
client = Client(
impersonate=Impersonate.Firefox133,
user_agent="rnet",
async_dns=True,
proxies=[
Proxy.http("socks5h://abc:[email protected]:1080"),
Proxy.https(url="socks5h://127.0.0.1:1080", username="abc", password="def"),
Expand Down
15 changes: 9 additions & 6 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use arc_swap::{ArcSwap, Guard};
use pyo3::prelude::*;
use pyo3_stub_gen::derive::{gen_stub_pyclass, gen_stub_pymethods};
use rquest::{
dns::LookupIpStrategy,
header::{HeaderMap, HeaderName, HeaderValue},
redirect::Policy,
Url,
Expand All @@ -29,6 +28,11 @@ macro_rules! apply_option {
$builder = $builder.$method(value);
}
};
(apply_if_ok, $builder:expr, $result:expr, $method:ident) => {
if let Ok(value) = $result() {
$builder = $builder.$method(value);
}
};
(apply_if_some_ref, $builder:expr, $option:expr, $method:ident) => {
if let Some(value) = $option.take() {
$builder = $builder.$method(&value);
Expand Down Expand Up @@ -80,7 +84,9 @@ impl Client {
/// ```
pub fn default() -> &'static Self {
static CLIENT: LazyLock<Client> = LazyLock::new(|| {
rquest::Client::builder()
let mut builder = rquest::Client::builder();
apply_option!(apply_if_ok, builder, dns::get_or_try_init, dns_resolver);
builder
.no_hickory_dns()
.no_keepalive()
.build()
Expand Down Expand Up @@ -576,10 +582,7 @@ impl Client {
apply_option!(apply_if_some, builder, params.cookie_store, cookie_store);

// Async resolver options.
if params.async_dns.unwrap_or(false) {
let hickory_dns_resolver = dns::get_or_try_init(LookupIpStrategy::Ipv4AndIpv6)?;
builder = builder.dns_resolver(hickory_dns_resolver);
}
apply_option!(apply_if_ok, builder, dns::get_or_try_init, dns_resolver);

// Timeout options.
apply_option!(
Expand Down
7 changes: 2 additions & 5 deletions src/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,12 @@ use std::sync::{Arc, OnceLock};
///
/// let resolver = get_or_try_init(LookupIpStrategy::default()).unwrap();
/// ```
pub fn get_or_try_init<S>(strategy: S) -> crate::Result<Arc<HickoryDnsResolver>>
where
S: Into<Option<LookupIpStrategy>>,
{
pub fn get_or_try_init() -> crate::Result<Arc<HickoryDnsResolver>> {
static DNS_RESOLVER: OnceLock<Result<Arc<HickoryDnsResolver>, &'static str>> = OnceLock::new();

DNS_RESOLVER
.get_or_init(move || {
HickoryDnsResolver::new(strategy.into())
HickoryDnsResolver::new(LookupIpStrategy::Ipv4AndIpv6)
.map(Arc::new)
.map_err(|err| {
#[cfg(feature = "logging")]
Expand Down
5 changes: 0 additions & 5 deletions src/param/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,6 @@ pub struct ClientParams {
#[pyo3(get)]
pub cookie_store: Option<bool>,

/// Whether to use async DNS resolver.
#[pyo3(get)]
pub async_dns: Option<bool>,

// ========= Timeout options =========
/// The timeout to use for the request. (in seconds)
#[pyo3(get)]
Expand Down Expand Up @@ -259,7 +255,6 @@ impl<'py> FromPyObject<'py> for ClientParams {
extract_option!(ob, params, referer);
extract_option!(ob, params, allow_redirects);
extract_option!(ob, params, cookie_store);
extract_option!(ob, params, async_dns);

extract_option!(ob, params, timeout);
extract_option!(ob, params, connect_timeout);
Expand Down

0 comments on commit 6c6f994

Please sign in to comment.