]> Untitled Git - lemmy.git/blob - crates/utils/src/lib.rs
9ca427cf934c230a95d04379f2afcb2f5a60cbbb
[lemmy.git] / crates / utils / src / lib.rs
1 #[macro_use]
2 extern crate strum_macros;
3 #[macro_use]
4 extern crate smart_default;
5
6 pub mod apub;
7 pub mod email;
8 pub mod rate_limit;
9 pub mod settings;
10
11 pub mod claims;
12 pub mod error;
13 pub mod request;
14 pub mod response;
15 pub mod utils;
16 pub mod version;
17
18 use error::LemmyError;
19 use futures::Future;
20 use once_cell::sync::Lazy;
21 use std::time::Duration;
22 use tracing::Instrument;
23
24 pub type ConnectionId = usize;
25
26 pub const REQWEST_TIMEOUT: Duration = Duration::from_secs(10);
27
28 #[macro_export]
29 macro_rules! location_info {
30   () => {
31     format!(
32       "None value at {}:{}, column {}",
33       file!(),
34       line!(),
35       column!()
36     )
37   };
38 }
39
40 /// if true, all federation should happen synchronously. useful for debugging and testing.
41 /// defaults to true on debug mode, false on releasemode
42 /// override to true by setting env LEMMY_SYNCHRONOUS_FEDERATION=1
43 /// override to false by setting env LEMMY_SYNCHRONOUS_FEDERATION=""
44 pub static SYNCHRONOUS_FEDERATION: Lazy<bool> = Lazy::new(|| {
45   std::env::var("LEMMY_SYNCHRONOUS_FEDERATION")
46     .map(|s| !s.is_empty())
47     .unwrap_or(cfg!(debug_assertions))
48 });
49
50 /// tokio::spawn, but accepts a future that may fail and also
51 /// * logs errors
52 /// * attaches the spawned task to the tracing span of the caller for better logging
53 pub fn spawn_try_task(task: impl Future<Output = Result<(), LemmyError>> + Send + 'static) {
54   tokio::spawn(
55     async {
56       if let Err(e) = task.await {
57         tracing::warn!("error in spawn: {e}");
58       }
59     }
60     .in_current_span(), // this makes sure the inner tracing gets the same context as where spawn was called
61   );
62 }