Skip to main content

eggfetch_core/
service.rs

1//! Service adapters for native transport-oriented consumers.
2
3use std::future::Future;
4use std::pin::Pin;
5use std::task::{Context, Poll};
6
7use bytes::Bytes;
8
9use crate::{Client, Error, NativeRequestOptions, NativeResponseBody};
10
11/// A Tower service adapter for eggfetch's native frame-preserving HTTP path.
12///
13/// This service delegates every request to [`Client::execute_http_body`]. It
14/// is always ready to accept a request; origin-aware logical pool admission,
15/// physical connection admission, and transport backpressure happen in the
16/// future returned by [`tower_service::Service::call`]. Consequently,
17/// Tower's readiness-based load-shedding layers do not observe eggfetch pool
18/// saturation. Wrap this service in caller-owned Tower layers when a separate
19/// global concurrency or load-shed policy is needed.
20///
21/// The native path does not apply eggfetch's high-level redirects, logical
22/// retries, cookies, authentication, decompression, or decoded-body limits.
23/// Request extensions are not guaranteed to be forwarded to Hyper; middleware
24/// may use them before the request reaches this service, but they are not an
25/// eggfetch transport contract.
26#[derive(Clone, Debug)]
27pub struct NativeHttpService {
28    client: Client,
29    options: NativeRequestOptions,
30}
31
32impl NativeHttpService {
33    /// Create a service using default native transport options.
34    #[must_use]
35    pub fn new(client: Client) -> Self {
36        Self {
37            client,
38            options: NativeRequestOptions::default(),
39        }
40    }
41
42    /// Configure the native transport options used by every service request.
43    #[must_use]
44    pub fn with_options(mut self, options: NativeRequestOptions) -> Self {
45        self.options = options;
46        self
47    }
48}
49
50impl Client {
51    /// Create a Tower service for the native frame-preserving HTTP path.
52    ///
53    /// The returned service is configured with [`NativeRequestOptions::default`].
54    /// Use [`NativeHttpService::with_options`] to configure service-instance
55    /// defaults, or call [`Client::execute_http_body`] directly for options
56    /// that vary by request.
57    #[must_use]
58    pub fn native_service(&self) -> NativeHttpService {
59        NativeHttpService::new(self.clone())
60    }
61}
62
63impl<B> tower_service::Service<http::Request<B>> for NativeHttpService
64where
65    B: http_body::Body<Data = Bytes> + Send + 'static,
66    B::Error: std::error::Error + Send + Sync + 'static,
67{
68    type Response = http::Response<NativeResponseBody>;
69    type Error = Error;
70    type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;
71
72    /// Eggfetch admission is origin-aware and requires the request, so it is
73    /// performed by the future returned from [`tower_service::Service::call`].
74    fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
75        Poll::Ready(Ok(()))
76    }
77
78    /// Execute one native request through the existing client transport path.
79    fn call(&mut self, request: http::Request<B>) -> Self::Future {
80        let client = self.client.clone();
81        let options = self.options.clone();
82        Box::pin(async move { client.execute_http_body(request, options).await })
83    }
84}