reifydb_sub_server/
state.rs1use std::time::Duration;
10
11use reifydb_auth::service::AuthService;
12use reifydb_engine::engine::StandardEngine;
13use reifydb_runtime::{
14 actor::system::ActorSystem,
15 context::{clock::Clock, rng::Rng},
16};
17
18use crate::interceptor::RequestInterceptorChain;
19
20#[derive(Debug, Clone)]
22pub struct StateConfig {
23 pub query_timeout: Duration,
26 pub request_timeout: Duration,
29 pub max_connections: usize,
32 pub admin_enabled: bool,
34}
35
36impl Default for StateConfig {
37 fn default() -> Self {
38 Self {
39 query_timeout: Duration::from_secs(30),
40 request_timeout: Duration::from_secs(60),
41 max_connections: 10_000,
42 admin_enabled: false,
43 }
44 }
45}
46
47impl StateConfig {
48 pub fn new() -> Self {
50 Self::default()
51 }
52
53 pub fn query_timeout(mut self, timeout: Duration) -> Self {
55 self.query_timeout = timeout;
56 self
57 }
58
59 pub fn request_timeout(mut self, timeout: Duration) -> Self {
61 self.request_timeout = timeout;
62 self
63 }
64
65 pub fn max_connections(mut self, max: usize) -> Self {
67 self.max_connections = max;
68 self
69 }
70
71 pub fn admin_enabled(mut self, enabled: bool) -> Self {
73 self.admin_enabled = enabled;
74 self
75 }
76}
77
78#[derive(Clone)]
96pub struct AppState {
97 actor_system: ActorSystem,
98 engine: StandardEngine,
99 auth_service: AuthService,
100 config: StateConfig,
101 request_interceptors: RequestInterceptorChain,
102 clock: Clock,
103 rng: Rng,
104}
105
106impl AppState {
107 pub fn new(
110 actor_system: ActorSystem,
111 engine: StandardEngine,
112 auth_service: AuthService,
113 config: StateConfig,
114 request_interceptors: RequestInterceptorChain,
115 clock: Clock,
116 rng: Rng,
117 ) -> Self {
118 Self {
119 actor_system,
120 engine,
121 auth_service,
122 config,
123 request_interceptors,
124 clock,
125 rng,
126 }
127 }
128
129 pub fn clone_with_config(&self, config: StateConfig) -> Self {
132 Self {
133 actor_system: self.actor_system.clone(),
134 engine: self.engine.clone(),
135 auth_service: self.auth_service.clone(),
136 config,
137 request_interceptors: self.request_interceptors.clone(),
138 clock: self.clock.clone(),
139 rng: self.rng.clone(),
140 }
141 }
142
143 #[inline]
147 pub fn actor_system(&self) -> ActorSystem {
148 self.actor_system.clone()
149 }
150
151 #[inline]
153 pub fn engine(&self) -> &StandardEngine {
154 &self.engine
155 }
156
157 #[inline]
161 pub fn engine_clone(&self) -> StandardEngine {
162 self.engine.clone()
163 }
164
165 #[inline]
167 pub fn config(&self) -> &StateConfig {
168 &self.config
169 }
170
171 #[inline]
173 pub fn query_timeout(&self) -> Duration {
174 self.config.query_timeout
175 }
176
177 #[inline]
179 pub fn request_timeout(&self) -> Duration {
180 self.config.request_timeout
181 }
182
183 #[inline]
185 pub fn max_connections(&self) -> usize {
186 self.config.max_connections
187 }
188
189 #[inline]
191 pub fn admin_enabled(&self) -> bool {
192 self.config.admin_enabled
193 }
194
195 #[inline]
197 pub fn request_interceptors(&self) -> &RequestInterceptorChain {
198 &self.request_interceptors
199 }
200
201 #[inline]
203 pub fn clock(&self) -> &Clock {
204 &self.clock
205 }
206
207 #[inline]
209 pub fn rng(&self) -> &Rng {
210 &self.rng
211 }
212
213 #[inline]
215 pub fn auth_service(&self) -> &AuthService {
216 &self.auth_service
217 }
218}
219
220#[cfg(test)]
221pub mod tests {
222 use super::*;
223
224 #[test]
225 fn test_query_defaults() {
226 let config = StateConfig::default();
227 assert_eq!(config.query_timeout, Duration::from_secs(30));
228 assert_eq!(config.request_timeout, Duration::from_secs(60));
229 assert_eq!(config.max_connections, 10_000);
230 }
231
232 #[test]
233 fn test_query_config_builder() {
234 let config = StateConfig::new()
235 .query_timeout(Duration::from_secs(60))
236 .request_timeout(Duration::from_secs(120))
237 .max_connections(5_000);
238
239 assert_eq!(config.query_timeout, Duration::from_secs(60));
240 assert_eq!(config.request_timeout, Duration::from_secs(120));
241 assert_eq!(config.max_connections, 5_000);
242 }
243}