Skip to main content

ProxyList

Struct ProxyList 

Source
pub struct ProxyList { /* private fields */ }
Expand description

A pool of proxy URLs rotated round-robin, with a cooldown applied to proxies that were recently marked bad (e.g. after a connect failure or a 407 response from the proxy itself).

URLs are validated and canonicalised on construction: a bare host:port becomes http://host:port/, scheme and host are lowercased, default ports are dropped, duplicates (after canonicalisation) are removed, and SOCKS schemes are rejected unless the socks feature is enabled. pick and as_slice return the canonical form; mark_bad and in_cooldown accept either form.

All state lives behind an internal mutex that is never held across an .await, so a single ProxyList can be used concurrently from many tasks. Its Debug output hides proxy credentials.

§Examples

use reqwest_rotate::ProxyList;

let proxies = ProxyList::new([
    "http://proxy-a.example:8080",
    "proxy-b.example:8080", // no scheme: treated as http://
])
.unwrap();

assert_eq!(proxies.pick(), Some("http://proxy-a.example:8080/"));
assert_eq!(proxies.pick(), Some("http://proxy-b.example:8080/"));
assert_eq!(proxies.pick(), Some("http://proxy-a.example:8080/"));

Implementations§

Source§

impl ProxyList

Source

pub fn new<I, S>(proxies: I) -> Result<Self, Error>
where I: IntoIterator<Item = S>, S: AsRef<str>,

Builds a proxy list from proxy URLs such as "http://user:pass@host:port". An empty iterator is valid and means “no proxies”: pick then always returns None.

§Errors

Returns Error::InvalidProxy if an entry is blank, cannot be parsed as a URL, or uses an unsupported scheme.

§Examples
use reqwest_rotate::ProxyList;

let empty = ProxyList::new(Vec::<String>::new()).unwrap();
assert!(empty.is_empty());
assert_eq!(empty.pick(), None);
Source

pub fn is_empty(&self) -> bool

Returns true if no proxies were configured.

Source

pub fn len(&self) -> usize

Number of configured (distinct) proxies, regardless of cooldown state.

Source

pub fn as_slice(&self) -> &[String]

All configured proxy URLs, canonicalised, in rotation order.

Source

pub fn pick(&self) -> Option<&str>

Picks the next proxy in round-robin order, skipping proxies that are still in cooldown when a healthy one is available. Returns None only if the list is empty.

If every proxy is in cooldown, the one whose cooldown ends soonest is returned anyway: a proxy that might work beats no proxy at all. A proxy that then answers a request sent through a RotatingClient that rotates over this list is taken out of cooldown at once; the others stay marked until their own cooldown expires.

Source

pub fn mark_bad(&self, proxy: &str, cooldown: Duration) -> bool

Marks a proxy as bad for cooldown: pick will skip it, unless every proxy is unhealthy, until the cooldown expires or the proxy answers a request sent through a RotatingClient, whichever comes first.

proxy is matched in canonical form, so both what pick returned and what you originally configured work. Returns false if it is not in this list, in which case nothing changes.

Source

pub fn in_cooldown(&self, proxy: &str) -> bool

Returns true if proxy (in either form, see mark_bad) is currently in cooldown.

Trait Implementations§

Source§

impl Debug for ProxyList

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more