Skip to main content

reifydb_sub_server_http/
state.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use std::ops::Deref;
5
6use reifydb_core::actors::server::ServerMessage;
7use reifydb_runtime::actor::{mailbox::ActorRef, system::ActorHandle};
8use reifydb_sub_server::state::AppState;
9
10/// HTTP server state wrapping the shared `AppState`.
11///
12/// Spawns a fresh actor per request to avoid serializing all HTTP requests
13/// through a single actor mailbox.
14#[derive(Clone)]
15pub struct HttpServerState {
16	state: AppState,
17}
18
19impl HttpServerState {
20	pub fn new(state: AppState) -> Self {
21		Self {
22			state,
23		}
24	}
25
26	pub fn state(&self) -> &AppState {
27		&self.state
28	}
29
30	/// Spawn a short-lived actor for one request and return its ref + handle.
31	///
32	/// The caller must keep the `ActorHandle` alive until the reply is received;
33	/// dropping it shuts down the actor.
34	pub fn spawn_actor(&self) -> (ActorRef<ServerMessage>, ActorHandle<ServerMessage>) {
35		self.state.spawn_server_actor()
36	}
37}
38
39impl Deref for HttpServerState {
40	type Target = AppState;
41
42	fn deref(&self) -> &Self::Target {
43		&self.state
44	}
45}