all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: "Lukas Wagner" <l.wagner@proxmox.com>
To: "Robert Obkircher" <r.obkircher@proxmox.com>,
	"Lukas Wagner" <l.wagner@proxmox.com>
Cc: pdm-devel@lists.proxmox.com
Subject: Re: [PATCH proxmox 01/20] router: introduce shared state
Date: Mon, 24 Aug 2026 15:13:20 +0200	[thread overview]
Message-ID: <DKX78D3YEF6G.3JEDF7P6J90HL@proxmox.com> (raw)
In-Reply-To: <178732075530.242959.4186982857535541130.b4-review@b4>

On Fri Aug 21, 2026 at 3:59 PM CEST, Robert Obkircher wrote:
>> +use anyhow::{Error, bail};
>> +
>> +/// Registry of state values, keyed by their type.
>> +///
>> +/// It holds at most one value per type. Values are registered with
>> +/// [`register`](SharedStateRegistry::register) before the registry is handed over to the API
>> +/// environment, from where handlers can access them through
>> +/// [`RpcEnvironment::shared_state`](crate::RpcEnvironment::shared_state). This allows passing
>> +/// application context to handlers without resorting to globals.
>> +#[derive(Default)]
>> +pub struct SharedStateRegistry {
>> +    map: HashMap<TypeId, Box<dyn Any + Send + Sync>>,
>> +}
>
> We could get rid of some indirection by replacing the map with a
> Box<&dyn SharedState>. Probably not worth it, though.

Yeah, the map lookups happen once per API request, so I don't think the
indirection should be an issue here -- performance-wise at least.

The current approach has in my opinion the advantage that it keeps the
type-id magic contained within proxmox-router. With the trait-based
approach, if I understand it correctly, the application code would need
to perform the cast from dyn Any. Now, given that this is handled behind
the scenes by the API macro, it should not matter that much, probably.

>
> trait SharedState { fn lookup(&self, id: TypeId) -> &dyn Any; }
>
> struct PdmState { a: A, b: B }
>
> impl SharedState for PdmState {
>     fn lookup(&self, id: TypeId) -> &dyn Any {
>         if id == TypeId::of::<A>() { &self.a }
>         else if id == TypeId::of::<B>() { &self.b }
>         else { unreachable!() }
>     }
> }
>
>
>> +
>> +impl SharedStateRegistry {
>> +    /// Get a clone of the registered value of type `T`, if there is one.
>> +    pub fn lookup<T: 'static + Send + Sync + Clone>(&self) -> Option<T> {
>> +        self.map
>> +            .get(&TypeId::of::<T>())
>> +            .and_then(|s| s.downcast_ref())
>> +            .cloned()
> This could .expect("value must have correct type for key").


Makes sense, as a failed cast is clearly a bug here. Thanks!




  reply	other threads:[~2026-08-24 13:13 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-17 12:57 [PATCH datacenter-manager/proxmox 00/20] inject application context via API macro for easier integration testing Lukas Wagner
2026-08-17 12:57 ` [PATCH proxmox 01/20] router: introduce shared state Lukas Wagner
2026-08-17 13:26   ` Lukas Wagner
2026-08-20 11:23   ` Lukas Wagner
2026-08-21 13:59   ` Robert Obkircher
2026-08-24 13:13     ` Lukas Wagner [this message]
2026-08-17 12:57 ` [PATCH proxmox 02/20] rest-server: allow to inject " Lukas Wagner
2026-08-17 12:57 ` [PATCH proxmox 03/20] api-macro: support shared state extraction type Lukas Wagner
2026-08-21 13:59   ` Robert Obkircher
2026-08-24 13:13     ` Lukas Wagner
2026-08-17 12:57 ` [PATCH datacenter-manager 04/20] context: promote context to a dir-style module Lukas Wagner
2026-08-17 12:57 ` [PATCH datacenter-manager 05/20] pdm-config: remotes: rename trait methods to read/write/lock Lukas Wagner
2026-08-17 12:57 ` [PATCH datacenter-manager 06/20] pdm-config: subscriptions: " Lukas Wagner
2026-08-21 14:00   ` Robert Obkircher
2026-08-24 13:13     ` Lukas Wagner
2026-08-17 12:57 ` [PATCH datacenter-manager 07/20] remote iterator: pass remote config reader explicitly Lukas Wagner
2026-08-17 12:57 ` [PATCH datacenter-manager 08/20] context: introduce a ContextFactory to build application context Lukas Wagner
2026-08-17 12:57 ` [PATCH datacenter-manager 09/20] context: establish PdmApplication object Lukas Wagner
2026-08-17 12:57 ` [PATCH datacenter-manager 10/20] context: register PdmApplication in router Lukas Wagner
2026-08-17 12:57 ` [PATCH datacenter-manager 11/20] parallel fetcher: pass arguments to closure in a single type Lukas Wagner
2026-08-21 14:00   ` Robert Obkircher
2026-08-24 13:13     ` Lukas Wagner
2026-08-17 12:57 ` [PATCH datacenter-manager 12/20] parallel fetcher: support a custom client factory Lukas Wagner
2026-08-17 12:57 ` [PATCH datacenter-manager 13/20] api: sdn: use PdmApplication handle for accessing remotes Lukas Wagner
2026-08-17 12:57 ` [PATCH datacenter-manager 14/20] tests: add helpers for building API-handler-level integration tests Lukas Wagner
2026-08-17 12:57 ` [PATCH datacenter-manager 15/20] tests: add example tests for SDN API routes Lukas Wagner
2026-08-17 12:57 ` [PATCH datacenter-manager 16/20] api-cache: add wrapper type Lukas Wagner
2026-08-17 12:57 ` [PATCH datacenter-manager 17/20] context: provide api-cache on the app object Lukas Wagner
2026-08-17 12:57 ` [PATCH datacenter-manager 18/20] api: subscriptions: use PdmApplication instead of globals Lukas Wagner
2026-08-17 12:57 ` [PATCH datacenter-manager 19/20] pdm-config: subscriptions: drop unused accessor functions Lukas Wagner
2026-08-17 12:57 ` [PATCH datacenter-manager 20/20] tests: add example tests for remote subscription management Lukas Wagner
2026-08-20 14:54 ` superseded: [PATCH datacenter-manager/proxmox 00/20] inject application context via API macro for easier integration testing 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=DKX78D3YEF6G.3JEDF7P6J90HL@proxmox.com \
    --to=l.wagner@proxmox.com \
    --cc=pdm-devel@lists.proxmox.com \
    --cc=r.obkircher@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal