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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use std::collections::VecDeque;
use parking_lot::Mutex;
use tokio::sync::{Notify, oneshot};
use crate::grpc_registration::RegisterGrpcServiceFn;
/// Installers for a specific gear (gear name + service installers).
#[derive(Default)]
pub struct GearInstallers {
pub gear_name: String,
pub installers: Vec<RegisterGrpcServiceFn>,
}
/// Grouped installers for all gears in the process.
#[derive(Default)]
pub struct GrpcInstallerData {
pub gears: Vec<GearInstallers>,
/// Whether a later batch may follow ([`GrpcInstallerStore::publish_late`]):
/// the application group's services, installed once it has started. An
/// empty initial batch with more to come still needs a listener.
pub more_batches: bool,
}
/// A batch of installers published after the hub started serving. The hub
/// installs it on its running listener, then [`ack`](Self::ack)s it.
pub struct LateBatch {
pub gears: Vec<GearInstallers>,
ack: oneshot::Sender<anyhow::Result<()>>,
}
impl LateBatch {
/// Report whether the batch is installed and callable.
pub fn ack(self, result: anyhow::Result<()>) {
// The publisher stopped waiting: nothing to report to.
drop(self.ack.send(result));
}
}
/// Runtime-owned gRPC installers: the hub takes system services before serving,
/// then installs and acknowledges application services on the same listener.
/// Each batch runs once; earlier installers are never rerun.
pub struct GrpcInstallerStore {
inner: Mutex<Inner>,
late_published: Notify,
}
#[derive(Default)]
struct Inner {
initial: Option<GrpcInstallerData>,
initial_set: bool,
late: VecDeque<LateBatch>,
closed: bool,
}
impl GrpcInstallerStore {
#[must_use]
pub fn new() -> Self {
Self {
inner: Mutex::new(Inner::default()),
late_published: Notify::new(),
}
}
/// Set the initial batch once. Fails if already set.
///
/// # Errors
/// Returns an error if the initial batch has already been set.
pub fn set(&self, data: GrpcInstallerData) -> anyhow::Result<()> {
let mut guard = self.inner.lock();
if guard.initial_set {
anyhow::bail!("gRPC installers already initialized");
}
guard.initial_set = true;
guard.initial = Some(data);
Ok(())
}
/// Consume and return the initial batch.
pub fn take(&self) -> Option<GrpcInstallerData> {
self.inner.lock().initial.take()
}
/// Check if the initial batch is absent (optional helper).
#[allow(dead_code)]
pub fn is_empty(&self) -> bool {
self.inner.lock().initial.is_none()
}
/// Publish a late batch for the running hub. The receiver resolves with
/// the hub's [`LateBatch::ack`]; it errs when the hub drops the batch
/// without installing it.
///
/// # Errors
/// The store is closed: no more batches are accepted.
pub fn publish_late(
&self,
gears: Vec<GearInstallers>,
) -> anyhow::Result<oneshot::Receiver<anyhow::Result<()>>> {
let (ack, installed) = oneshot::channel();
{
let mut guard = self.inner.lock();
anyhow::ensure!(!guard.closed, "gRPC installer store is closed");
guard.late.push_back(LateBatch { gears, ack });
}
self.late_published.notify_waiters();
Ok(installed)
}
/// The next late batch; `None` once the store is closed and drained.
// cancel-safe: a batch is popped and returned under the lock with no await
// in between, so a future dropped at its await loses no batch.
pub async fn next_late(&self) -> Option<LateBatch> {
loop {
let published = self.late_published.notified();
tokio::pin!(published);
published.as_mut().enable();
{
let mut guard = self.inner.lock();
if let Some(batch) = guard.late.pop_front() {
return Some(batch);
}
if guard.closed {
return None;
}
}
published.await;
}
}
/// No more late batches: [`next_late`](Self::next_late) ends once the
/// queued ones are taken.
pub fn close(&self) {
self.inner.lock().closed = true;
self.late_published.notify_waiters();
}
}
impl Default for GrpcInstallerStore {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
#[path = "grpc_installers_tests.rs"]
mod grpc_installers_tests;