Skip to content

Commit 025f100

Browse files
committed
refactor: [#1311] move static initialization to the module where the static values are used
This will alllow to initizlize static values in other packages, not only the main tracker app.
1 parent df4a59e commit 025f100

File tree

6 files changed

+37
-17
lines changed

6 files changed

+37
-17
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ figment = "0"
5252
futures = "0"
5353
futures-util = "0"
5454
hyper = "1"
55-
lazy_static = "1"
5655
parking_lot = "0"
5756
percent-encoding = "2"
5857
r2d2 = "0"

packages/clock/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ version.workspace = true
1818
[dependencies]
1919
chrono = { version = "0", default-features = false, features = ["clock"] }
2020
lazy_static = "1"
21+
tracing = "0"
2122

2223
torrust-tracker-primitives = { version = "3.0.0-develop", path = "../primitives" }
2324

packages/clock/src/lib.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@
2222
//! > **NOTICE**: the timestamp does not depend on the time zone. That gives you
2323
//! > the ability to use the clock regardless of the underlying system time zone
2424
//! > configuration. See [Unix time Wikipedia entry](https://en.wikipedia.org/wiki/Unix_time).
25-
2625
pub mod clock;
2726
pub mod conv;
2827
pub mod static_time;
2928

3029
#[macro_use]
3130
extern crate lazy_static;
3231

32+
use tracing::instrument;
33+
3334
/// This code needs to be copied into each crate.
3435
/// Working version, for production.
3536
#[cfg(not(test))]
@@ -40,3 +41,16 @@ pub(crate) type CurrentClock = clock::Working;
4041
#[cfg(test)]
4142
#[allow(dead_code)]
4243
pub(crate) type CurrentClock = clock::Stopped;
44+
45+
/// It initializes the application static values.
46+
///
47+
/// These values are accessible throughout the entire application:
48+
///
49+
/// - The time when the application started.
50+
/// - An ephemeral instance random seed. This seed is used for encryption and
51+
/// it's changed when the main application process is restarted.
52+
#[instrument(skip())]
53+
pub fn initialize_static() {
54+
// Set the time of Torrust app starting
55+
lazy_static::initialize(&static_time::TIME_AT_APP_START);
56+
}

packages/udp-tracker-core/src/lib.rs

+16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ pub mod crypto;
44
pub mod services;
55
pub mod statistics;
66

7+
use crypto::ephemeral_instance_keys;
8+
use tracing::instrument;
9+
710
#[macro_use]
811
extern crate lazy_static;
912

@@ -12,3 +15,16 @@ extern crate lazy_static;
1215
pub const MAX_CONNECTION_ID_ERRORS_PER_IP: u32 = 10;
1316

1417
pub const UDP_TRACKER_LOG_TARGET: &str = "UDP TRACKER";
18+
19+
/// It initializes the static values.
20+
#[instrument(skip())]
21+
pub fn initialize_static() {
22+
// Initialize the Ephemeral Instance Random Seed
23+
lazy_static::initialize(&ephemeral_instance_keys::RANDOM_SEED);
24+
25+
// Initialize the Ephemeral Instance Random Cipher
26+
lazy_static::initialize(&ephemeral_instance_keys::RANDOM_CIPHER_BLOWFISH);
27+
28+
// Initialize the Zeroed Cipher
29+
lazy_static::initialize(&ephemeral_instance_keys::ZEROED_TEST_CIPHER_BLOWFISH);
30+
}

src/bootstrap/app.rs

+4-14
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
//! 2. Initialize static variables.
1212
//! 3. Initialize logging.
1313
//! 4. Initialize the domain tracker.
14-
use bittorrent_udp_tracker_core::crypto::ephemeral_instance_keys;
1514
use bittorrent_udp_tracker_core::crypto::keys::{self, Keeper as _};
16-
use torrust_tracker_clock::static_time;
1715
use torrust_tracker_configuration::validator::Validator;
1816
use torrust_tracker_configuration::{logging, Configuration};
1917
use tracing::instrument;
@@ -71,18 +69,10 @@ pub fn initialize_global_services(configuration: &Configuration) {
7169
/// These values are accessible throughout the entire application:
7270
///
7371
/// - The time when the application started.
74-
/// - An ephemeral instance random seed. This seed is used for encryption and it's changed when the main application process is restarted.
72+
/// - An ephemeral instance random seed. This seed is used for encryption and
73+
/// it's changed when the main application process is restarted.
7574
#[instrument(skip())]
7675
pub fn initialize_static() {
77-
// Set the time of Torrust app starting
78-
lazy_static::initialize(&static_time::TIME_AT_APP_START);
79-
80-
// Initialize the Ephemeral Instance Random Seed
81-
lazy_static::initialize(&ephemeral_instance_keys::RANDOM_SEED);
82-
83-
// Initialize the Ephemeral Instance Random Cipher
84-
lazy_static::initialize(&ephemeral_instance_keys::RANDOM_CIPHER_BLOWFISH);
85-
86-
// Initialize the Zeroed Cipher
87-
lazy_static::initialize(&ephemeral_instance_keys::ZEROED_TEST_CIPHER_BLOWFISH);
76+
torrust_tracker_clock::initialize_static();
77+
bittorrent_udp_tracker_core::initialize_static();
8878
}

0 commit comments

Comments
 (0)