1use crate::error::IntoFailure;
2use crate::http::error::{DispatchError, ResponseError};
3use crate::http::{Request, Response, config::DispatcherConfig};
4use crate::io::{Filter, Io, types};
5use crate::service::pipeline::{Pipeline, PipelineFactory};
6use crate::service::{Ctx, IntoServiceFactory, RequestState, Service, ServiceFactory};
7
8use super::control::{Control, ControlAck, ControlResult};
9use super::dispatcher::Dispatcher;
10
11#[derive(derive_more::Debug)]
21#[debug("H1Service")]
22pub struct H1Service<F, Req: RequestState<Io<F>>, Err> {
23 sf: crate::http::HttpPipeline<Req::State, Err>,
24 ctl: Option<crate::http::Ctl1Pipeline<Req::State, F, Err>>,
26 config: DispatcherConfig,
27}
28
29impl<F, Req, Err> H1Service<F, Req, Err>
30where
31 F: Filter,
32 Req: RequestState<Io<F>>,
33 Req::State: Clone,
34 Err: ResponseError + 'static,
35{
36 pub(crate) fn new<Sf>(sf: impl IntoServiceFactory<Sf, Req::State, Request>) -> Self
38 where
39 Sf: ServiceFactory<Req::State, Request, Error = Err> + 'static,
40 Sf::Res: Into<Response>,
41 Sf::InitError: IntoFailure,
42 {
43 H1Service {
44 sf: PipelineFactory::new(
45 sf.into_factory()
46 .map(Into::into)
47 .map_init_err(|e| DispatchError::Control(e.fail())),
48 ),
49 ctl: None,
50 config: DispatcherConfig::default(),
51 }
52 }
53}
54
55impl<F, Req, Err> H1Service<F, Req, Err>
56where
57 F: Filter,
58 Req: RequestState<Io<F>>,
59 Req::State: Clone,
60 Err: ResponseError + 'static,
61{
62 #[must_use]
63 pub fn control<I, Sf>(self, ctl: I) -> Self
70 where
71 I: IntoServiceFactory<Sf, Req::State, Control<F, Err>>,
72 Sf: ServiceFactory<Req::State, Control<F, Err>, Res = ControlAck<F>> + 'static,
73 Sf::Error: IntoFailure,
74 Sf::InitError: IntoFailure,
75 {
76 H1Service {
77 sf: self.sf,
78 ctl: Some(PipelineFactory::new(
79 ctl.into_factory()
80 .map_err(|e| DispatchError::Service(e.fail()))
81 .map_init_err(|e| DispatchError::Control(e.fail())),
82 )),
83 config: self.config,
84 }
85 }
86}
87
88impl<St, F, Req, Err> Service<St, Req> for H1Service<F, Req, Err>
89where
90 F: Filter,
91 Req: RequestState<Io<F>>,
92 Req::State: Clone,
93 Err: ResponseError + 'static,
94{
95 type Res = ();
96 type Error = DispatchError;
97
98 async fn call(&self, req: Req, _: Ctx<'_, Self, St>) -> Result<(), Self::Error> {
99 let (st, io) = req.unpack();
100
101 let svc = self.sf.create(st.clone()).await?;
102 let ctl = match &self.ctl {
103 Some(ctl) => Some(ctl.create(st).await?),
104 None => None,
105 };
106
107 let id = self.config.next_id();
108 let ioref = io.get_ref();
109 let (_guard, inflight) = self.config.insert_io(&ioref);
110
111 log::trace!(
112 "{}: New http1 connection {id}, peer address {:?}, inflight: {}",
113 io.tag(),
114 io.query::<types::PeerAddr>().get(),
115 inflight
116 );
117
118 handle_io(id, io, svc, ctl, self.config.clone()).await
119 }
120
121 async fn ready(&self, _: Ctx<'_, Self, St>) -> Result<(), Self::Error> {
122 Ok(())
123 }
124
125 async fn shutdown(&self, _: crate::Ctx<'_, Self, St>) {
126 let inflight = self.config.shutdown();
128 if inflight != 0 {
129 log::trace!("Shutting down service, in-flight connections: {inflight}");
130
131 self.config.wait_shutdown().await;
132 log::trace!("Shutting down is complected");
133 }
134 }
135}
136
137pub(crate) async fn handle_io<F, Err>(
138 id: usize,
139 io: Io<F>,
140 svc: Pipeline<Request, Response, Err>,
141 ctl: Option<Pipeline<Control<F, Err>, ControlAck<F>, DispatchError>>,
142 config: DispatcherConfig,
143) -> Result<(), DispatchError>
144where
145 F: Filter,
146 Err: ResponseError + 'static,
147{
148 let io = if let Some(ctl) = &ctl {
150 match ctl.call(Control::connect(id, io)).await {
151 Ok(ack) => {
152 let ControlResult::Connect(io) = ack.result else {
153 unreachable!();
154 };
155 io
156 }
157 Err(err) => {
158 svc.shutdown().await;
159 ctl.shutdown().await;
160 return Err(err);
161 }
162 }
163 } else {
164 io
165 };
166
167 let mut disp = std::pin::pin!(Dispatcher::new(id, io, svc, ctl, config));
169 let result = disp.as_mut().await;
170 disp.as_ref().get_ref().shutdown_services().await;
171 result
172}