forked from TraceMachina/nativelink
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshutdown_guard.rs
More file actions
130 lines (118 loc) · 3.8 KB
/
shutdown_guard.rs
File metadata and controls
130 lines (118 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2024 The NativeLink Authors. All rights reserved.
//
// Licensed under the Functional Source License, Version 1.1, Apache 2.0 Future License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// See LICENSE file for details
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::collections::HashMap;
use tokio::sync::watch;
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub enum Priority {
// The least important priority.
LeastImportant = 2,
// All priorities greater than 1 must be completed.
P1 = 1,
// All other priorities must be completed.
P0 = 0,
}
impl From<usize> for Priority {
fn from(value: usize) -> Self {
match value {
0 => Self::P0,
1 => Self::P1,
_ => Self::LeastImportant,
}
}
}
impl From<Priority> for usize {
fn from(priority: Priority) -> Self {
match priority {
Priority::P0 => 0,
Priority::P1 => 1,
Priority::LeastImportant => 2,
}
}
}
/// Tracks other services that have registered to be notified when
/// the process is being shut down.
#[derive(Debug)]
pub struct ShutdownGuard {
priority: Priority,
tx: watch::Sender<HashMap<Priority, usize>>,
rx: watch::Receiver<HashMap<Priority, usize>>,
}
impl ShutdownGuard {
/// Waits for all priorities less important than the given
/// priority to be completed.
pub async fn wait_for(&mut self, priority: Priority) {
if priority != self.priority {
// Promote our priority to the new priority.
self.tx.send_modify(|map| {
let old_count = map.remove(&self.priority).unwrap_or(0).saturating_sub(1);
map.insert(self.priority, old_count);
self.priority = priority;
let new_count = map.get(&priority).unwrap_or(&0).saturating_add(1);
map.insert(priority, new_count);
});
}
// Ignore error because the receiver will never be closed
// if the sender is still alive here.
drop(
self.rx
.wait_for(|map| {
let start = usize::from(priority) + 1;
let end = usize::from(Priority::LeastImportant);
for p in start..=end {
if *map.get(&p.into()).unwrap_or(&0) > 0 {
return false;
}
}
true
})
.await,
);
}
}
impl Default for ShutdownGuard {
fn default() -> Self {
let priority = Priority::LeastImportant;
let mut map = HashMap::new();
map.insert(priority, 0);
let (tx, rx) = watch::channel(map);
Self { priority, tx, rx }
}
}
impl Clone for ShutdownGuard {
fn clone(&self) -> Self {
self.tx.send_modify(|map| {
map.insert(
self.priority,
map.get(&Priority::LeastImportant)
.unwrap_or(&0)
.saturating_add(1),
);
});
Self {
priority: Priority::LeastImportant,
tx: self.tx.clone(),
rx: self.rx.clone(),
}
}
}
impl Drop for ShutdownGuard {
fn drop(&mut self) {
self.tx.send_modify(|map| {
map.insert(
self.priority,
map.get(&self.priority).unwrap_or(&0).saturating_sub(1),
);
});
}
}