dotnet / aspire

An opinionated, cloud ready stack for building observable, production ready, distributed applications in .NET
https://learn.microsoft.com/dotnet/aspire
MIT License
3.79k stars 440 forks source link

Make ResourceService a singleton #1318

Open drewnoakes opened 10 months ago

drewnoakes commented 10 months ago

Currently, the IResourceService component is scoped to the user session:

https://github.com/dotnet/aspire/blob/f7bd7c2d7f6821d2113e164070382420657c767f/src/Aspire.Hosting/Dcp/DcpHostService.cs#L65

Every page load initialises a connection to kubernetes, subscribing to containers, executables, endpoints and services. This data streams in asynchronously, concurrent with the page load. From the user's perspective, the UI takes a few beats to reach a steady state.

We should change this to a singleton so that clients receive all data with the initial connection, and F5 is instantaneous. This will also reduce memory when multiple clients are connected.

Note that we cannot change to a singleton just yet, as IResourceService works with mutable ResourceViewModel objects.

Before tacking this, we will make IResourceService work with immutable ResourceSnapshot values, and move the mutable view model state to the client, scoped to the current user session.

Scoped (before)

fast-reload-before

Singleton (after)

fast-reload-after

drewnoakes commented 10 months ago

1003 will make ResourceService a server-only component, however there'll be an equivalent client-side component that should then become a singleton.