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