1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//! Proxy source port for browser context pools.
//!
//! This module provides the [`ProxySource`] and [`ProxyLease`] traits that
//! decouple `stygian-browser` from any concrete proxy implementation. The
//! browser crate owns the trait definitions; `stygian-proxy` implements them.
//!
//! Wire in a real proxy pool by setting [`BrowserConfigBuilder::proxy_source`](crate::config::BrowserConfigBuilder::proxy_source) to an
//! `Arc<dyn ProxySource>`. `stygian-proxy` provides a ready-made
//! implementation via `ProxyManagerBridge` when compiled with its `browser`
//! feature.
//!
//! # Example
//!
//! ```rust,no_run
//! use std::sync::Arc;
//! use async_trait::async_trait;
//! use stygian_browser::proxy::{ProxyLease, ProxySource, DirectLease};
//! use stygian_browser::error::Result;
//!
//! #[derive(Debug)]
//! struct StaticProxy {
//! url: String,
//! }
//!
//! #[async_trait]
//! impl ProxySource for StaticProxy {
//! async fn bind_proxy(&self) -> Result<(String, Box<dyn ProxyLease>)> {
//! Ok((self.url.clone(), Box::new(DirectLease)))
//! }
//! }
//!
//! let cfg = stygian_browser::BrowserConfig::builder()
//! .proxy_source(Arc::new(StaticProxy { url: "http://proxy.example.com:8080".into() }))
//! .build();
//! ```
use fmt;
use async_trait;
use crateResult;
// ─── ProxyLease ───────────────────────────────────────────────────────────────
/// RAII guard for a proxy acquired from a [`ProxySource`].
///
/// Held for the lifetime of the browser instance using the proxy. Call
/// [`mark_success`](ProxyLease::mark_success) when the browser session
/// completes cleanly. Dropping without calling it signals a failure to the
/// underlying circuit breaker (if any).
///
/// # Example
///
/// ```
/// use stygian_browser::proxy::{ProxyLease, DirectLease};
/// let lease: Box<dyn ProxyLease> = Box::new(DirectLease);
/// lease.mark_success(); // no-op for DirectLease
/// ```
// ─── DirectLease ─────────────────────────────────────────────────────────────
/// A no-op [`ProxyLease`] for use when no proxy is configured.
///
/// All methods are no-ops. Use this as the lease type in [`ProxySource`]
/// implementations that do not need circuit-breaker tracking.
///
/// # Example
///
/// ```
/// use stygian_browser::proxy::{ProxyLease, DirectLease};
/// let lease = DirectLease;
/// lease.mark_success(); // no-op
/// ```
;
// ─── ProxySource ──────────────────────────────────────────────────────────────
/// Source of proxies for browser context pools.
///
/// Implement this trait and pass an `Arc<dyn ProxySource>` to
/// [`BrowserConfig::builder().proxy_source(...)`](crate::config::BrowserConfigBuilder::proxy_source)
/// to enable per-context proxy rotation with circuit-breaker support.
///
/// Each call to [`bind_proxy`](ProxySource::bind_proxy) acquires a proxy URL
/// and an RAII [`ProxyLease`] that must be held for the lifetime of the
/// browser instance.
///
/// `stygian-proxy` provides a ready-made implementation via
/// `ProxyManagerBridge` when compiled with the `browser` feature.
///
/// # Example
///
/// ```rust,no_run
/// use std::sync::Arc;
/// use async_trait::async_trait;
/// use stygian_browser::proxy::{ProxyLease, ProxySource, DirectLease};
/// use stygian_browser::error::Result;
///
/// #[derive(Debug)]
/// struct RoundRobinProxy {
/// urls: Vec<String>,
/// }
///
/// #[async_trait]
/// impl ProxySource for RoundRobinProxy {
/// async fn bind_proxy(&self) -> Result<(String, Box<dyn ProxyLease>)> {
/// let url = self.urls[0].clone(); // simplified — real impl would rotate
/// Ok((url, Box::new(DirectLease)))
/// }
/// }
///
/// let source = Arc::new(RoundRobinProxy { urls: vec!["http://p.example.com:8080".into()] });
/// let cfg = stygian_browser::BrowserConfig::builder()
/// .proxy_source(source)
/// .build();
/// ```