queue consumers: stop taking messages on sigterm and exit 0
This commit is contained in:
parent
a089da25a5
commit
a8775f1a7a
1 changed files with 164 additions and 53 deletions
|
|
@ -182,6 +182,7 @@ struct Args {
|
|||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
let stop = swarm_secret_client::shutdown_signal();
|
||||
swarm_secret_client::install_crypto_provider();
|
||||
tracing_subscriber::fmt()
|
||||
.with_env_filter(
|
||||
|
|
@ -210,14 +211,14 @@ async fn main() -> anyhow::Result<()> {
|
|||
)
|
||||
.await
|
||||
}
|
||||
(None, Some(args)) => serve(args).await,
|
||||
(None, Some(args)) => serve(args, stop).await,
|
||||
(None, None) => unreachable!("clap requires the responder's flags when no verb is given"),
|
||||
}
|
||||
}
|
||||
|
||||
/// The responder: answers every authorization request until its
|
||||
/// subscription ends.
|
||||
async fn serve(args: Args) -> anyhow::Result<()> {
|
||||
async fn serve(args: Args, stop: swarm_secret_client::Shutdown) -> anyhow::Result<()> {
|
||||
// The bucket is NOT a flag. `swarm_queue_client::status`'s own docs say
|
||||
// why: reader and writer must name the same bucket, and an option is a
|
||||
// way for two deployments to disagree about which one that is. This
|
||||
|
|
@ -279,25 +280,33 @@ async fn serve(args: Args) -> anyhow::Result<()> {
|
|||
callout_xkey,
|
||||
issuer,
|
||||
});
|
||||
answer_each(requests, CONCURRENT_REQUESTS, move |msg| {
|
||||
let responder = Arc::clone(&responder);
|
||||
let client = client.clone();
|
||||
async move {
|
||||
let Some((reply_to, reply)) = responder.answer(&msg).await else {
|
||||
return;
|
||||
};
|
||||
if let Err(e) = client.publish(reply_to, reply.into()).await {
|
||||
tracing::warn!(error = ?e, "failed to publish auth response");
|
||||
answer_each(
|
||||
requests.take_until(stop.clone()),
|
||||
CONCURRENT_REQUESTS,
|
||||
move |msg| {
|
||||
let responder = Arc::clone(&responder);
|
||||
let client = client.clone();
|
||||
async move {
|
||||
let Some((reply_to, reply)) = responder.answer(&msg).await else {
|
||||
return;
|
||||
};
|
||||
if let Err(e) = client.publish(reply_to, reply.into()).await {
|
||||
tracing::warn!(error = ?e, "failed to publish auth response");
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
)
|
||||
.await;
|
||||
anyhow::bail!("subscription to {AUTH_SUBJECT} ended")
|
||||
if stop.peek().is_none() {
|
||||
anyhow::bail!("subscription to {AUTH_SUBJECT} ended");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Runs `answer` on each of `requests`, at most `permits` at once. A request
|
||||
/// waits in `requests` until a permit is free; the reply carries `server_id`
|
||||
/// and `user_nkey`, so the order answers go out in does not matter.
|
||||
/// and `user_nkey`, so the order answers go out in does not matter. Once
|
||||
/// `requests` ends, the answers in hand get the drain to finish.
|
||||
async fn answer_each<R, F, Fut>(
|
||||
mut requests: impl futures_util::Stream<Item = R> + Unpin,
|
||||
permits: usize,
|
||||
|
|
@ -307,16 +316,18 @@ async fn answer_each<R, F, Fut>(
|
|||
Fut: std::future::Future<Output = ()> + Send + 'static,
|
||||
{
|
||||
let permits = Arc::new(tokio::sync::Semaphore::new(permits));
|
||||
let mut in_flight = swarm_secret_client::InFlight::new();
|
||||
while let Some(request) = requests.next().await {
|
||||
let Ok(permit) = Arc::clone(&permits).acquire_owned().await else {
|
||||
unreachable!("the semaphore is never closed");
|
||||
};
|
||||
let answering = answer(request);
|
||||
tokio::spawn(async move {
|
||||
in_flight.spawn(async move {
|
||||
answering.await;
|
||||
drop(permit);
|
||||
});
|
||||
}
|
||||
in_flight.drain().await;
|
||||
}
|
||||
|
||||
/// Whether the server has stopped waiting for the answer to a request whose
|
||||
|
|
|
|||
Loading…
Reference in a new issue