1
0
mirror of https://github.com/matrix-org/matrix-authentication-service.git synced 2025-08-07 17:03:01 +03:00

Add an ActivityTracker which tracks session activity and regularly flush them to the database

This commit is contained in:
Quentin Gliech
2023-09-19 17:10:09 +02:00
parent 16962b451b
commit cf5510a1a2
9 changed files with 563 additions and 12 deletions

View File

@@ -19,7 +19,8 @@ use clap::Parser;
use itertools::Itertools;
use mas_config::AppConfig;
use mas_handlers::{
AppState, CookieManager, HttpClientFactory, MatrixHomeserver, MetadataCache, SiteConfig,
ActivityTracker, AppState, CookieManager, HttpClientFactory, MatrixHomeserver, MetadataCache,
SiteConfig,
};
use mas_listener::{server::Server, shutdown::ShutdownStream};
use mas_matrix_synapse::SynapseConnection;
@@ -140,11 +141,13 @@ impl Options {
compat_token_ttl: config.experimental.compat_token_ttl,
};
let activity_tracker = ActivityTracker::new(pool.clone(), Duration::from_secs(60 * 5));
// Explicitly the config to properly zeroize secret keys
drop(config);
// Listen for SIGHUP
register_sighup(&templates)?;
register_sighup(&templates, &activity_tracker)?;
let graphql_schema = mas_handlers::graphql_schema(&pool, &policy_factory, conn);
@@ -163,6 +166,7 @@ impl Options {
http_client_factory,
password_manager,
site_config,
activity_tracker,
conn_acquisition_histogram: None,
};
s.init_metrics()?;
@@ -242,6 +246,8 @@ impl Options {
mas_listener::server::run_servers(servers, shutdown).await;
state.activity_tracker.shutdown().await;
Ok(())
}
}

View File

@@ -20,7 +20,7 @@ use mas_config::{
PasswordsConfig, PolicyConfig, TemplatesConfig,
};
use mas_email::{MailTransport, Mailer};
use mas_handlers::passwords::PasswordManager;
use mas_handlers::{passwords::PasswordManager, ActivityTracker};
use mas_policy::PolicyFactory;
use mas_router::UrlBuilder;
use mas_templates::{TemplateLoadingError, Templates};
@@ -206,11 +206,16 @@ pub async fn database_connection_from_config(
}
/// Reload templates on SIGHUP
pub fn register_sighup(templates: &Templates) -> anyhow::Result<()> {
pub fn register_sighup(
templates: &Templates,
activity_tracker: &ActivityTracker,
) -> anyhow::Result<()> {
#[cfg(unix)]
{
let mut signal = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::hangup())?;
let templates = templates.clone();
let activity_tracker = activity_tracker.clone();
tokio::spawn(async move {
loop {
if signal.recv().await.is_none() {
@@ -218,8 +223,9 @@ pub fn register_sighup(templates: &Templates) -> anyhow::Result<()> {
break;
};
info!("SIGHUP received, reloading templates");
info!("SIGHUP received, reloading templates & flushing activity tracker");
activity_tracker.flush().await;
templates.clone().reload().await.unwrap_or_else(|err| {
error!(?err, "Error while reloading templates");
});