-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathservice.rs
385 lines (330 loc) · 12.1 KB
/
service.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
use std::fmt;
use std::sync::Arc;
use actix::prelude::*;
use actix_web::{server, App};
use failure::{Backtrace, Context, Fail, ResultExt};
use listenfd::ListenFd;
use once_cell::race::OnceBox;
use relay_aws_extension::AwsExtension;
use relay_config::Config;
use relay_metrics::{Aggregator, AggregatorService};
use relay_redis::RedisPool;
use relay_system::{Addr, Configure, Controller, Service};
use crate::actors::envelopes::{EnvelopeManager, EnvelopeManagerService};
use crate::actors::health_check::{HealthCheck, HealthCheckService};
use crate::actors::outcome::{OutcomeProducer, OutcomeProducerService, TrackOutcome};
use crate::actors::outcome_aggregator::OutcomeAggregator;
use crate::actors::processor::{EnvelopeProcessor, EnvelopeProcessorService};
use crate::actors::project_cache::ProjectCache;
use crate::actors::relays::{RelayCache, RelayCacheService};
#[cfg(feature = "processing")]
use crate::actors::store::StoreService;
use crate::actors::test_store::{TestStore, TestStoreService};
use crate::actors::upstream::UpstreamRelay;
use crate::middlewares::{
AddCommonHeaders, ErrorHandlers, Metrics, ReadRequestMiddleware, SentryMiddleware,
};
use crate::utils::BufferGuard;
use crate::{endpoints, utils};
pub static REGISTRY: OnceBox<Registry> = OnceBox::new();
/// Common error type for the relay server.
#[derive(Debug)]
pub struct ServerError {
inner: Context<ServerErrorKind>,
}
/// Indicates the type of failure of the server.
#[derive(Debug, Fail, Copy, Clone, PartialEq, Eq, Hash)]
pub enum ServerErrorKind {
/// Binding failed.
#[fail(display = "bind to interface failed")]
BindFailed,
/// Listening on the HTTP socket failed.
#[fail(display = "listening failed")]
ListenFailed,
/// A TLS error ocurred.
#[fail(display = "could not initialize the TLS server")]
TlsInitFailed,
/// TLS support was not compiled in.
#[fail(display = "compile with the `ssl` feature to enable SSL support")]
TlsNotSupported,
/// GeoIp construction failed.
#[fail(display = "could not load the Geoip Db")]
GeoIpError,
/// Configuration failed.
#[fail(display = "configuration error")]
ConfigError,
/// Initializing the Kafka producer failed.
#[fail(display = "could not initialize kafka producer")]
KafkaError,
/// Initializing the Redis cluster client failed.
#[fail(display = "could not initialize redis cluster client")]
RedisError,
}
impl Fail for ServerError {
fn cause(&self) -> Option<&dyn Fail> {
self.inner.cause()
}
fn backtrace(&self) -> Option<&Backtrace> {
self.inner.backtrace()
}
}
impl fmt::Display for ServerError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.inner, f)
}
}
impl ServerError {
/// Returns the error kind of the error.
pub fn kind(&self) -> ServerErrorKind {
*self.inner.get_context()
}
}
impl From<ServerErrorKind> for ServerError {
fn from(kind: ServerErrorKind) -> ServerError {
ServerError {
inner: Context::new(kind),
}
}
}
impl From<Context<ServerErrorKind>> for ServerError {
fn from(inner: Context<ServerErrorKind>) -> ServerError {
ServerError { inner }
}
}
#[derive(Clone)]
pub struct Registry {
pub aggregator: Addr<Aggregator>,
pub health_check: Addr<HealthCheck>,
pub outcome_producer: Addr<OutcomeProducer>,
pub outcome_aggregator: Addr<TrackOutcome>,
pub processor: Addr<EnvelopeProcessor>,
pub envelope_manager: Addr<EnvelopeManager>,
pub test_store: Addr<TestStore>,
pub relay_cache: Addr<RelayCache>,
}
impl Registry {
/// Get the [`AggregatorService`] address from the registry.
///
/// TODO(actix): this is temporary solution while migrating `ProjectCache` actor to the new tokio
/// runtime and follow up refactoring of the dependencies.
pub fn aggregator() -> Addr<Aggregator> {
REGISTRY.get().unwrap().aggregator.clone()
}
}
impl fmt::Debug for Registry {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Registry")
.field("aggregator", &self.aggregator)
.field("health_check", &self.health_check)
.field("outcome_producer", &self.outcome_producer)
.field("outcome_aggregator", &self.outcome_aggregator)
.field("processor", &format_args!("Addr<Processor>"))
.finish()
}
}
/// Server state.
#[derive(Clone)]
pub struct ServiceState {
config: Arc<Config>,
buffer_guard: Arc<BufferGuard>,
_aggregator_runtime: Arc<tokio::runtime::Runtime>,
_outcome_runtime: Arc<tokio::runtime::Runtime>,
_main_runtime: Arc<tokio::runtime::Runtime>,
_store_runtime: Option<Arc<tokio::runtime::Runtime>>,
}
impl ServiceState {
/// Starts all services and returns addresses to all of them.
pub fn start(config: Arc<Config>) -> Result<Self, ServerError> {
let system = System::current();
let registry = system.registry();
let main_runtime = utils::create_runtime(config.cpu_concurrency());
let aggregator_runtime = utils::create_runtime(1);
let outcome_runtime = utils::create_runtime(1);
let mut _store_runtime = None;
let upstream_relay = UpstreamRelay::new(config.clone());
registry.set(Arbiter::start(|_| upstream_relay));
let guard = outcome_runtime.enter();
let outcome_producer = OutcomeProducerService::create(config.clone())?.start();
let outcome_aggregator = OutcomeAggregator::new(&config, outcome_producer.clone()).start();
drop(guard);
let redis_pool = match config.redis() {
Some(redis_config) if config.processing_enabled() => {
Some(RedisPool::new(redis_config).context(ServerErrorKind::RedisError)?)
}
_ => None,
};
let _guard = main_runtime.enter();
let buffer = Arc::new(BufferGuard::new(config.envelope_buffer_size()));
let processor = EnvelopeProcessorService::new(config.clone(), redis_pool.clone())?.start();
#[allow(unused_mut)]
let mut envelope_manager = EnvelopeManagerService::new(config.clone());
#[cfg(feature = "processing")]
if config.processing_enabled() {
let rt = utils::create_runtime(1);
let _guard = rt.enter();
let store = StoreService::create(config.clone())?.start();
envelope_manager.set_store_forwarder(store);
_store_runtime = Some(rt);
}
let envelope_manager = envelope_manager.start();
let test_store = TestStoreService::new(config.clone()).start();
let project_cache = ProjectCache::new(config.clone(), redis_pool);
let project_cache = Arbiter::start(|_| project_cache);
registry.set(project_cache.clone());
let health_check = HealthCheckService::new(config.clone()).start();
let relay_cache = RelayCacheService::new(config.clone()).start();
if let Some(aws_api) = config.aws_runtime_api() {
if let Ok(aws_extension) = AwsExtension::new(aws_api) {
aws_extension.start();
}
}
let guard = aggregator_runtime.enter();
let aggregator =
AggregatorService::new(config.aggregator_config(), Some(project_cache.recipient()))
.start();
drop(guard);
REGISTRY
.set(Box::new(Registry {
aggregator,
processor,
health_check,
outcome_producer,
outcome_aggregator,
envelope_manager,
test_store,
relay_cache,
}))
.unwrap();
Ok(ServiceState {
buffer_guard: buffer,
config,
_aggregator_runtime: Arc::new(aggregator_runtime),
_outcome_runtime: Arc::new(outcome_runtime),
_main_runtime: Arc::new(main_runtime),
_store_runtime: _store_runtime.map(Arc::new),
})
}
/// Returns an atomically counted reference to the config.
pub fn config(&self) -> Arc<Config> {
self.config.clone()
}
/// Returns a reference to the guard of the envelope buffer.
///
/// This can be used to enter new envelopes into the processing queue and reserve a slot in the
/// buffer. See [`BufferGuard`] for more information.
pub fn buffer_guard(&self) -> Arc<BufferGuard> {
self.buffer_guard.clone()
}
}
/// The actix app type for the relay web service.
pub type ServiceApp = App<ServiceState>;
fn make_app(state: ServiceState) -> ServiceApp {
App::with_state(state)
.middleware(SentryMiddleware::new())
.middleware(Metrics)
.middleware(AddCommonHeaders)
.middleware(ErrorHandlers)
.middleware(ReadRequestMiddleware)
.configure(endpoints::configure_app)
}
fn dump_listen_infos<H, F>(server: &server::HttpServer<H, F>)
where
H: server::IntoHttpHandler + 'static,
F: Fn() -> H + Send + Clone + 'static,
{
relay_log::info!("spawning http server");
for (addr, scheme) in server.addrs_with_scheme() {
relay_log::info!(" listening on: {}://{}/", scheme, addr);
}
}
fn listen<H, F>(
server: server::HttpServer<H, F>,
config: &Config,
) -> Result<server::HttpServer<H, F>, ServerError>
where
H: server::IntoHttpHandler + 'static,
F: Fn() -> H + Send + Clone + 'static,
{
Ok(
match ListenFd::from_env()
.take_tcp_listener(0)
.context(ServerErrorKind::BindFailed)?
{
Some(listener) => server.listen(listener),
None => server
.bind(config.listen_addr())
.context(ServerErrorKind::BindFailed)?,
},
)
}
#[cfg(feature = "ssl")]
fn listen_ssl<H, F>(
mut server: server::HttpServer<H, F>,
config: &Config,
) -> Result<server::HttpServer<H, F>, ServerError>
where
H: server::IntoHttpHandler + 'static,
F: Fn() -> H + Send + Clone + 'static,
{
if let (Some(addr), Some(path), Some(password)) = (
config.tls_listen_addr(),
config.tls_identity_path(),
config.tls_identity_password(),
) {
use native_tls::{Identity, TlsAcceptor};
use std::fs::File;
use std::io::Read;
let mut file = File::open(path).unwrap();
let mut data = vec![];
file.read_to_end(&mut data).unwrap();
let identity =
Identity::from_pkcs12(&data, password).context(ServerErrorKind::TlsInitFailed)?;
let acceptor = TlsAcceptor::builder(identity)
.build()
.context(ServerErrorKind::TlsInitFailed)?;
server = server
.bind_tls(addr, acceptor)
.context(ServerErrorKind::BindFailed)?;
}
Ok(server)
}
#[cfg(not(feature = "ssl"))]
fn listen_ssl<H, F>(
server: server::HttpServer<H, F>,
config: &Config,
) -> Result<server::HttpServer<H, F>, ServerError>
where
H: server::IntoHttpHandler + 'static,
F: Fn() -> H + Send + Clone + 'static,
{
if config.tls_listen_addr().is_some()
|| config.tls_identity_path().is_some()
|| config.tls_identity_password().is_some()
{
Err(ServerErrorKind::TlsNotSupported.into())
} else {
Ok(server)
}
}
/// Given a relay config spawns the server together with all actors and lets them run forever.
///
/// Effectively this boots the server.
pub fn start(config: Config) -> Result<Recipient<server::StopServer>, ServerError> {
let config = Arc::new(config);
Controller::from_registry().do_send(Configure {
shutdown_timeout: config.shutdown_timeout(),
});
let state = ServiceState::start(config.clone())?;
let mut server = server::new(move || make_app(state.clone()));
server = server
.workers(config.cpu_concurrency())
.shutdown_timeout(config.shutdown_timeout().as_secs() as u16)
.maxconn(config.max_connections())
.maxconnrate(config.max_connection_rate())
.backlog(config.max_pending_connections())
.disable_signals();
server = listen(server, &config)?;
server = listen_ssl(server, &config)?;
dump_listen_infos(&server);
Ok(server.start().recipient())
}