1mod engine;
4#[cfg(feature = "reactor")]
5mod reactor;
6mod source;
7
8use std::any::TypeId;
9use std::fmt;
10use std::net::{SocketAddr, ToSocketAddrs};
11use std::path::PathBuf;
12use std::sync::Arc;
13use std::thread::JoinHandle;
14use std::time::{Duration, Instant};
15
16use crate::control::ControlLine;
17use crate::driver::DriverKind;
18use crate::error::{CloseReason, ConfigReason, KillReason, NetError, RefusalCause};
19use crate::events::{EventSink, NoopSink};
20use crate::gate::Gate;
21use crate::handoff::ListenerHandle;
22use crate::id::{ConnectionId, ListenerId, WorkerId};
23use crate::limits::Limits;
24use crate::opts::{ListenOptions, LocalOptions};
25use crate::proxy::ProxyMode;
26use crate::session::SessionFactory;
27use crate::tcp::{ListenerInfo, TcpConnection};
28use crate::time::Clock;
29use crate::tls::ErasedAcceptor;
30use crate::tls::{NoTls, TlsAcceptor};
31use crate::trusted::TrustedProxies;
32
33pub(crate) use engine::Shared;
34
35#[derive(Clone, Copy, Debug, PartialEq, Eq)]
41pub enum DoorState {
42 Open,
44 Closed,
46}
47
48#[derive(Clone, Copy, Debug, PartialEq, Eq)]
55pub struct DrainReport {
56 pub graceful: u32,
58 pub killed: u32,
60 pub took: Duration,
62}
63
64#[non_exhaustive]
73#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
74pub struct Stats {
75 pub open: u64,
77 pub accepted: u64,
79 pub refused: u64,
81 pub closed: u64,
83 pub bytes_in: u64,
85 pub bytes_out: u64,
87 pub egress_denied: u64,
89 pub killed: u64,
91 pub slow_peers: u64,
93 pub timeouts: u64,
95 pub descriptor_pressure: u64,
97 pub stale_completions: u64,
99 pub session_panics: u64,
101 refused_by: [u64; RefusalCause::COUNT],
102 closed_by: [u64; CloseReason::COUNT],
103}
104
105impl Stats {
106 #[must_use]
108 pub fn refused_for(&self, cause: RefusalCause) -> u64 {
109 self.refused_by[cause.index()]
110 }
111
112 #[must_use]
114 pub fn closed_for(&self, reason: CloseReason) -> u64 {
115 self.closed_by[reason.index()]
116 }
117
118 pub fn closes(&self) -> impl Iterator<Item = (CloseReason, u64)> + '_ {
129 CloseReason::BY_INDEX
130 .iter()
131 .zip(self.closed_by.iter())
132 .filter(|(_, n)| **n > 0)
133 .map(|(r, n)| (*r, *n))
134 }
135
136 pub fn refusals(&self) -> impl Iterator<Item = (RefusalCause, u64)> + '_ {
138 RefusalCause::BY_INDEX
139 .iter()
140 .zip(self.refused_by.iter())
141 .filter(|(_, n)| **n > 0)
142 .map(|(r, n)| (*r, *n))
143 }
144}
145
146#[must_use]
157#[derive(Debug)]
158pub struct ListenerSpec {
159 pub(crate) addrs: Result<Vec<SocketAddr>, String>,
160 pub(crate) options: Option<ListenOptions>,
161 pub(crate) name: Option<String>,
162 pub(crate) limits: Limits,
163 pub(crate) proxy: ProxyMode,
164 pub(crate) trusted: Option<Arc<TrustedProxies>>,
165 pub(crate) tls: bool,
166 pub(crate) unix: Option<(PathBuf, LocalOptions)>,
167 #[cfg(all(windows, feature = "local"))]
168 pub(crate) pipe: Option<(String, crate::pipe::PipeOptions)>,
169 #[cfg(feature = "udp")]
170 pub(crate) udp: Option<crate::udp::UdpOptions>,
171 pub(crate) adopt: Option<ListenerHandle>,
172 pub(crate) lockdown_allowed: bool,
173}
174
175impl ListenerSpec {
176 fn check_listener_limits(&self) -> Result<(), NetError> {
180 if self.limits.max_connections().is_some() {
181 return Err(NetError::InvalidConfig {
182 field: "max_connections",
183 reason: ConfigReason::Inconsistent,
184 });
185 }
186 Ok(())
187 }
188
189 pub fn tcp<A: ToSocketAddrs>(addr: A) -> ListenerSpec {
191 ListenerSpec {
192 addrs: addr
193 .to_socket_addrs()
194 .map(|i| i.collect())
195 .map_err(|e| e.to_string()),
196 options: None,
197 name: None,
198 limits: Limits::default(),
199 proxy: ProxyMode::Off,
200 trusted: None,
201 tls: false,
202 unix: None,
203 #[cfg(all(windows, feature = "local"))]
204 pipe: None,
205 #[cfg(feature = "udp")]
206 udp: None,
207 adopt: None,
208 lockdown_allowed: false,
209 }
210 }
211
212 pub fn lockdown_allowed(mut self, on: bool) -> ListenerSpec {
215 self.lockdown_allowed = on;
216 self
217 }
218
219 pub fn tcp_with<A: ToSocketAddrs>(addr: A, options: ListenOptions) -> ListenerSpec {
221 ListenerSpec {
222 options: Some(options),
223 ..ListenerSpec::tcp(addr)
224 }
225 }
226
227 #[cfg(feature = "udp")]
238 pub fn udp<A: ToSocketAddrs>(addr: A, options: crate::udp::UdpOptions) -> ListenerSpec {
239 ListenerSpec {
240 udp: Some(options),
241 ..ListenerSpec::tcp(addr)
242 }
243 }
244
245 #[cfg(all(windows, feature = "local"))]
256 pub fn pipe(name: &str, options: crate::pipe::PipeOptions) -> ListenerSpec {
257 ListenerSpec {
258 pipe: Some((name.to_owned(), options)),
259 ..ListenerSpec::tcp(std::net::SocketAddr::from(([127, 0, 0, 1], 0)))
260 }
261 }
262
263 pub fn unix(path: impl Into<PathBuf>, options: LocalOptions) -> ListenerSpec {
278 ListenerSpec {
279 addrs: Ok(Vec::new()),
280 options: None,
281 name: None,
282 limits: Limits::default(),
283 proxy: ProxyMode::Off,
284 trusted: None,
285 tls: false,
286 unix: Some((path.into(), options)),
287 lockdown_allowed: false,
288 #[cfg(all(windows, feature = "local"))]
289 pipe: None,
290 #[cfg(feature = "udp")]
291 udp: None,
292 adopt: None,
293 }
294 }
295
296 pub fn adopt(handle: ListenerHandle) -> ListenerSpec {
312 ListenerSpec {
313 addrs: Ok(Vec::new()),
314 options: None,
315 name: None,
316 limits: Limits::default(),
317 proxy: ProxyMode::Off,
318 trusted: None,
319 tls: false,
320 unix: None,
321 #[cfg(all(windows, feature = "local"))]
322 pipe: None,
323 #[cfg(feature = "udp")]
324 udp: None,
325 adopt: Some(handle),
326 lockdown_allowed: false,
327 }
328 }
329
330 pub fn name(mut self, name: &str) -> ListenerSpec {
332 self.name = Some(name.to_owned());
333 self
334 }
335
336 pub fn limits(mut self, limits: Limits) -> ListenerSpec {
338 self.limits = limits;
339 self
340 }
341
342 pub fn proxy(mut self, mode: ProxyMode) -> ListenerSpec {
351 self.proxy = mode;
352 self
353 }
354
355 pub fn tls(mut self, on: bool) -> ListenerSpec {
358 self.tls = on;
359 self
360 }
361
362 pub fn trusted_proxies(mut self, proxies: TrustedProxies) -> ListenerSpec {
364 self.trusted = Some(Arc::new(proxies));
365 self
366 }
367}
368
369#[derive(Clone, Copy, Debug, Default)]
371pub struct NoWork;
372
373#[cfg(feature = "udp")]
376#[derive(Debug)]
377pub struct UdpOnly;
378
379#[derive(Clone, Copy, Debug, Default)]
381pub struct NoGate;
382
383#[non_exhaustive]
390#[derive(Clone, Debug, Default, PartialEq, Eq)]
391pub enum Placement {
392 #[default]
394 Off,
395 PinPerCore,
397 Explicit(Vec<usize>),
399}
400
401#[derive(Debug)]
403pub struct Sessions<F> {
404 pub(crate) factory: F,
405}
406
407#[cfg(feature = "reactor")]
408impl<F> Sessions<F> {
409 pub(crate) fn factory(&self) -> &F {
410 &self.factory
411 }
412}
413
414pub struct BlockingHandler<H> {
416 pub(crate) handler: H,
417}
418
419impl<H> fmt::Debug for BlockingHandler<H> {
420 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
421 f.write_str("BlockingHandler")
422 }
423}
424
425pub(crate) mod sealed {
426 pub trait Sealed {}
427}
428
429pub trait Workload: sealed::Sealed + engine::Serve + Send + Sync + 'static {}
431
432#[cfg(feature = "udp")]
433impl sealed::Sealed for UdpOnly {}
434#[cfg(feature = "udp")]
435impl Workload for UdpOnly {}
436
437impl<F: SessionFactory> sealed::Sealed for Sessions<F> {}
438impl<F: SessionFactory> Workload for Sessions<F> {}
439
440impl<H> sealed::Sealed for BlockingHandler<H> where
441 H: Fn(&mut TcpConnection) -> Result<(), NetError> + Send + Sync + 'static
442{
443}
444impl<H> Workload for BlockingHandler<H> where
445 H: Fn(&mut TcpConnection) -> Result<(), NetError> + Send + Sync + 'static
446{
447}
448
449#[must_use]
474pub struct ServerBuilder<W = NoWork, G = NoGate, S = NoopSink, T = NoTls> {
475 listeners: Vec<ListenerSpec>,
476 driver: DriverKind,
477 workers: Option<u16>,
478 limits: Limits,
479 work: W,
480 gate: G,
481 sink: S,
482 tls: T,
483 control: Option<Arc<dyn ControlLine>>,
484 wipe: bool,
485 clock: Option<Arc<dyn Clock>>,
486 trusted: Option<Arc<TrustedProxies>>,
487 placement: Placement,
488 #[cfg(feature = "udp")]
489 udp: Option<Arc<dyn crate::udp::UdpSessionFactory>>,
490}
491
492impl ServerBuilder {
493 pub fn new() -> ServerBuilder {
495 ServerBuilder {
496 listeners: Vec::new(),
497 driver: DriverKind::Auto,
498 workers: None,
499 limits: Limits::default(),
500 work: NoWork,
501 gate: NoGate,
502 sink: NoopSink,
503 tls: NoTls,
504 control: None,
505 wipe: true,
506 clock: None,
507 trusted: None,
508 placement: Placement::Off,
509 #[cfg(feature = "udp")]
510 udp: None,
511 }
512 }
513}
514
515impl Default for ServerBuilder {
516 fn default() -> ServerBuilder {
517 ServerBuilder::new()
518 }
519}
520
521impl<W, G, S, T> ServerBuilder<W, G, S, T> {
522 fn map<W2, G2, S2, T2>(
523 self,
524 f: impl FnOnce(W, G, S, T) -> (W2, G2, S2, T2),
525 ) -> ServerBuilder<W2, G2, S2, T2> {
526 let ServerBuilder {
527 listeners,
528 driver,
529 workers,
530 limits,
531 work,
532 gate,
533 sink,
534 tls,
535 control,
536 wipe,
537 clock,
538 trusted,
539 placement,
540 #[cfg(feature = "udp")]
541 udp,
542 } = self;
543 let (work, gate, sink, tls) = f(work, gate, sink, tls);
544 ServerBuilder {
545 listeners,
546 driver,
547 workers,
548 limits,
549 work,
550 gate,
551 sink,
552 tls,
553 control,
554 wipe,
555 clock,
556 trusted,
557 placement,
558 #[cfg(feature = "udp")]
559 udp,
560 }
561 }
562
563 pub fn trusted_proxies(mut self, proxies: TrustedProxies) -> Self {
566 self.trusted = Some(Arc::new(proxies));
567 self
568 }
569
570 pub fn listener(mut self, spec: ListenerSpec) -> Self {
572 self.listeners.push(spec);
573 self
574 }
575
576 pub fn driver(mut self, kind: DriverKind) -> Self {
578 self.driver = kind;
579 self
580 }
581
582 pub fn placement(mut self, placement: Placement) -> Self {
585 self.placement = placement;
586 self
587 }
588
589 pub fn workers(mut self, n: u16) -> Self {
591 self.workers = Some(n);
592 self
593 }
594
595 pub fn limits(mut self, limits: Limits) -> Self {
597 self.limits = limits;
598 self
599 }
600
601 pub fn sessions<F: SessionFactory>(self, factory: F) -> ServerBuilder<Sessions<F>, G, S, T> {
603 self.map(|_, g, s, t| (Sessions { factory }, g, s, t))
604 }
605
606 pub fn handler<H>(self, handler: H) -> ServerBuilder<BlockingHandler<H>, G, S, T>
608 where
609 H: Fn(&mut TcpConnection) -> Result<(), NetError> + Send + Sync + 'static,
610 {
611 self.map(|_, g, s, t| (BlockingHandler { handler }, g, s, t))
612 }
613
614 pub fn tls<T2: TlsAcceptor>(self, acceptor: T2) -> ServerBuilder<W, G, S, T2> {
616 self.map(|w, g, s, _| (w, g, s, acceptor))
617 }
618
619 pub fn gate<G2: Gate>(self, gate: G2) -> ServerBuilder<W, G2, S, T> {
621 self.map(|w, _, s, t| (w, gate, s, t))
622 }
623
624 pub fn events<S2: EventSink>(self, sink: S2) -> ServerBuilder<W, G, S2, T> {
626 self.map(|w, g, _, t| (w, g, sink, t))
627 }
628
629 pub fn control<C: ControlLine>(mut self, line: Arc<C>) -> Self {
631 self.control = Some(line);
632 self
633 }
634
635 pub fn wipe_buffers(mut self, on: bool) -> Self {
637 self.wipe = on;
638 self
639 }
640
641 pub fn clock<K: Clock>(mut self, clock: Arc<K>) -> Self {
643 self.clock = Some(clock);
644 self
645 }
646}
647
648#[cfg(feature = "udp")]
649impl<G, S, T> ServerBuilder<NoWork, G, S, T> {
650 pub fn udp_sessions<F>(self, factory: F) -> ServerBuilder<UdpOnly, G, S, T>
671 where
672 F: crate::udp::UdpSessionFactory,
673 {
674 let mut b = self.map(|_, g, s, t| (UdpOnly, g, s, t));
675 b.udp = Some(Arc::new(factory));
676 b.workers.get_or_insert(1);
677 b
678 }
679}
680
681#[cfg(feature = "udp")]
682impl<W: Workload, G, S, T> ServerBuilder<W, G, S, T> {
683 pub fn udp_sessions<F>(mut self, factory: F) -> Self
685 where
686 F: crate::udp::UdpSessionFactory,
687 {
688 self.udp = Some(Arc::new(factory));
689 self
690 }
691}
692
693impl<W, G, S, T> ServerBuilder<W, G, S, T>
694where
695 W: Workload,
696 G: Gate,
697 S: EventSink,
698 T: TlsAcceptor,
699{
700 pub fn build(self) -> Result<Server, NetError> {
708 let requested = match self.driver {
710 DriverKind::Auto if <W as engine::Serve>::BLOCKING_ONLY => DriverKind::Blocking,
711 other => other,
712 };
713 let driver = match requested.resolve() {
714 DriverKind::Reactor if <W as engine::Serve>::BLOCKING_ONLY => {
715 return Err(invalid("driver", ConfigReason::Inconsistent));
716 }
717 DriverKind::Reactor if !DriverKind::reactor_built() => {
718 return Err(NetError::Unsupported {
719 feature: "the reactor driver",
720 platform: std::env::consts::OS,
721 });
722 }
723 DriverKind::Uring => {
724 return Err(NetError::Unsupported {
725 feature: "the io_uring driver (after 1.0.0)",
726 platform: std::env::consts::OS,
727 });
728 }
729 kind => kind,
730 };
731 if self.listeners.is_empty() {
732 return Err(invalid("listener", ConfigReason::Missing));
733 }
734 if self.listeners.len() > 64 {
735 return Err(invalid("listener", ConfigReason::TooLarge { max: 64 }));
736 }
737 let workers = self.workers.unwrap_or_else(|| {
738 if driver == DriverKind::Reactor {
739 std::thread::available_parallelism()
740 .map_or(4, |n| u16::try_from(n.get()).unwrap_or(4096).min(4096))
741 } else {
742 128
743 }
744 });
745 if workers == 0 {
746 return Err(invalid("workers", ConfigReason::Zero));
747 }
748 if workers > 4096 {
749 return Err(invalid("workers", ConfigReason::TooLarge { max: 4096 }));
750 }
751 let mut listeners = self.listeners;
752 let has_tls = TypeId::of::<T>() != TypeId::of::<NoTls>();
753 let mut names = Vec::new();
754 #[cfg(feature = "udp")]
755 for spec in &listeners {
756 if (spec.udp.is_some() && self.udp.is_none())
757 || (spec.udp.is_none() && <W as engine::Serve>::UDP_ONLY)
758 {
759 return Err(invalid("udp_sessions", ConfigReason::Inconsistent));
760 }
761 if spec.udp.is_some() && (spec.tls || spec.proxy != ProxyMode::Off) {
762 return Err(invalid("udp", ConfigReason::Inconsistent));
763 }
764 }
765 for spec in &mut listeners {
766 check_spec(spec, self.trusted.as_ref(), has_tls, &names)?;
767 if let Some(n) = &spec.name {
768 names.push(n.clone());
769 }
770 }
771 let limits = if self.limits.max_connections().is_none() {
774 let cap = if driver == DriverKind::Reactor {
775 reactor_default_cap(workers)
776 } else {
777 u32::from(workers)
778 };
779 self.limits.to_builder().max_connections(cap).build()?
780 } else {
781 self.limits.clone()
782 };
783 limits.validate(workers, crate::sys::descriptor_limit())?;
784 for spec in &listeners {
785 spec.check_listener_limits()?;
786 limits.overlay(&spec.limits).validate(workers, None)?;
787 }
788 if self.placement != Placement::Off && !crate::sys::CAN_PIN {
789 return Err(NetError::Unsupported {
790 feature: "worker placement",
791 platform: std::env::consts::OS,
792 });
793 }
794 if self.placement == Placement::Explicit(Vec::new()) {
795 return Err(invalid("placement", ConfigReason::Zero));
796 }
797 let tls: Option<Arc<dyn ErasedAcceptor>> = if has_tls {
798 Some(Arc::new(self.tls))
799 } else {
800 None
801 };
802 engine::start(engine::Config {
803 tls,
804 trusted: self.trusted,
805 listeners,
806 driver,
807 workers,
808 limits,
809 work: self.work,
810 gate: self.gate,
811 sink: self.sink,
812 control: self.control,
813 wipe: self.wipe,
814 clock: self.clock,
815 placement: self.placement,
816 #[cfg(feature = "udp")]
817 udp: self.udp,
818 })
819 }
820}
821
822fn reactor_default_cap(workers: u16) -> u32 {
825 let wanted = 10_000u32;
826 match crate::sys::descriptor_limit() {
827 Some(have) => {
828 let spare = u64::from(workers).saturating_mul(2) + 64;
829 let room = have.saturating_sub(spare).clamp(16, u64::from(wanted));
830 u32::try_from(room).unwrap_or(wanted)
831 }
832 None => wanted,
833 }
834}
835
836fn invalid(field: &'static str, reason: ConfigReason) -> NetError {
837 NetError::InvalidConfig { field, reason }
838}
839
840#[must_use]
848#[derive(Clone, Debug, PartialEq, Eq)]
849pub struct AdoptSpec {
850 listener: String,
851 lane: Option<crate::info::Lane>,
852}
853
854impl AdoptSpec {
855 pub fn new(listener: &str) -> AdoptSpec {
857 AdoptSpec {
858 listener: listener.to_owned(),
859 lane: None,
860 }
861 }
862
863 pub fn lane(mut self, lane: crate::info::Lane) -> AdoptSpec {
865 self.lane = Some(lane);
866 self
867 }
868}
869
870fn check_spec(
873 spec: &mut ListenerSpec,
874 server_trusted: Option<&Arc<TrustedProxies>>,
875 has_tls: bool,
876 taken: &[String],
877) -> Result<(), NetError> {
878 if spec.name.is_none() {
879 spec.name = spec
880 .adopt
881 .as_ref()
882 .and_then(|h| h.name())
883 .map(str::to_owned);
884 }
885 let unix = spec.unix.is_some()
886 || spec
887 .adopt
888 .as_ref()
889 .is_some_and(|h| h.kind() == crate::info::Transport::Unix);
890 if unix && !cfg!(feature = "local") {
891 return Err(NetError::Unsupported {
892 feature: "Unix domain sockets (the `local` feature)",
893 platform: std::env::consts::OS,
894 });
895 }
896 if spec.tls {
897 if !cfg!(feature = "tls") {
898 return Err(NetError::Unsupported {
899 feature: "TLS (the `tls` feature)",
900 platform: std::env::consts::OS,
901 });
902 }
903 if !has_tls {
904 return Err(invalid("tls", ConfigReason::Missing));
905 }
906 }
907 if spec.proxy != ProxyMode::Off {
908 if !cfg!(feature = "proxy") {
909 return Err(NetError::Unsupported {
910 feature: "PROXY protocol (the `proxy` feature)",
911 platform: std::env::consts::OS,
912 });
913 }
914 if !unix {
916 let trusted = spec.trusted.clone().or_else(|| server_trusted.cloned());
917 match trusted {
918 Some(t) if !t.is_empty() => spec.trusted = Some(t),
919 _ => return Err(invalid("trusted_proxies", ConfigReason::Missing)),
921 }
922 }
923 }
924 if let Some(n) = &spec.name {
925 if n.len() > 64 {
926 return Err(invalid("name", ConfigReason::TooLarge { max: 64 }));
927 }
928 if taken.contains(n) {
929 return Err(invalid("name", ConfigReason::Inconsistent));
930 }
931 }
932 Ok(())
933}
934
935#[must_use = "dropping a Server stops it; call wait_for_stop() to run until a stop is requested"]
938pub struct Server {
939 pub(crate) shared: Arc<Shared>,
940 pub(crate) threads: Vec<JoinHandle<()>>,
941 pub(crate) listeners: Vec<ListenerInfo>,
942 pub(crate) stopped: bool,
943}
944
945impl Server {
946 pub fn open_door(&self) -> Result<(), NetError> {
952 self.shared.open_door()
953 }
954
955 pub fn close_door(&self) {
957 self.shared.close_door();
958 }
959
960 #[must_use]
962 pub fn door(&self) -> DoorState {
963 self.shared.door()
964 }
965
966 pub fn drain(&self, deadline: Instant) -> DrainReport {
969 self.shared.drain(deadline)
970 }
971
972 pub fn shutdown(mut self, deadline: Instant) -> DrainReport {
974 self.stop(deadline)
975 }
976
977 pub fn wait_for_stop(mut self) -> DrainReport {
980 let deadline = self.shared.wait_for_stop_request();
981 self.stop(deadline)
982 }
983
984 fn stop(&mut self, deadline: Instant) -> DrainReport {
985 if self.stopped {
986 return DrainReport {
987 graceful: 0,
988 killed: 0,
989 took: Duration::ZERO,
990 };
991 }
992 self.stopped = true;
993 let report = self.shared.drain(deadline);
994 self.shared.stop_threads();
995 for t in self.threads.drain(..) {
996 let _ = t.join();
997 }
998 self.shared.join_listeners();
999 report
1000 }
1001
1002 pub fn adopt(&self, conn: TcpConnection, spec: AdoptSpec) -> Result<ConnectionId, NetError> {
1029 let mut conn = conn;
1030 let link = conn.take_link().ok_or(NetError::Closed)?;
1031 let (Some(peer), Some(local)) =
1032 (link.info.peer.socket_addr(), link.info.local.socket_addr())
1033 else {
1034 return Err(invalid("connection", ConfigReason::Inconsistent));
1035 };
1036 let stream = link
1037 .into_stream(CloseReason::SessionClosed)
1038 .and_then(crate::sys::Stream::into_tcp)
1039 .ok_or(invalid("connection", ConfigReason::Inconsistent))?;
1040 self.shared
1041 .adopt(stream, peer, local, &spec.listener, spec.lane)
1042 }
1043
1044 pub fn release_listeners(&self) -> Result<Vec<ListenerHandle>, NetError> {
1054 self.shared.release_listeners()
1055 }
1056
1057 pub fn add_listener(&mut self, spec: ListenerSpec) -> Result<ListenerId, NetError> {
1081 let mut spec = spec;
1082 if self.listeners.len() >= 64 {
1083 return Err(invalid("listener", ConfigReason::TooLarge { max: 64 }));
1084 }
1085 let names: Vec<String> = self
1086 .listeners
1087 .iter()
1088 .filter_map(|l| l.name.clone())
1089 .collect();
1090 check_spec(
1091 &mut spec,
1092 self.shared.trusted.as_ref(),
1093 self.shared.tls.is_some(),
1094 &names,
1095 )?;
1096 let workers = u16::try_from(self.shared.worker_count()).unwrap_or(u16::MAX);
1097 spec.check_listener_limits()?;
1098 self.shared
1099 .base_limits
1100 .overlay(&spec.limits)
1101 .validate(workers, None)?;
1102 let info = self.shared.add_listener(spec)?;
1103 let id = info.id;
1104 self.listeners.push(info);
1105 Ok(id)
1106 }
1107
1108 pub fn remove_listener(
1118 &mut self,
1119 id: ListenerId,
1120 deadline: Instant,
1121 ) -> Result<DrainReport, NetError> {
1122 let report = self.shared.remove_listener(id, deadline)?;
1123 self.listeners.retain(|l| l.id != id);
1124 Ok(report)
1125 }
1126
1127 #[must_use]
1129 pub fn handle(&self) -> ServerHandle {
1130 ServerHandle {
1131 shared: self.shared.clone(),
1132 }
1133 }
1134
1135 #[must_use]
1137 pub fn stats(&self) -> Stats {
1138 self.shared.stats()
1139 }
1140
1141 #[must_use]
1143 pub fn listeners(&self) -> &[ListenerInfo] {
1144 &self.listeners
1145 }
1146}
1147
1148impl Drop for Server {
1149 fn drop(&mut self) {
1150 let deadline = Instant::now() + self.shared.drain_timeout;
1151 self.stop(deadline);
1152 }
1153}
1154
1155impl fmt::Debug for Server {
1156 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1157 f.debug_struct("Server")
1158 .field("listeners", &self.listeners)
1159 .field("door", &self.door())
1160 .finish()
1161 }
1162}
1163
1164#[derive(Clone)]
1178pub struct ServerHandle {
1179 pub(crate) shared: Arc<Shared>,
1180}
1181
1182impl ServerHandle {
1183 pub fn kill(&self, id: ConnectionId, reason: KillReason) -> bool {
1185 self.shared.kill(id, reason)
1186 }
1187
1188 pub fn close_door(&self) {
1190 self.shared.close_door();
1191 }
1192
1193 pub fn request_stop(&self) {
1195 self.shared.request_stop(None);
1196 }
1197
1198 pub fn wake_all(&self) {
1200 self.shared.wake_all();
1201 }
1202
1203 #[must_use]
1205 pub fn completer(&self, worker: WorkerId) -> Option<crate::completer::Completer> {
1206 self.shared.completer(worker)
1207 }
1208
1209 #[must_use]
1211 pub fn stats(&self) -> Stats {
1212 self.shared.stats()
1213 }
1214
1215 #[must_use]
1217 pub fn door(&self) -> DoorState {
1218 self.shared.door()
1219 }
1220}
1221
1222impl fmt::Debug for ServerHandle {
1223 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1224 f.debug_struct("ServerHandle")
1225 .field("door", &self.door())
1226 .finish()
1227 }
1228}
1229
1230impl Stats {
1231 pub(crate) fn from_parts(
1232 mut base: Stats,
1233 refused_by: [u64; RefusalCause::COUNT],
1234 closed_by: [u64; CloseReason::COUNT],
1235 ) -> Stats {
1236 base.refused_by = refused_by;
1237 base.closed_by = closed_by;
1238 base
1239 }
1240}