From: Lukas Wagner <l.wagner@proxmox.com>
To: pdm-devel@lists.proxmox.com
Subject: [PATCH datacenter-manager v3 17/21] api-cache: add wrapper type
Date: Thu, 27 Aug 2026 13:42:40 +0200 [thread overview]
Message-ID: <20260827114244.424784-18-l.wagner@proxmox.com> (raw)
In-Reply-To: <20260827114244.424784-1-l.wagner@proxmox.com>
NamespacedCache is supposed to be fully generic and eventually be moved
to a shared crate. ApiCache adds PDM-specific semantics, namely having
per-remote namespaces and also one global namespace.
Before, these additional semantics were encoded in the helper functions
in api_cache.rs (e.g. read_global, read_remote), but since we want to
put move a handle to the api cache into PdmApplication, the helpers are
turned into methods of this new wrapper type.
Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
---
Notes:
Changes since v2:
- Extend the commit message to hopefully better convey why this wrapper
is needed.
server/src/api_cache.rs | 98 +++++++++++++++++++++++++++++++++++------
1 file changed, 84 insertions(+), 14 deletions(-)
diff --git a/server/src/api_cache.rs b/server/src/api_cache.rs
index b20f0535..6dc8a043 100644
--- a/server/src/api_cache.rs
+++ b/server/src/api_cache.rs
@@ -59,6 +59,8 @@ use std::time::Duration;
use nix::sys::stat::Mode;
+use proxmox_sys::fs::CreateOptions;
+
use crate::namespaced_cache::{
BlockingReadableCacheNamespace, BlockingWritableCacheNamespace, CacheError, NamespacedCache,
ReadableCacheNamespace, WritableCacheNamespace,
@@ -70,57 +72,125 @@ pub const PDM_API_CACHE_PATH: &str = concat!(pdm_buildcfg::PDM_RUN_DIR_M!(), "/a
const GLOBAL_NAMESPACE: &str = "global";
const LOCK_TIMEOUT: Duration = Duration::from_secs(10);
-static CACHE: LazyLock<NamespacedCache> = LazyLock::new(|| {
+static CACHE: LazyLock<ApiCache> = LazyLock::new(|| {
let file_options = proxmox_product_config::default_create_options();
let dir_options = file_options.perm(Mode::from_bits_truncate(0o750));
- NamespacedCache::new(PathBuf::from(PDM_API_CACHE_PATH), dir_options, file_options)
+ ApiCache::new(PathBuf::from(PDM_API_CACHE_PATH), dir_options, file_options)
});
fn format_remote_namespace(remote: &str) -> String {
format!("remote-{remote}")
}
+/// Cache for API responses from remotes.
+///
+/// Thin wrapper around a [`NamespacedCache`], providing remote-specific and
+/// global namespaces as described in the module documentation.
+pub struct ApiCache {
+ cache: NamespacedCache,
+}
+
+impl ApiCache {
+ pub fn new<P: Into<PathBuf>>(
+ base_directory: P,
+ dir_options: CreateOptions,
+ file_options: CreateOptions,
+ ) -> Self {
+ Self {
+ cache: NamespacedCache::new(base_directory, dir_options, file_options),
+ }
+ }
+
+ /// Lock the cache for reading remote-specific data (blocking interface).
+ pub fn read_remote_blocking(
+ &self,
+ remote: &str,
+ ) -> Result<BlockingReadableCacheNamespace, CacheError> {
+ self.cache
+ .read_blocking(&format_remote_namespace(remote), LOCK_TIMEOUT)
+ }
+
+ /// Lock the cache for writing remote-specific data (blocking interface).
+ pub fn write_remote_blocking(
+ &self,
+ remote: &str,
+ ) -> Result<BlockingWritableCacheNamespace, CacheError> {
+ self.cache
+ .write_blocking(&format_remote_namespace(remote), LOCK_TIMEOUT)
+ }
+
+ /// Lock the cache for reading global data (blocking interface).
+ pub fn read_global_blocking(&self) -> Result<BlockingReadableCacheNamespace, CacheError> {
+ self.cache.read_blocking(GLOBAL_NAMESPACE, LOCK_TIMEOUT)
+ }
+
+ /// Lock the cache for writing global data (blocking interface).
+ pub fn write_global_blocking(&self) -> Result<BlockingWritableCacheNamespace, CacheError> {
+ self.cache.write_blocking(GLOBAL_NAMESPACE, LOCK_TIMEOUT)
+ }
+
+ /// Lock the cache for reading remote-specific data (async interface).
+ pub async fn read_remote(&self, remote: &str) -> Result<ReadableCacheNamespace, CacheError> {
+ self.cache
+ .read(&format_remote_namespace(remote), LOCK_TIMEOUT)
+ .await
+ }
+
+ /// Lock the cache for writing remote-specific data (async interface).
+ pub async fn write_remote(&self, remote: &str) -> Result<WritableCacheNamespace, CacheError> {
+ self.cache
+ .write(&format_remote_namespace(remote), LOCK_TIMEOUT)
+ .await
+ }
+
+ /// Lock the cache for reading global data (async interface).
+ pub async fn read_global(&self) -> Result<ReadableCacheNamespace, CacheError> {
+ self.cache.read(GLOBAL_NAMESPACE, LOCK_TIMEOUT).await
+ }
+
+ /// Lock the cache for writing global data (async interface).
+ pub async fn write_global(&self) -> Result<WritableCacheNamespace, CacheError> {
+ self.cache.write(GLOBAL_NAMESPACE, LOCK_TIMEOUT).await
+ }
+}
+
/// Lock the cache for reading remote-specific data (blocking interface).
pub fn read_remote_blocking(remote: &str) -> Result<BlockingReadableCacheNamespace, CacheError> {
- CACHE.read_blocking(&format_remote_namespace(remote), LOCK_TIMEOUT)
+ CACHE.read_remote_blocking(remote)
}
/// Lock the cache for writing remote-specific data (blocking interface).
pub fn write_remote_blocking(remote: &str) -> Result<BlockingWritableCacheNamespace, CacheError> {
- CACHE.write_blocking(&format_remote_namespace(remote), LOCK_TIMEOUT)
+ CACHE.write_remote_blocking(remote)
}
/// Lock the cache for reading global data (blocking interface).
pub fn read_global_blocking() -> Result<BlockingReadableCacheNamespace, CacheError> {
- CACHE.read_blocking(GLOBAL_NAMESPACE, LOCK_TIMEOUT)
+ CACHE.read_global_blocking()
}
/// Lock the cache for writing global data (blocking interface).
pub fn write_global_blocking() -> Result<BlockingWritableCacheNamespace, CacheError> {
- CACHE.write_blocking(GLOBAL_NAMESPACE, LOCK_TIMEOUT)
+ CACHE.write_global_blocking()
}
/// Lock the cache for reading remote-specific data (async interface).
pub async fn read_remote(remote: &str) -> Result<ReadableCacheNamespace, CacheError> {
- CACHE
- .read(&format_remote_namespace(remote), LOCK_TIMEOUT)
- .await
+ CACHE.read_remote(remote).await
}
/// Lock the cache for writing remote-specific data (async interface).
pub async fn write_remote(remote: &str) -> Result<WritableCacheNamespace, CacheError> {
- CACHE
- .write(&format_remote_namespace(remote), LOCK_TIMEOUT)
- .await
+ CACHE.write_remote(remote).await
}
/// Lock the cache for reading global data (async interface).
pub async fn read_global() -> Result<ReadableCacheNamespace, CacheError> {
- CACHE.read(GLOBAL_NAMESPACE, LOCK_TIMEOUT).await
+ CACHE.read_global().await
}
/// Lock the cache for writing global data (async interface).
pub async fn write_global() -> Result<WritableCacheNamespace, CacheError> {
- CACHE.write(GLOBAL_NAMESPACE, LOCK_TIMEOUT).await
+ CACHE.write_global().await
}
--
2.47.3
next prev parent reply other threads:[~2026-08-27 11:43 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-27 11:42 [PATCH datacenter-manager/proxmox v3 00/21] inject application context via API macro for easier integration testing Lukas Wagner
2026-08-27 11:42 ` [PATCH proxmox v3 01/21] router: introduce shared state Lukas Wagner
2026-08-27 11:42 ` [PATCH proxmox v3 02/21] rest-server: allow to inject " Lukas Wagner
2026-08-27 11:42 ` [PATCH proxmox v3 03/21] api-macro: support shared state extraction type Lukas Wagner
2026-08-27 11:42 ` [PATCH proxmox v3 04/21] product-config: add ProductConfig type Lukas Wagner
2026-08-27 11:42 ` [PATCH datacenter-manager v3 05/21] context: promote context to a dir-style module Lukas Wagner
2026-08-27 11:42 ` [PATCH datacenter-manager v3 06/21] pdm-config: remotes: rename trait methods to read/write/lock Lukas Wagner
2026-08-27 11:42 ` [PATCH datacenter-manager v3 07/21] pdm-config: subscriptions: " Lukas Wagner
2026-08-27 11:42 ` [PATCH datacenter-manager v3 08/21] remote iterator: pass remote config reader explicitly Lukas Wagner
2026-08-27 11:42 ` [PATCH datacenter-manager v3 09/21] context: introduce a ContextFactory to build application context Lukas Wagner
2026-08-27 11:42 ` [PATCH datacenter-manager v3 10/21] context: establish PdmApplication object Lukas Wagner
2026-08-27 11:42 ` [PATCH datacenter-manager v3 11/21] context: register PdmApplication in router Lukas Wagner
2026-08-27 11:42 ` [PATCH datacenter-manager v3 12/21] connection: use client factory from PdmApplication handle Lukas Wagner
2026-08-27 11:42 ` [PATCH datacenter-manager v3 13/21] parallel fetcher: pass arguments to closure in a single type Lukas Wagner
2026-08-27 11:42 ` [PATCH datacenter-manager v3 14/21] server: migrate existing ParallelFetcher users to use PdmApplication Lukas Wagner
2026-08-27 11:42 ` [PATCH datacenter-manager v3 15/21] tests: add helpers for building API-handler-level integration tests Lukas Wagner
2026-08-27 11:42 ` [PATCH datacenter-manager v3 16/21] tests: add example tests for SDN API routes Lukas Wagner
2026-08-27 11:42 ` Lukas Wagner [this message]
2026-08-27 11:42 ` [PATCH datacenter-manager v3 18/21] context: provide api-cache on the app object Lukas Wagner
2026-08-27 11:42 ` [PATCH datacenter-manager v3 19/21] api: subscriptions: use PdmApplication instead of globals Lukas Wagner
2026-08-27 11:42 ` [PATCH datacenter-manager v3 20/21] pdm-config: subscriptions: drop unused accessor functions Lukas Wagner
2026-08-27 11:42 ` [PATCH datacenter-manager v3 21/21] tests: add example tests for remote subscription management Lukas Wagner
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260827114244.424784-18-l.wagner@proxmox.com \
--to=l.wagner@proxmox.com \
--cc=pdm-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox