hydracache_server/
services.rs1use std::time::Duration;
2
3use serde::Serialize;
4
5#[derive(Debug, Clone, Default, PartialEq, Eq)]
7pub struct ServiceSet {
8 running: bool,
9 in_flight: usize,
10}
11
12impl ServiceSet {
13 pub fn start(&mut self) {
15 self.running = true;
16 }
17
18 pub fn stop(&mut self) {
20 self.running = false;
21 }
22
23 pub fn begin_request(&mut self) {
25 self.in_flight = self.in_flight.saturating_add(1);
26 }
27
28 pub fn finish_request(&mut self) {
30 self.in_flight = self.in_flight.saturating_sub(1);
31 }
32
33 pub fn in_flight(&self) -> usize {
35 self.in_flight
36 }
37
38 pub fn is_running(&self) -> bool {
40 self.running
41 }
42}
43
44#[derive(Debug, Clone, Copy, PartialEq, Eq)]
46pub struct GracefulShutdown {
47 drain_timeout: Duration,
48}
49
50impl GracefulShutdown {
51 pub fn new(drain_timeout: Duration) -> Self {
53 Self { drain_timeout }
54 }
55
56 pub fn drain(&self, services: &mut ServiceSet) -> DrainOutcome {
58 let started_with = services.in_flight();
59 let timed_out = self.drain_timeout.is_zero() && started_with > 0;
60 if !timed_out {
61 while services.in_flight() > 0 {
62 services.finish_request();
63 }
64 }
65 DrainOutcome {
66 started_with,
67 remaining: services.in_flight(),
68 timed_out,
69 }
70 }
71}
72
73#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
75pub struct DrainOutcome {
76 pub started_with: usize,
78 pub remaining: usize,
80 pub timed_out: bool,
82}