Cache user lang determination

This commit is contained in:
Thorsten Sommer 2026-02-25 19:29:46 +01:00
parent f0de897fa9
commit 1197ca63ef
Signed by untrusted user who does not match committer: tsommer
GPG Key ID: 371BBA77A02C0108
5 changed files with 88 additions and 26 deletions

View File

@ -100,7 +100,7 @@ public sealed class RustAvailabilityMonitorService : BackgroundService, IMessage
{ {
try try
{ {
await this.rustService.ReadUserLanguage(); await this.rustService.ReadUserLanguage(forceRequest: true);
} }
catch (Exception e) catch (Exception e)
{ {

View File

@ -2,15 +2,34 @@
public sealed partial class RustService public sealed partial class RustService
{ {
public async Task<string> ReadUserLanguage() public async Task<string> ReadUserLanguage(bool forceRequest = false)
{ {
var response = await this.http.GetAsync("/system/language"); if (!forceRequest && !string.IsNullOrWhiteSpace(this.cachedUserLanguage))
if (!response.IsSuccessStatusCode) return this.cachedUserLanguage;
await this.userLanguageLock.WaitAsync();
try
{ {
this.logger!.LogError($"Failed to read the user language from Rust: '{response.StatusCode}'"); if (!forceRequest && !string.IsNullOrWhiteSpace(this.cachedUserLanguage))
return string.Empty; return this.cachedUserLanguage;
var response = await this.http.GetAsync("/system/language");
if (!response.IsSuccessStatusCode)
{
this.logger!.LogError($"Failed to read the user language from Rust: '{response.StatusCode}'");
return string.Empty;
}
var userLanguage = (await response.Content.ReadAsStringAsync()).Trim();
if (string.IsNullOrWhiteSpace(userLanguage))
return string.Empty;
this.cachedUserLanguage = userLanguage;
return userLanguage;
}
finally
{
this.userLanguageLock.Release();
} }
return await response.Content.ReadAsStringAsync();
} }
} }

View File

@ -17,6 +17,7 @@ public sealed partial class RustService : BackgroundService
private static string TB(string fallbackEN) => I18N.I.T(fallbackEN, typeof(RustService).Namespace, nameof(RustService)); private static string TB(string fallbackEN) => I18N.I.T(fallbackEN, typeof(RustService).Namespace, nameof(RustService));
private readonly HttpClient http; private readonly HttpClient http;
private readonly SemaphoreSlim userLanguageLock = new(1, 1);
private readonly JsonSerializerOptions jsonRustSerializerOptions = new() private readonly JsonSerializerOptions jsonRustSerializerOptions = new()
{ {
@ -29,6 +30,7 @@ public sealed partial class RustService : BackgroundService
private ILogger<RustService>? logger; private ILogger<RustService>? logger;
private Encryption? encryptor; private Encryption? encryptor;
private string? cachedUserLanguage;
private readonly string apiPort; private readonly string apiPort;
private readonly string certificateFingerprint; private readonly string certificateFingerprint;
@ -88,6 +90,7 @@ public sealed partial class RustService : BackgroundService
public override void Dispose() public override void Dispose()
{ {
this.http.Dispose(); this.http.Dispose();
this.userLanguageLock.Dispose();
base.Dispose(); base.Dispose();
} }

View File

@ -1 +1,4 @@
# v26.3.1, build 235 (2026-03-xx xx:xx UTC) # v26.3.1, build 235 (2026-03-xx xx:xx UTC)
- Improved the performance by caching the OS language detection and requesting the user language only once per app start.
- Improved the user-language logging by limiting language detection logs to a single entry per app start.

View File

@ -15,6 +15,9 @@ pub static DATA_DIRECTORY: OnceLock<String> = OnceLock::new();
/// The config directory where the application stores its configuration. /// The config directory where the application stores its configuration.
pub static CONFIG_DIRECTORY: OnceLock<String> = OnceLock::new(); pub static CONFIG_DIRECTORY: OnceLock<String> = OnceLock::new();
/// The user language cached once per runtime process.
static USER_LANGUAGE: OnceLock<String> = OnceLock::new();
/// Returns the config directory. /// Returns the config directory.
#[get("/system/directories/config")] #[get("/system/directories/config")]
pub fn get_config_directory(_token: APIToken) -> String { pub fn get_config_directory(_token: APIToken) -> String {
@ -87,12 +90,11 @@ fn normalize_locale_tag(locale: &str) -> Option<String> {
} }
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
fn read_locale_from_environment() -> Option<String> { fn read_locale_from_environment() -> Option<(String, &'static str)> {
if let Ok(language) = env::var("LANGUAGE") { if let Ok(language) = env::var("LANGUAGE") {
for candidate in language.split(':') { for candidate in language.split(':') {
if let Some(locale) = normalize_locale_tag(candidate) { if let Some(locale) = normalize_locale_tag(candidate) {
info!("Detected user language from Linux environment variable 'LANGUAGE': '{}'.", locale); return Some((locale, "LANGUAGE"));
return Some(locale);
} }
} }
} }
@ -100,8 +102,7 @@ fn read_locale_from_environment() -> Option<String> {
for key in ["LC_ALL", "LC_MESSAGES", "LANG"] { for key in ["LC_ALL", "LC_MESSAGES", "LANG"] {
if let Ok(value) = env::var(key) { if let Ok(value) = env::var(key) {
if let Some(locale) = normalize_locale_tag(&value) { if let Some(locale) = normalize_locale_tag(&value) {
info!("Detected user language from Linux environment variable '{}': '{}'.", key, locale); return Some((locale, key));
return Some(locale);
} }
} }
} }
@ -110,10 +111,35 @@ fn read_locale_from_environment() -> Option<String> {
} }
#[cfg(not(target_os = "linux"))] #[cfg(not(target_os = "linux"))]
fn read_locale_from_environment() -> Option<String> { fn read_locale_from_environment() -> Option<(String, &'static str)> {
None None
} }
enum LanguageDetectionSource {
SysLocale,
LinuxEnvironmentVariable(&'static str),
DefaultLanguage,
}
fn detect_user_language() -> (String, LanguageDetectionSource) {
if let Some(locale) = get_locale() {
if let Some(normalized_locale) = normalize_locale_tag(&locale) {
return (normalized_locale, LanguageDetectionSource::SysLocale);
}
warn!("sys-locale returned an unusable locale value: '{}'.", locale);
}
if let Some((locale, key)) = read_locale_from_environment() {
return (locale, LanguageDetectionSource::LinuxEnvironmentVariable(key));
}
(
String::from(DEFAULT_LANGUAGE),
LanguageDetectionSource::DefaultLanguage,
)
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::normalize_locale_tag; use super::normalize_locale_tag;
@ -137,21 +163,32 @@ mod tests {
#[get("/system/language")] #[get("/system/language")]
pub fn read_user_language(_token: APIToken) -> String { pub fn read_user_language(_token: APIToken) -> String {
if let Some(locale) = get_locale() { USER_LANGUAGE
if let Some(normalized_locale) = normalize_locale_tag(&locale) { .get_or_init(|| {
info!("Detected user language from sys-locale: '{}'.", normalized_locale); let (user_language, source) = detect_user_language();
return normalized_locale; match source {
} LanguageDetectionSource::SysLocale => {
info!("Detected user language from sys-locale: '{}'.", user_language);
},
warn!("sys-locale returned an unusable locale value: '{}'.", locale); LanguageDetectionSource::LinuxEnvironmentVariable(key) => {
} info!(
"Detected user language from Linux environment variable '{}': '{}'.",
key, user_language
);
},
if let Some(locale) = read_locale_from_environment() { LanguageDetectionSource::DefaultLanguage => {
return locale; warn!(
} "Could not determine the system language. Use default '{}'.",
DEFAULT_LANGUAGE
);
},
}
warn!("Could not determine the system language. Use default '{}'.", DEFAULT_LANGUAGE); user_language
String::from(DEFAULT_LANGUAGE) })
.clone()
} }
#[get("/system/enterprise/config/id")] #[get("/system/enterprise/config/id")]