Skip to content

Commit a741f23

Browse files
Mariusz Reichertm-reichert
authored andcommitted
Making salt rotation interval configurable
1 parent 3ebad43 commit a741f23

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/bin/electrs.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ extern crate electrs;
66

77
use crossbeam_channel::{self as channel};
88
use error_chain::ChainedError;
9-
use std::{process, thread};
9+
use std::{env, process, thread};
1010
use std::sync::{Arc, RwLock};
1111
use std::time::Duration;
1212
use bitcoin::hex::DisplayHex;
@@ -29,6 +29,9 @@ use electrs::otlp_trace;
2929
use electrs::elements::AssetRegistry;
3030
use electrs::metrics::MetricOpts;
3131

32+
/// Default salt rotation interval in seconds (24 hours)
33+
const DEFAULT_SALT_ROTATION_INTERVAL_SECS: u64 = 24 * 3600;
34+
3235
fn fetch_from(config: &Config, store: &Store) -> FetchFrom {
3336
let mut jsonrpc_import = config.jsonrpc_import;
3437
if !jsonrpc_import {
@@ -166,13 +169,25 @@ fn rotate_salt(salt: &mut String) {
166169
*salt = generate_salt();
167170
}
168171

172+
fn get_salt_rotation_interval() -> Duration {
173+
let var_name = "SALT_ROTATION_INTERVAL_SECS";
174+
let secs = env::var(var_name)
175+
.ok()
176+
.and_then(|val| val.parse::<u64>().ok())
177+
.unwrap_or(DEFAULT_SALT_ROTATION_INTERVAL_SECS);
178+
179+
Duration::from_secs(secs)
180+
}
181+
169182
fn spawn_salt_rotation_thread() -> Arc<RwLock<String>> {
170183
let salt = generate_salt();
171184
let salt_rwlock = Arc::new(RwLock::new(salt));
172185
let writer_arc = Arc::clone(&salt_rwlock);
186+
let interval = get_salt_rotation_interval();
187+
173188
thread::spawn(move || {
174189
loop {
175-
thread::sleep(Duration::from_secs(24 * 3600)); // 24 hours
190+
thread::sleep(interval); // 24 hours
176191
{
177192
let mut guard = writer_arc.write().unwrap();
178193
rotate_salt(&mut *guard);

0 commit comments

Comments
 (0)