fix: change tokio::sync::Notify to tokio_util::sync::CancellationToken

This commit is contained in:
Jun Kurihara 2024-07-26 20:58:00 +09:00
commit 0950fdbd15
No known key found for this signature in database
GPG key ID: D992B3E3DE1DED23
8 changed files with 22 additions and 18 deletions

View file

@ -2,6 +2,7 @@ use crate::{constants::*, count::RequestCount};
use hot_reload::ReloaderReceiver;
use rpxy_certs::ServerCryptoBase;
use std::{net::SocketAddr, sync::Arc, time::Duration};
use tokio_util::sync::CancellationToken;
/// Global object containing proxy configurations and shared object like counters.
/// But note that in Globals, we do not have Mutex and RwLock. It is indeed, the context shared among async tasks.
@ -13,7 +14,7 @@ pub struct Globals {
/// Shared context - Async task runtime handler
pub runtime_handle: tokio::runtime::Handle,
/// Shared context - Notify object to stop async tasks
pub term_notify: Option<Arc<tokio::sync::Notify>>,
pub cancel_token: Option<CancellationToken>,
/// Shared context - Certificate reloader service receiver // TODO: newer one
pub cert_reloader_rx: Option<ReloaderReceiver<ServerCryptoBase>>,

View file

@ -23,6 +23,7 @@ use futures::future::select_all;
use hot_reload::ReloaderReceiver;
use rpxy_certs::ServerCryptoBase;
use std::sync::Arc;
use tokio_util::sync::CancellationToken;
/* ------------------------------------------------ */
pub use crate::globals::{AppConfig, AppConfigList, ProxyConfig, ReverseProxyConfig, TlsConfig, UpstreamUri};
@ -42,7 +43,7 @@ pub struct RpxyOptions {
/// Async task runtime handler
pub runtime_handle: tokio::runtime::Handle,
/// Notify object to stop async tasks
pub term_notify: Option<Arc<tokio::sync::Notify>>,
pub cancel_token: Option<CancellationToken>,
#[cfg(feature = "acme")]
/// ServerConfig used for only ACME challenge for ACME domains
@ -56,7 +57,7 @@ pub async fn entrypoint(
app_config_list,
cert_rx, // TODO:
runtime_handle,
term_notify,
cancel_token,
#[cfg(feature = "acme")]
server_configs_acme_challenge,
}: &RpxyOptions,
@ -107,7 +108,7 @@ pub async fn entrypoint(
proxy_config: proxy_config.clone(),
request_count: Default::default(),
runtime_handle: runtime_handle.clone(),
term_notify: term_notify.clone(),
cancel_token: cancel_token.clone(),
cert_reloader_rx: cert_rx.clone(),
#[cfg(feature = "acme")]

View file

@ -312,13 +312,13 @@ where
}
};
match &self.globals.term_notify {
Some(term) => {
match &self.globals.cancel_token {
Some(cancel_token) => {
select! {
_ = proxy_service.fuse() => {
warn!("Proxy service got down");
}
_ = term.notified().fuse() => {
_ = cancel_token.cancelled().fuse() => {
info!("Proxy service listening on {} receives term signal", self.listening_on);
}
}