Skip to main content

reifydb_sub_server_http/
state.rs

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