forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlauncher.rs
156 lines (127 loc) · 6.4 KB
/
launcher.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
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;
use derive_more::Constructor;
use futures_util::StreamExt;
use tokio::select;
use tokio::sync::oneshot;
use super::request_buffer::ActiveRequests;
use crate::bootstrap::jobs::Started;
use crate::core::Tracker;
use crate::servers::logging::STARTED_ON;
use crate::servers::registar::ServiceHealthCheckJob;
use crate::servers::signals::{shutdown_signal_with_message, Halted};
use crate::servers::udp::server::bound_socket::BoundSocket;
use crate::servers::udp::server::processor::Processor;
use crate::servers::udp::server::receiver::Receiver;
use crate::servers::udp::UDP_TRACKER_LOG_TARGET;
use crate::shared::bit_torrent::tracker::udp::client::check;
/// A UDP server instance launcher.
#[derive(Constructor)]
pub struct Launcher;
impl Launcher {
/// It starts the UDP server instance with graceful shutdown.
///
/// # Panics
///
/// It panics if unable to bind to udp socket, and get the address from the udp socket.
/// It also panics if unable to send address of socket.
pub async fn run_with_graceful_shutdown(
tracker: Arc<Tracker>,
bind_to: SocketAddr,
tx_start: oneshot::Sender<Started>,
rx_halt: oneshot::Receiver<Halted>,
) {
let halt_task = tokio::task::spawn(shutdown_signal_with_message(
rx_halt,
format!("Halting UDP Service Bound to Socket: {bind_to}"),
));
tracing::info!(target: UDP_TRACKER_LOG_TARGET, "Starting on: {bind_to}");
let socket = tokio::time::timeout(Duration::from_millis(5000), BoundSocket::new(bind_to))
.await
.expect("it should bind to the socket within five seconds");
let bound_socket = match socket {
Ok(socket) => socket,
Err(e) => {
tracing::error!(target: UDP_TRACKER_LOG_TARGET, addr = %bind_to, err = %e, "Udp::run_with_graceful_shutdown panic! (error when building socket)" );
panic!("could not bind to socket!");
}
};
let address = bound_socket.address();
let local_udp_url = bound_socket.url().to_string();
tracing::info!(target: UDP_TRACKER_LOG_TARGET, "{STARTED_ON}: {local_udp_url}");
let receiver = Receiver::new(bound_socket.into());
tracing::trace!(target: UDP_TRACKER_LOG_TARGET, local_udp_url, "Udp::run_with_graceful_shutdown (spawning main loop)");
let running = {
let local_addr = local_udp_url.clone();
tokio::task::spawn(async move {
tracing::debug!(target: UDP_TRACKER_LOG_TARGET, local_addr, "Udp::run_with_graceful_shutdown::task (listening...)");
let () = Self::run_udp_server_main(receiver, tracker.clone()).await;
})
};
tx_start
.send(Started { address })
.expect("the UDP Tracker service should not be dropped");
tracing::debug!(target: UDP_TRACKER_LOG_TARGET, local_udp_url, "Udp::run_with_graceful_shutdown (started)");
let stop = running.abort_handle();
select! {
_ = running => { tracing::debug!(target: UDP_TRACKER_LOG_TARGET, local_udp_url, "Udp::run_with_graceful_shutdown (stopped)"); },
_ = halt_task => { tracing::debug!(target: UDP_TRACKER_LOG_TARGET, local_udp_url, "Udp::run_with_graceful_shutdown (halting)"); }
}
stop.abort();
tokio::task::yield_now().await; // lets allow the other threads to complete.
}
#[must_use]
pub fn check(binding: &SocketAddr) -> ServiceHealthCheckJob {
let binding = *binding;
let info = format!("checking the udp tracker health check at: {binding}");
let job = tokio::spawn(async move { check(&binding).await });
ServiceHealthCheckJob::new(binding, info, job)
}
async fn run_udp_server_main(mut receiver: Receiver, tracker: Arc<Tracker>) {
let active_requests = &mut ActiveRequests::default();
let addr = receiver.bound_socket_address();
let local_addr = format!("udp://{addr}");
loop {
let processor = Processor::new(receiver.socket.clone(), tracker.clone());
if let Some(req) = {
tracing::trace!(target: UDP_TRACKER_LOG_TARGET, local_addr, "Udp::run_udp_server (wait for request)");
receiver.next().await
} {
tracing::trace!(target: UDP_TRACKER_LOG_TARGET, local_addr, "Udp::run_udp_server::loop (in)");
let req = match req {
Ok(req) => req,
Err(e) => {
if e.kind() == std::io::ErrorKind::Interrupted {
tracing::warn!(target: UDP_TRACKER_LOG_TARGET, local_addr, err = %e, "Udp::run_udp_server::loop (interrupted)");
return;
}
tracing::error!(target: UDP_TRACKER_LOG_TARGET, local_addr, err = %e, "Udp::run_udp_server::loop break: (got error)");
break;
}
};
// We spawn the new task even if there active requests buffer is
// full. This could seem counterintuitive because we are accepting
// more request and consuming more memory even if the server is
// already busy. However, we "force_push" the new tasks in the
// buffer. That means, in the worst scenario we will abort a
// running task to make place for the new task.
//
// Once concern could be to reach an starvation point were we
// are only adding and removing tasks without given them the
// chance to finish. However, the buffer is yielding before
// aborting one tasks, giving it the chance to finish.
let abort_handle: tokio::task::AbortHandle = tokio::task::spawn(processor.process_request(req)).abort_handle();
if abort_handle.is_finished() {
continue;
}
active_requests.force_push(abort_handle, &local_addr).await;
} else {
tokio::task::yield_now().await;
// the request iterator returned `None`.
tracing::error!(target: UDP_TRACKER_LOG_TARGET, local_addr, "Udp::run_udp_server breaking: (ran dry, should not happen in production!)");
break;
}
}
}
}