1use std::fmt;
82use std::sync::atomic::{AtomicUsize, Ordering};
83use std::sync::Arc;
84
85use dashmap::DashMap;
86use tokio::sync::{OwnedSemaphorePermit, Semaphore};
87
88use crate::error::{Error, Result};
89
90#[derive(Debug, Clone, Default)]
107pub struct PoolConfig {
108 pub max_idle_connections: Option<usize>,
114 pub max_idle_connections_per_host: Option<usize>,
118 pub max_connections: Option<usize>,
124 pub max_connections_per_host: Option<usize>,
131 pub max_in_flight_requests: Option<usize>,
136 pub max_in_flight_requests_per_origin: Option<usize>,
140 pub idle_timeout: Option<std::time::Duration>,
144}
145
146impl PoolConfig {
147 #[must_use]
149 pub(crate) fn effective_max_in_flight(&self) -> Option<usize> {
150 self.max_in_flight_requests.or(self.max_connections)
151 }
152
153 #[must_use]
155 pub(crate) fn effective_max_in_flight_per_origin(&self) -> Option<usize> {
156 self.max_in_flight_requests_per_origin
157 .or(self.max_connections_per_host)
158 }
159}
160
161#[derive(Debug, Clone, PartialEq, Eq, Hash)]
169pub(crate) struct OriginKey {
170 scheme: String,
172 host: String,
174 port: u16,
176 proxy_host: Option<String>,
178 proxy_port: Option<u16>,
180 proxy_scheme: Option<String>,
183 is_tunnel: bool,
186}
187
188impl OriginKey {
189 pub(crate) fn from_url(scheme: &str, url: &url::Url) -> Option<Self> {
191 let host = url.host_str()?.to_owned();
192 let port = url.port_or_known_default()?;
193 Some(Self {
194 scheme: scheme.to_owned(),
195 host,
196 port,
197 proxy_host: None,
198 proxy_port: None,
199 proxy_scheme: None,
200 is_tunnel: false,
201 })
202 }
203
204 #[cfg(feature = "proxy")]
206 pub(crate) fn from_url_with_proxy_scheme(
207 scheme: &str,
208 url: &url::Url,
209 proxy_host: Option<&str>,
210 proxy_port: Option<u16>,
211 proxy_scheme: Option<&str>,
212 is_tunnel: bool,
213 ) -> Option<Self> {
214 let host = url.host_str()?.to_owned();
215 let port = url.port_or_known_default()?;
216 Some(Self {
217 scheme: scheme.to_owned(),
218 host,
219 port,
220 proxy_host: proxy_host.map(str::to_owned),
221 proxy_port,
222 proxy_scheme: proxy_scheme.map(str::to_owned),
223 is_tunnel,
224 })
225 }
226
227 #[cfg(any(test, feature = "test-util"))]
230 #[allow(dead_code)]
231 pub(crate) fn from_parts(scheme: &str, host: &str, port: u16) -> Self {
232 Self {
233 scheme: scheme.to_owned(),
234 host: host.to_owned(),
235 port,
236 proxy_host: None,
237 proxy_port: None,
238 proxy_scheme: None,
239 is_tunnel: false,
240 }
241 }
242
243 #[cfg(all(feature = "proxy", any(test, feature = "test-util")))]
246 #[allow(dead_code)]
247 pub(crate) fn from_parts_with_proxy(
248 scheme: &str,
249 host: &str,
250 port: u16,
251 proxy_host: Option<&str>,
252 proxy_port: Option<u16>,
253 proxy_scheme: Option<&str>,
254 is_tunnel: bool,
255 ) -> Self {
256 Self {
257 scheme: scheme.to_owned(),
258 host: host.to_owned(),
259 port,
260 proxy_host: proxy_host.map(str::to_owned),
261 proxy_port,
262 proxy_scheme: proxy_scheme.map(str::to_owned),
263 is_tunnel,
264 }
265 }
266
267 #[cfg(any(test, feature = "test-util"))]
269 #[allow(dead_code)]
270 pub(crate) fn scheme(&self) -> &str {
271 &self.scheme
272 }
273
274 pub(crate) fn host(&self) -> &str {
276 &self.host
277 }
278
279 #[cfg(any(test, feature = "test-util"))]
281 #[allow(dead_code)]
282 pub(crate) fn port(&self) -> u16 {
283 self.port
284 }
285
286 #[cfg(any(test, feature = "test-util"))]
288 #[allow(dead_code)]
289 pub(crate) fn proxy_host(&self) -> Option<&str> {
290 self.proxy_host.as_deref()
291 }
292
293 #[cfg(any(test, feature = "test-util"))]
295 #[allow(dead_code)]
296 pub(crate) fn proxy_port(&self) -> Option<u16> {
297 self.proxy_port
298 }
299
300 #[cfg(any(test, feature = "test-util"))]
302 #[allow(dead_code)]
303 pub(crate) fn is_tunnel(&self) -> bool {
304 self.is_tunnel
305 }
306}
307
308impl fmt::Display for OriginKey {
309 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
310 write!(f, "{}://{}:{}", self.scheme, self.host, self.port)?;
311 if let (Some(ref ph), Some(pp)) = (&self.proxy_host, self.proxy_port) {
312 if let Some(ref scheme) = self.proxy_scheme {
313 write!(f, " via {scheme}://{ph}:{pp}")?;
314 } else {
315 write!(f, " via {ph}:{pp}")?;
316 }
317 if self.is_tunnel {
318 write!(f, " tunnel")?;
319 }
320 }
321 Ok(())
322 }
323}
324
325#[derive(Debug, Default)]
348pub struct PoolMetrics {
349 pub acquisition_waits: AtomicUsize,
351 pub acquisition_cancellations: AtomicUsize,
353}
354
355pub struct PoolGuard {
359 pool: Arc<PoolInner>,
360 pub(crate) origin: Option<OriginKey>,
361 #[allow(dead_code)]
365 pub(crate) global_permit: Option<OwnedSemaphorePermit>,
366 #[allow(dead_code)]
367 pub(crate) origin_permit: Option<OwnedSemaphorePermit>,
368}
369
370impl PoolGuard {
371 fn new(
373 pool: Arc<PoolInner>,
374 origin: Option<OriginKey>,
375 global_permit: Option<OwnedSemaphorePermit>,
376 origin_permit: Option<OwnedSemaphorePermit>,
377 ) -> Self {
378 Self {
379 pool,
380 origin,
381 global_permit,
382 origin_permit,
383 }
384 }
385
386 #[must_use]
388 #[allow(dead_code)] pub(crate) fn origin(&self) -> Option<&OriginKey> {
390 self.origin.as_ref()
391 }
392
393 #[allow(dead_code)]
395 pub fn host(&self) -> Option<&str> {
396 self.origin.as_ref().map(OriginKey::host)
397 }
398
399 #[must_use]
401 pub fn metrics(&self) -> &PoolMetrics {
402 &self.pool.metrics
403 }
404}
405
406impl Drop for PoolGuard {
407 fn drop(&mut self) {
408 }
411}
412
413struct PoolInner {
415 global_semaphore: Option<Arc<Semaphore>>,
417 per_origin: DashMap<OriginKey, Arc<PerOriginSemaphore>>,
419 per_origin_table_lock: tokio::sync::Mutex<()>,
425 config: PoolConfig,
427 metrics: PoolMetrics,
429}
430
431struct PerOriginSemaphore {
436 semaphore: Arc<Semaphore>,
437 waiters: AtomicUsize,
438}
439
440impl PerOriginSemaphore {
441 fn new(max_per_origin: usize) -> Self {
442 Self {
443 semaphore: Arc::new(Semaphore::new(max_per_origin)),
444 waiters: AtomicUsize::new(0),
445 }
446 }
447}
448
449struct WaiterGuard {
455 entry: Arc<PerOriginSemaphore>,
456}
457
458impl WaiterGuard {
459 fn new(entry: Arc<PerOriginSemaphore>) -> Self {
460 entry.waiters.fetch_add(1, Ordering::AcqRel);
461 Self { entry }
462 }
463}
464
465impl Drop for WaiterGuard {
466 fn drop(&mut self) {
467 self.entry.waiters.fetch_sub(1, Ordering::AcqRel);
468 }
469}
470
471#[derive(Clone)]
488pub struct Pool {
489 inner: Arc<PoolInner>,
490}
491
492impl Pool {
493 #[must_use]
495 pub fn new(config: PoolConfig) -> Self {
496 let global_semaphore = config
497 .effective_max_in_flight()
498 .map(|n| Arc::new(Semaphore::new(n)));
499
500 Self {
501 inner: Arc::new(PoolInner {
502 global_semaphore,
503 per_origin: DashMap::new(),
504 per_origin_table_lock: tokio::sync::Mutex::new(()),
505 config,
506 metrics: PoolMetrics::default(),
507 }),
508 }
509 }
510
511 #[must_use]
517 pub(crate) fn idle_timeout(&self) -> Option<std::time::Duration> {
518 self.inner.config.idle_timeout
519 }
520
521 pub(crate) async fn acquire(&self, origin: Option<&OriginKey>) -> Result<PoolGuard> {
534 let mut waited = false;
535 let mut global_permit: Option<OwnedSemaphorePermit> = None;
536 let mut origin_permit: Option<OwnedSemaphorePermit> = None;
537
538 if let Some(ref sem) = self.inner.global_semaphore {
541 if let Ok(permit) = sem.clone().try_acquire_owned() {
542 global_permit = Some(permit);
543 } else if let Ok(permit) = sem.clone().acquire_owned().await {
544 global_permit = Some(permit);
545 waited = true;
546 } else {
547 self.inner
548 .metrics
549 .acquisition_cancellations
550 .fetch_add(1, Ordering::Relaxed);
551 return Err(Error::Pool("global semaphore is closed".into()));
552 }
553 }
554
555 if let (Some(max_per_origin), Some(origin)) = (
558 self.inner.config.effective_max_in_flight_per_origin(),
559 origin,
560 ) {
561 let entry = if let Some(entry) = self.inner.per_origin.get(origin) {
567 entry.clone()
568 } else {
569 let table_lock = self.inner.per_origin_table_lock.lock().await;
580 let entry = if let Some(entry) = self.inner.per_origin.get(origin) {
581 entry.clone()
582 } else {
583 self.inner.per_origin.retain(|_, entry| {
584 entry.semaphore.available_permits() < max_per_origin
585 || entry.waiters.load(Ordering::Acquire) > 0
586 });
587 self.inner
588 .per_origin
589 .entry(origin.clone())
590 .or_insert_with(|| Arc::new(PerOriginSemaphore::new(max_per_origin)))
591 .clone()
592 };
593 drop(table_lock);
594 entry
595 };
596
597 let mut waiter_guard = Some(WaiterGuard::new(Arc::clone(&entry)));
600 if let Ok(permit) = entry.semaphore.clone().try_acquire_owned() {
604 waiter_guard.take();
605 origin_permit = Some(permit);
606 } else {
607 let permit = entry.semaphore.clone().acquire_owned().await;
608 waiter_guard.take();
609 if let Ok(permit) = permit {
610 origin_permit = Some(permit);
611 waited = true;
612 } else {
613 self.inner
614 .metrics
615 .acquisition_cancellations
616 .fetch_add(1, Ordering::Relaxed);
617 drop(global_permit);
618 return Err(Error::Pool("per-origin semaphore is closed".into()));
619 }
620 }
621 }
622
623 if waited {
624 self.inner
625 .metrics
626 .acquisition_waits
627 .fetch_add(1, Ordering::Relaxed);
628 }
629
630 Ok(PoolGuard::new(
631 self.inner.clone(),
632 origin.cloned(),
633 global_permit,
634 origin_permit,
635 ))
636 }
637
638 #[must_use]
640 pub fn metrics(&self) -> &PoolMetrics {
641 &self.inner.metrics
642 }
643}
644
645#[cfg(test)]
646mod tests {
647 use super::*;
648
649 #[test]
650 fn default_config_has_no_limits() {
651 let config = PoolConfig::default();
652 assert!(config.max_idle_connections.is_none());
653 assert!(config.max_idle_connections_per_host.is_none());
654 assert!(config.max_connections.is_none());
655 assert!(config.max_connections_per_host.is_none());
656 assert!(config.max_in_flight_requests.is_none());
657 assert!(config.max_in_flight_requests_per_origin.is_none());
658 assert!(config.idle_timeout.is_none());
659 }
660
661 #[test]
662 fn effective_limits_prefer_new_names() {
663 let config = PoolConfig {
664 max_connections: Some(10),
665 max_in_flight_requests: Some(7),
666 max_connections_per_host: Some(5),
667 max_in_flight_requests_per_origin: Some(3),
668 ..PoolConfig::default()
669 };
670 assert_eq!(config.effective_max_in_flight(), Some(7));
671 assert_eq!(config.effective_max_in_flight_per_origin(), Some(3));
672 }
673
674 #[test]
675 fn effective_limits_fall_back_to_aliases() {
676 let config = PoolConfig {
677 max_connections: Some(10),
678 max_connections_per_host: Some(5),
679 ..PoolConfig::default()
680 };
681 assert_eq!(config.effective_max_in_flight(), Some(10));
682 assert_eq!(config.effective_max_in_flight_per_origin(), Some(5));
683 }
684
685 #[tokio::test]
686 async fn new_names_enforce_logical_concurrency() {
687 let pool = Pool::new(PoolConfig {
690 max_in_flight_requests_per_origin: Some(1),
691 ..PoolConfig::default()
692 });
693 let origin = OriginKey::from_parts("http", "example.com", 80);
694 let _guard = pool.acquire(Some(&origin)).await.unwrap();
695 assert!(
696 tokio::time::timeout(
697 std::time::Duration::from_millis(10),
698 pool.acquire(Some(&origin))
699 )
700 .await
701 .is_err(),
702 "second logical request must wait while first holds its permit"
703 );
704 }
705
706 #[test]
707 fn pool_constructs_with_defaults() {
708 let pool = Pool::new(PoolConfig::default());
709 assert!(pool.inner.global_semaphore.is_none());
710 assert!(pool.inner.per_origin.is_empty());
711 }
712
713 #[test]
714 fn pool_constructs_with_global_limit() {
715 let config = PoolConfig {
716 max_connections: Some(10),
717 ..Default::default()
718 };
719 let pool = Pool::new(config);
720 assert!(pool.inner.global_semaphore.is_some());
721 }
722
723 #[test]
724 fn pool_metrics_starts_zeroed() {
725 let pool = Pool::new(PoolConfig::default());
726 let metrics = pool.metrics();
727 assert_eq!(metrics.acquisition_waits.load(Ordering::Relaxed), 0);
728 assert_eq!(metrics.acquisition_cancellations.load(Ordering::Relaxed), 0);
729 }
730
731 #[tokio::test]
732 async fn acquire_without_limits_returns_immediately() {
733 let pool = Pool::new(PoolConfig::default());
734 let guard = pool.acquire(None).await.unwrap();
735 assert!(guard.origin().is_none());
736 }
737
738 #[tokio::test]
739 async fn acquire_with_global_limit() {
740 let config = PoolConfig {
741 max_connections: Some(2),
742 ..Default::default()
743 };
744 let pool = Pool::new(config);
745
746 let origin = OriginKey::from_parts("http", "example.com", 80);
747 let g1 = pool.acquire(Some(&origin)).await.unwrap();
748 let g2 = pool.acquire(Some(&origin)).await.unwrap();
749
750 assert_eq!(pool.metrics().acquisition_waits.load(Ordering::Relaxed), 0);
752
753 drop(g1);
754 drop(g2);
755 }
756
757 #[tokio::test]
758 async fn acquire_with_per_origin_limit() {
759 let config = PoolConfig {
760 max_connections_per_host: Some(1),
761 ..Default::default()
762 };
763 let pool = Pool::new(config);
764
765 let origin = OriginKey::from_parts("http", "example.com", 80);
766 let g1 = pool.acquire(Some(&origin)).await.unwrap();
767 assert_eq!(pool.metrics().acquisition_waits.load(Ordering::Relaxed), 0);
768
769 drop(g1);
770 }
771
772 #[tokio::test]
773 async fn closed_semaphores_count_cancellation_without_a_wait() {
774 let global_pool = Pool::new(PoolConfig {
775 max_connections: Some(1),
776 ..Default::default()
777 });
778 let global_guard = global_pool.acquire(None).await.unwrap();
779 global_pool.inner.global_semaphore.as_ref().unwrap().close();
780 assert!(global_pool.acquire(None).await.is_err());
781 assert_eq!(
782 global_pool
783 .metrics()
784 .acquisition_waits
785 .load(Ordering::Relaxed),
786 0
787 );
788 assert_eq!(
789 global_pool
790 .metrics()
791 .acquisition_cancellations
792 .load(Ordering::Relaxed),
793 1
794 );
795 drop(global_guard);
796
797 let origin_pool = Pool::new(PoolConfig {
798 max_connections_per_host: Some(1),
799 ..Default::default()
800 });
801 let origin = OriginKey::from_parts("http", "example.com", 80);
802 let origin_guard = origin_pool.acquire(Some(&origin)).await.unwrap();
803 origin_pool
804 .inner
805 .per_origin
806 .get(&origin)
807 .unwrap()
808 .semaphore
809 .close();
810 assert!(origin_pool.acquire(Some(&origin)).await.is_err());
811 assert_eq!(
812 origin_pool
813 .metrics()
814 .acquisition_waits
815 .load(Ordering::Relaxed),
816 0
817 );
818 assert_eq!(
819 origin_pool
820 .metrics()
821 .acquisition_cancellations
822 .load(Ordering::Relaxed),
823 1
824 );
825 drop(origin_guard);
826 }
827
828 #[test]
829 fn pool_is_clone() {
830 let pool = Pool::new(PoolConfig::default());
831 let pool2 = pool.clone();
832 assert!(std::ptr::eq(&*pool.inner, &*pool2.inner));
834 }
835
836 #[tokio::test]
837 async fn acquire_per_origin_limit_separate_origins() {
838 let config = PoolConfig {
839 max_connections_per_host: Some(1),
840 ..Default::default()
841 };
842 let pool = Pool::new(config);
843 let o1 = OriginKey::from_parts("http", "host-a", 80);
844 let o2 = OriginKey::from_parts("http", "host-b", 80);
845 let g1 = pool.acquire(Some(&o1)).await.unwrap();
846 let g2 = pool.acquire(Some(&o2)).await.unwrap();
847 assert!(g1.origin().is_some());
848 assert!(g2.origin().is_some());
849 drop(g1);
850 drop(g2);
851 }
852
853 #[test]
854 fn origin_key_from_url_http() {
855 let url = url::Url::parse("http://example.com:8080/path").unwrap();
856 let key = OriginKey::from_url("http", &url).unwrap();
857 assert_eq!(key.scheme(), "http");
858 assert_eq!(key.host(), "example.com");
859 assert_eq!(key.port(), 8080);
860 }
861
862 #[test]
863 fn origin_key_from_url_https_default_port() {
864 let url = url::Url::parse("https://example.com/path").unwrap();
865 let key = OriginKey::from_url("https", &url).unwrap();
866 assert_eq!(key.scheme(), "https");
867 assert_eq!(key.host(), "example.com");
868 assert_eq!(key.port(), 443);
869 }
870
871 #[test]
872 fn origin_key_from_url_http_default_port() {
873 let url = url::Url::parse("http://example.com/path").unwrap();
874 let key = OriginKey::from_url("http", &url).unwrap();
875 assert_eq!(key.scheme(), "http");
876 assert_eq!(key.port(), 80);
877 }
878
879 #[test]
880 fn origin_keys_distinguish_scheme() {
881 let http = OriginKey::from_parts("http", "example.com", 80);
882 let https = OriginKey::from_parts("https", "example.com", 443);
883 assert_ne!(http, https);
884 }
885
886 #[test]
887 fn origin_keys_distinguish_port() {
888 let p80 = OriginKey::from_parts("http", "example.com", 80);
889 let p8080 = OriginKey::from_parts("http", "example.com", 8080);
890 assert_ne!(p80, p8080);
891 }
892
893 #[test]
894 fn origin_keys_equal_for_same_origin() {
895 let a = OriginKey::from_parts("http", "example.com", 80);
896 let b = OriginKey::from_parts("http", "example.com", 80);
897 assert_eq!(a, b);
898 }
899
900 #[test]
901 fn origin_key_display() {
902 let key = OriginKey::from_parts("https", "example.com", 443);
903 assert_eq!(key.to_string(), "https://example.com:443");
904 }
905
906 #[tokio::test]
907 async fn origin_keys_separate_per_origin_map_entries() {
908 let config = PoolConfig {
909 max_connections_per_host: Some(1),
910 ..Default::default()
911 };
912 let pool = Pool::new(config);
913 let a = OriginKey::from_parts("http", "example.com", 80);
914 let b = OriginKey::from_parts("http", "example.com", 8080);
915 assert!(pool.acquire(Some(&a)).await.unwrap().origin().is_some());
916 assert!(pool.acquire(Some(&b)).await.unwrap().origin().is_some());
917 }
918
919 #[test]
920 fn origin_keys_distinct_by_scheme() {
921 let http = OriginKey::from_parts("http", "example.com", 443);
923 let https = OriginKey::from_parts("https", "example.com", 443);
924 assert_ne!(http, https);
925 }
926
927 #[test]
928 fn origin_key_from_url_uses_scheme_default_port() {
929 let explicit = url::Url::parse("http://example.com:8080/path").unwrap();
931 let default = url::Url::parse("http://example.com/path").unwrap();
932 let k_explicit = OriginKey::from_url("http", &explicit).unwrap();
933 let k_default = OriginKey::from_url("http", &default).unwrap();
934 assert_eq!(k_explicit.port(), 8080);
935 assert_eq!(k_default.port(), 80);
936 }
937
938 #[test]
939 fn origin_key_direct_has_no_proxy() {
940 let key = OriginKey::from_parts("http", "example.com", 80);
941 assert!(key.proxy_host().is_none());
942 assert!(key.proxy_port().is_none());
943 assert!(!key.is_tunnel());
944 }
945
946 #[cfg(feature = "proxy")]
947 #[test]
948 fn origin_key_proxy_a_not_equal_proxy_b() {
949 let url = url::Url::parse("http://example.com/path").unwrap();
950 let a = OriginKey::from_url_with_proxy_scheme(
951 "http",
952 &url,
953 Some("proxy-a"),
954 Some(8080),
955 None,
956 false,
957 )
958 .unwrap();
959 let b = OriginKey::from_url_with_proxy_scheme(
960 "http",
961 &url,
962 Some("proxy-b"),
963 Some(8080),
964 None,
965 false,
966 )
967 .unwrap();
968 assert_ne!(a, b);
969 }
970
971 #[cfg(feature = "proxy")]
972 #[test]
973 fn origin_key_tunnel_not_equal_non_tunnel() {
974 let url = url::Url::parse("http://example.com/path").unwrap();
975 let tunnel = OriginKey::from_url_with_proxy_scheme(
976 "http",
977 &url,
978 Some("proxy"),
979 Some(8080),
980 None,
981 true,
982 )
983 .unwrap();
984 let direct = OriginKey::from_url_with_proxy_scheme(
985 "http",
986 &url,
987 Some("proxy"),
988 Some(8080),
989 None,
990 false,
991 )
992 .unwrap();
993 assert_ne!(tunnel, direct);
994 }
995
996 #[cfg(feature = "proxy")]
997 #[test]
998 fn origin_key_proxy_same_route_equal() {
999 let url = url::Url::parse("http://example.com/path").unwrap();
1000 let a = OriginKey::from_url_with_proxy_scheme(
1001 "http",
1002 &url,
1003 Some("proxy"),
1004 Some(8080),
1005 None,
1006 false,
1007 )
1008 .unwrap();
1009 let b = OriginKey::from_url_with_proxy_scheme(
1010 "http",
1011 &url,
1012 Some("proxy"),
1013 Some(8080),
1014 None,
1015 false,
1016 )
1017 .unwrap();
1018 assert_eq!(a, b);
1019 }
1020
1021 #[cfg(feature = "proxy")]
1022 #[test]
1023 fn origin_key_parts_preserves_proxy_scheme() {
1024 let http = OriginKey::from_parts_with_proxy(
1025 "https",
1026 "example.com",
1027 443,
1028 Some("proxy"),
1029 Some(8080),
1030 Some("http"),
1031 true,
1032 );
1033 let https = OriginKey::from_parts_with_proxy(
1034 "https",
1035 "example.com",
1036 443,
1037 Some("proxy"),
1038 Some(8080),
1039 Some("https"),
1040 true,
1041 );
1042 assert_ne!(http, https);
1043 assert_eq!(
1044 http.to_string(),
1045 "https://example.com:443 via http://proxy:8080 tunnel"
1046 );
1047 }
1048
1049 #[cfg(feature = "proxy")]
1050 #[test]
1051 fn origin_key_display_with_proxy() {
1052 let url = url::Url::parse("https://example.com/path").unwrap();
1053 let key = OriginKey::from_url_with_proxy_scheme(
1054 "https",
1055 &url,
1056 Some("proxy"),
1057 Some(8080),
1058 None,
1059 true,
1060 )
1061 .unwrap();
1062 assert_eq!(
1063 key.to_string(),
1064 "https://example.com:443 via proxy:8080 tunnel"
1065 );
1066 }
1067
1068 #[cfg(feature = "proxy")]
1069 #[test]
1070 fn origin_key_display_without_tunnel() {
1071 let url = url::Url::parse("http://example.com/path").unwrap();
1072 let key = OriginKey::from_url_with_proxy_scheme(
1073 "http",
1074 &url,
1075 Some("proxy"),
1076 Some(8080),
1077 None,
1078 false,
1079 )
1080 .unwrap();
1081 assert_eq!(key.to_string(), "http://example.com:80 via proxy:8080");
1082 }
1083
1084 #[cfg(feature = "proxy")]
1085 #[tokio::test]
1086 async fn proxy_route_separate_from_direct() {
1087 let config = PoolConfig {
1088 max_connections_per_host: Some(1),
1089 ..Default::default()
1090 };
1091 let pool = Pool::new(config);
1092 let direct = OriginKey::from_parts("http", "example.com", 80);
1093 let proxied = OriginKey::from_parts_with_proxy(
1094 "http",
1095 "example.com",
1096 80,
1097 Some("proxy"),
1098 Some(8080),
1099 None,
1100 false,
1101 );
1102 let g1 = pool.acquire(Some(&direct)).await.unwrap();
1103 let g2 = pool.acquire(Some(&proxied)).await.unwrap();
1104 assert!(g1.origin().is_some());
1105 assert!(g2.origin().is_some());
1106 }
1107
1108 #[tokio::test]
1109 async fn global_wait_does_not_hold_per_origin_slot() {
1110 let pool = Pool::new(PoolConfig {
1111 max_connections: Some(1),
1112 max_connections_per_host: Some(1),
1113 ..Default::default()
1114 });
1115 let first_origin = OriginKey::from_parts("http", "first.example", 80);
1116 let waiting_origin = OriginKey::from_parts("http", "waiting.example", 80);
1117 let first = pool.acquire(Some(&first_origin)).await.unwrap();
1118
1119 let pool_for_task = pool.clone();
1120 let waiting_origin_for_task = waiting_origin.clone();
1121 let waiter =
1122 tokio::spawn(
1123 async move { pool_for_task.acquire(Some(&waiting_origin_for_task)).await },
1124 );
1125 for _ in 0..3 {
1126 tokio::task::yield_now().await;
1127 }
1128
1129 assert!(pool.inner.per_origin.get(&waiting_origin).is_none());
1130 drop(first);
1131 drop(waiter.await.unwrap().unwrap());
1132 }
1133
1134 #[tokio::test]
1135 async fn idle_per_origin_entries_are_swept_on_acquire() {
1136 let pool = Pool::new(PoolConfig {
1137 max_connections_per_host: Some(1),
1138 ..Default::default()
1139 });
1140
1141 for index in 0..8 {
1142 let origin = OriginKey::from_parts("http", &format!("host-{index}.example"), 80);
1143 drop(pool.acquire(Some(&origin)).await.unwrap());
1144 }
1145
1146 assert!(pool.inner.per_origin.len() <= 8);
1153 if std::env::var("EGGFETCH_STRICT_POOL_EVICTION").is_ok() {
1157 assert!(pool.inner.per_origin.len() <= 1);
1158 }
1159 }
1160
1161 #[cfg(feature = "proxy")]
1162 #[tokio::test]
1163 async fn tunnel_separate_from_non_tunnel() {
1164 let config = PoolConfig {
1165 max_connections_per_host: Some(1),
1166 ..Default::default()
1167 };
1168 let pool = Pool::new(config);
1169 let http_fwd = OriginKey::from_parts_with_proxy(
1170 "http",
1171 "example.com",
1172 80,
1173 Some("proxy"),
1174 Some(8080),
1175 None,
1176 false,
1177 );
1178 let https_tunnel = OriginKey::from_parts_with_proxy(
1179 "http",
1180 "example.com",
1181 80,
1182 Some("proxy"),
1183 Some(8080),
1184 None,
1185 true,
1186 );
1187 let g1 = pool.acquire(Some(&http_fwd)).await.unwrap();
1188 let g2 = pool.acquire(Some(&https_tunnel)).await.unwrap();
1189 assert!(g1.origin().is_some());
1190 assert!(g2.origin().is_some());
1191 }
1192
1193 #[cfg(feature = "proxy")]
1194 #[tokio::test]
1195 async fn different_proxies_separate_semaphores() {
1196 let config = PoolConfig {
1197 max_connections_per_host: Some(1),
1198 ..Default::default()
1199 };
1200 let pool = Pool::new(config);
1201 let a = OriginKey::from_parts_with_proxy(
1202 "http",
1203 "example.com",
1204 80,
1205 Some("proxy-a"),
1206 Some(8080),
1207 None,
1208 false,
1209 );
1210 let b = OriginKey::from_parts_with_proxy(
1211 "http",
1212 "example.com",
1213 80,
1214 Some("proxy-b"),
1215 Some(8080),
1216 None,
1217 false,
1218 );
1219 let g1 = pool.acquire(Some(&a)).await.unwrap();
1220 let g2 = pool.acquire(Some(&b)).await.unwrap();
1221 assert!(g1.origin().is_some());
1222 assert!(g2.origin().is_some());
1223 }
1224}