Skip to content

Commit 13de5a3

Browse files
author
avifenesh
committed
Refactor random number generation to use updated rand crate methods and add glide_paths module
Signed-off-by: avifenesh <[email protected]>
1 parent e516f0b commit 13de5a3

18 files changed

+250
-135
lines changed

deny.toml

+2-6
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@ db-urls = ["https://github.com/rustsec/advisory-db"]
2121
yanked = "deny"
2222
# A list of advisory IDs to ignore. Note that ignored advisories will still
2323
# output a note when they are encountered.
24-
ignore = [
25-
# suppress this validation until #3226 not resolved
26-
# https://github.com/valkey-io/valkey-glide/issues/3226
27-
"RUSTSEC-2025-0007",
28-
]
24+
ignore = []
2925
# Threshold for security vulnerabilities, any vulnerability with a CVSS score
3026
# lower than the range specified will be ignored. Note that ignored advisories
3127
# will still output a note when they are encountered.
@@ -59,7 +55,7 @@ allow = [
5955
"ISC",
6056
"OpenSSL",
6157
"MPL-2.0",
62-
"Unicode-3.0"
58+
"Unicode-3.0",
6359
]
6460
# The confidence threshold for detecting a license from license text.
6561
# The higher the value, the more closely the license text must be to the

glide-core/Cargo.toml

+19-19
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ authors = ["Valkey GLIDE Maintainers"]
99

1010
[dependencies]
1111
bytes = "1"
12-
futures = "^0.3"
12+
futures = "0.3"
1313
redis = { path = "./redis-rs/redis", features = [
1414
"aio",
1515
"tokio-comp",
@@ -21,27 +21,27 @@ redis = { path = "./redis-rs/redis", features = [
2121
telemetrylib = { path = "./telemetry" }
2222
tokio = { version = "1", features = ["macros", "time"] }
2323
logger_core = { path = "../logger_core" }
24-
dispose = "0.5.0"
25-
tokio-util = { version = "^0.7", features = ["rt"], optional = true }
26-
num_cpus = { version = "^1.15", optional = true }
27-
tokio-retry2 = {version = "0.5", features = ["jitter"]}
24+
dispose = "0.5"
25+
tokio-util = { version = "0.7", features = ["rt"], optional = true }
26+
num_cpus = { version = "1", optional = true }
27+
tokio-retry2 = { version = "0.5", features = ["jitter"] }
2828

2929
protobuf = { version = "3", features = [
3030
"bytes",
3131
"with-bytes",
3232
], optional = true }
33-
integer-encoding = { version = "4.0.0", optional = true }
34-
thiserror = "1"
35-
rand = { version = "0.8.5" }
36-
futures-intrusive = "0.5.0"
37-
directories = { version = "5.0", optional = true }
38-
once_cell = "1.18.0"
39-
sha1_smol = "1.0.0"
40-
nanoid = "0.4.0"
41-
async-trait = { version = "0.1.24" }
33+
integer-encoding = { version = "4", optional = true }
34+
thiserror = "2"
35+
rand = "0.9"
36+
futures-intrusive = "0.5"
37+
directories = { version = "6", optional = true }
38+
once_cell = "1"
39+
sha1_smol = "1"
40+
nanoid = "0.4"
41+
async-trait = { version = "0.1" }
4242
serde_json = "1"
4343
serde = { version = "1", features = ["derive"] }
44-
versions = "6.3"
44+
versions = "6"
4545

4646
[features]
4747
socket-layer = [
@@ -56,12 +56,12 @@ standalone_heartbeat = []
5656
[dev-dependencies]
5757
rsevents = "0.3.1"
5858
socket2 = "^0.5"
59-
tempfile = "3.3.0"
60-
rstest = "^0.23"
59+
tempfile = "3.17"
60+
rstest = "^0.24"
6161
serial_test = "3"
6262
criterion = { version = "^0.5", features = ["html_reports", "async_tokio"] }
63-
which = "6"
64-
ctor = "0.2.2"
63+
which = "7"
64+
ctor = "0.3"
6565
redis = { path = "./redis-rs/redis", features = ["tls-rustls-insecure"] }
6666
iai-callgrind = "0.14"
6767
tokio = { version = "1", features = ["rt-multi-thread"] }

glide-core/benches/rotating_buffer_benchmark.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use glide_core::{
1111
};
1212
use integer_encoding::VarInt;
1313
use protobuf::Message;
14-
use rand::{distributions::Alphanumeric, Rng};
14+
use rand::{distr::Alphanumeric, Rng};
1515

1616
fn benchmark(
1717
c: &mut Criterion,
@@ -102,7 +102,7 @@ fn benchmark_split_data(
102102
}
103103

104104
fn generate_random_string(length: usize) -> bytes::Bytes {
105-
let s: String = rand::thread_rng()
105+
let s: String = rand::rng()
106106
.sample_iter(&Alphanumeric)
107107
.take(length)
108108
.map(char::from)

glide-core/redis-rs/redis/Cargo.toml

+4-4
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ r2d2 = { version = "0.8.8", optional = true }
5858

5959
# Only needed for cluster
6060
crc16 = { version = "0.4", optional = true }
61-
rand = { version = "0.8", optional = true }
61+
rand = { version = "0.9", optional = true }
6262

6363
# Only needed for async cluster
6464
dashmap = { version = "6.0", optional = true }
@@ -73,10 +73,10 @@ native-tls = { version = "0.2", optional = true }
7373
tokio-native-tls = { version = "0.3", optional = true }
7474

7575
# Only needed for rustls
76-
rustls = { version = "0.22", optional = true }
76+
rustls = { version = "0.23.23", optional = true }
7777
webpki-roots = { version = "0.26", optional = true }
78-
rustls-native-certs = { version = "0.7", optional = true }
79-
tokio-rustls = { version = "0.25", optional = true }
78+
rustls-native-certs = { version = "0.8", optional = true }
79+
tokio-rustls = { version = "0.26", optional = true }
8080
rustls-pemfile = { version = "2", optional = true }
8181
rustls-pki-types = { version = "1", optional = true }
8282

glide-core/redis-rs/redis/src/cluster.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ use crate::{
5555
cluster_routing::{Redirect, Route, RoutingInfo},
5656
IntoConnectionInfo, PushInfo,
5757
};
58-
use rand::{seq::IteratorRandom, thread_rng};
58+
use rand::{rng, seq::IteratorRandom};
5959
use std::cell::RefCell;
6060
use std::collections::HashSet;
6161
use std::str::FromStr;
@@ -372,7 +372,7 @@ where
372372

373373
fn create_new_slots(&self) -> RedisResult<SlotMap> {
374374
let mut connections = self.connections.borrow_mut();
375-
let mut rng = thread_rng();
375+
let mut rng = rng();
376376
let len = connections.len();
377377
let samples = connections.iter_mut().choose_multiple(&mut rng, len);
378378
let mut result = Err(RedisError::from((
@@ -959,7 +959,7 @@ fn get_random_connection<C: ConnectionLike + Connect + Sized>(
959959
) -> (String, &mut C) {
960960
let addr = connections
961961
.keys()
962-
.choose(&mut thread_rng())
962+
.choose(&mut rand::rng())
963963
.expect("Connections is empty")
964964
.to_string();
965965
let con = connections.get_mut(&addr).expect("Connections is empty");

glide-core/redis-rs/redis/src/cluster_async/connections_container.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ where
636636
(!self.connection_map.is_empty()).then_some({
637637
self.connection_map
638638
.iter()
639-
.choose_multiple(&mut rand::thread_rng(), amount)
639+
.choose_multiple(&mut rand::rng(), amount)
640640
.into_iter()
641641
.map(move |item| {
642642
let (address, node) = (item.key(), item.value());
@@ -657,7 +657,7 @@ where
657657
// Increase the total number of connections by the number of connections managed by `node`
658658
Telemetry::incr_total_connections(node.connections_count());
659659

660-
if let Some(old_conn) = self.connection_map.insert(address.clone(), node) {
660+
if let Some(old_conn) = self.connection_map.insert(String::from(&address), node) {
661661
// We are replacing a node. Reduce the counter by the number of connections managed by
662662
// the old connection
663663
Telemetry::decr_total_connections(old_conn.connections_count());

glide-core/redis-rs/redis/src/cluster_client.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ impl RetryParams {
8585
let clamped_wait = base_wait
8686
.min(self.max_wait_time)
8787
.max(self.min_wait_time + 1);
88-
let jittered_wait = rand::thread_rng().gen_range(self.min_wait_time..clamped_wait);
88+
let jittered_wait = rand::rng().random_range(self.min_wait_time..clamped_wait);
8989
Duration::from_millis(jittered_wait)
9090
}
9191
}
@@ -121,7 +121,7 @@ impl SlotsRefreshRateLimit {
121121
pub(crate) fn wait_duration(&self) -> Duration {
122122
let duration_jitter = match self.max_jitter_milli {
123123
0 => Duration::from_millis(0),
124-
_ => Duration::from_millis(rand::thread_rng().gen_range(0..self.max_jitter_milli)),
124+
_ => Duration::from_millis(rand::rng().random_range(0..self.max_jitter_milli)),
125125
};
126126
self.interval_duration.add(duration_jitter)
127127
}

glide-core/redis-rs/redis/src/cluster_routing.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1420,8 +1420,8 @@ impl Route {
14201420

14211421
/// Choose a random slot from `0..SLOT_SIZE` (excluding)
14221422
fn random_slot() -> u16 {
1423-
let mut rng = rand::thread_rng();
1424-
rng.gen_range(0..crate::cluster_topology::SLOT_SIZE)
1423+
let mut rng = rand::rng();
1424+
rng.random_range(0..crate::cluster_topology::SLOT_SIZE)
14251425
}
14261426

14271427
#[cfg(test)]

glide-core/redis-rs/redis/src/connection.rs

+24-3
Original file line numberDiff line numberDiff line change
@@ -898,8 +898,29 @@ pub(crate) fn create_rustls_config(
898898
not(feature = "tls-native-tls"),
899899
not(feature = "tls-rustls-webpki-roots")
900900
))]
901-
for cert in load_native_certs()? {
902-
root_store.add(cert)?;
901+
let native_certs = load_native_certs();
902+
#[cfg(all(
903+
feature = "tls-rustls",
904+
not(feature = "tls-native-tls"),
905+
not(feature = "tls-rustls-webpki-roots")
906+
))]
907+
if native_certs.errors.is_empty() {
908+
native_certs
909+
.certs
910+
.iter()
911+
.map(|der| root_store.add(der.to_owned()))
912+
.collect::<Result<(), _>>()?;
913+
} else {
914+
fail!((
915+
ErrorKind::InvalidClientConfig,
916+
"Unable to load native certificates",
917+
native_certs
918+
.errors
919+
.iter()
920+
.map(|e| e.to_string())
921+
.collect::<Vec<_>>()
922+
.join("\n")
923+
));
903924
}
904925

905926
let config = rustls::ClientConfig::builder();
@@ -939,7 +960,7 @@ pub(crate) fn create_rustls_config(
939960
config
940961
.dangerous()
941962
.set_certificate_verifier(Arc::new(NoCertificateVerification {
942-
supported: rustls::crypto::ring::default_provider()
963+
supported: rustls::crypto::aws_lc_rs::default_provider()
943964
.signature_verification_algorithms,
944965
}));
945966

glide-core/redis-rs/redis/src/sentinel.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ fn is_replica_valid(replica_info: &HashMap<String, String>) -> bool {
207207

208208
/// Generates a random value in the 0..max range.
209209
fn random_replica_index(max: NonZeroUsize) -> usize {
210-
rand::thread_rng().gen_range(0..max.into())
210+
rand::rng().random_range(0..max.into())
211211
}
212212

213213
fn try_connect_to_first_replica(

glide-core/redis-rs/redis/tests/test_cluster_scan.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ mod test_cluster_scan_async {
7474
) -> RoutingInfo {
7575
let mut cluster_conn = cluster.async_connection(None).await;
7676
let distribution_clone = slot_distribution.clone();
77-
let index_of_random_node = rand::thread_rng().gen_range(0..slot_distribution.len());
77+
let index_of_random_node = rand::rng().random_range(0..slot_distribution.len());
7878
let random_node = distribution_clone.get(index_of_random_node).unwrap();
7979
let random_node_route_info = RoutingInfo::SingleNode(SingleNodeRoutingInfo::ByAddress {
8080
host: random_node.1.clone(),

glide-core/src/client/standalone_client.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use crate::retry_strategies::RetryStrategy;
99
use futures::{future, stream, StreamExt};
1010
use logger_core::log_debug;
1111
use logger_core::log_warn;
12-
use rand::Rng;
1312
use redis::aio::ConnectionLike;
1413
use redis::cluster_routing::{self, is_readonly_cmd, ResponsePolicy, Routable, RoutingInfo};
1514
use redis::{PushInfo, RedisError, RedisResult, Value};
@@ -129,7 +128,7 @@ impl StandaloneClient {
129128
let tls_mode = connection_request.tls_mode;
130129
let node_count = connection_request.addresses.len();
131130
// randomize pubsub nodes, maybe a batter option is to always use the primary
132-
let pubsub_node_index = rand::thread_rng().gen_range(0..node_count);
131+
let pubsub_node_index = rand::random_range(0..node_count);
133132
let pubsub_addr = &connection_request.addresses[pubsub_node_index];
134133
let discover_az = matches!(
135134
connection_request.read_from,

glide-core/src/glide_paths.rs

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
use logger_core::{log_info, log_warn};
2+
use std::path::{Path, PathBuf};
3+
4+
pub const GLIDE_BASE_FOLDER_NAME: &str = "GLIDE";
5+
pub const SOCKET_FILE_NAME: &str = "glide-socket.sock";
6+
7+
/// For ergonomics: Converts a PathBuf into a string.
8+
fn pathbuf_to_string(pathbuf: &Path) -> String {
9+
pathbuf.as_os_str().to_string_lossy().to_string()
10+
}
11+
12+
/// For ergonomics: Converts a string into a PathBuf.
13+
fn string_to_pathbuf(string: &str) -> PathBuf {
14+
PathBuf::from(string)
15+
}
16+
17+
/// GlidePaths struct.
18+
/// Contains the path to the file, primarily in order to manage the socket file.
19+
/// The socket file is used to communicate with the GLIDE server.
20+
pub struct GlidePaths {
21+
pub glide_file: PathBuf,
22+
}
23+
/// GlidePaths struct implementation.
24+
impl GlidePaths {
25+
/// Creates a new GlidePaths struct with the given file path.
26+
pub fn new(glide_file: PathBuf) -> Self {
27+
GlidePaths { glide_file }
28+
}
29+
30+
/// Create a new GlidePaths struct from string paths.
31+
pub fn from_file_path(glide_file: &str) -> Self {
32+
GlidePaths {
33+
glide_file: string_to_pathbuf(glide_file),
34+
}
35+
}
36+
37+
/// Create a new GlidePaths struct from string file name.
38+
pub fn from_file_name(file_name: &str) -> Self {
39+
GlidePaths {
40+
glide_file: Self::glide_sock_dir_path().join(file_name),
41+
}
42+
}
43+
44+
/// Returns the path to the temporary directory where the socket file is stored.
45+
fn glide_sock_dir_path() -> PathBuf {
46+
let path = std::env::temp_dir()
47+
.join(GLIDE_BASE_FOLDER_NAME)
48+
.join(std::process::id().to_string());
49+
if !path.exists() {
50+
std::fs::create_dir_all(&path)
51+
.map_err(|e| {
52+
log_warn(
53+
"Socket directory creation",
54+
format!("Error creating socket directory: {e}"),
55+
)
56+
})
57+
.ok();
58+
}
59+
path
60+
}
61+
62+
/// Returns the path to the socket file as PathBuf.
63+
pub fn glide_sock_file_path() -> PathBuf {
64+
Self::glide_sock_dir_path().join(SOCKET_FILE_NAME)
65+
}
66+
67+
/// Removes the socket file.
68+
pub fn remove_glide_file(&self) -> Result<(), std::io::Error> {
69+
let results = std::fs::remove_file(self.glide_file.clone());
70+
if let Err(e) = results {
71+
log_warn(
72+
"Socket file removal",
73+
format!("Error removing socket file: {e}"),
74+
);
75+
return Err(e);
76+
}
77+
log_info("Socket file removal", "Socket file removed successfully.");
78+
Ok(())
79+
}
80+
81+
/// Get the glide_file as a string.
82+
pub fn glide_file(&self) -> String {
83+
pathbuf_to_string(&self.glide_file)
84+
}
85+
86+
/// Replace the glide_file with a new path.
87+
pub fn set_glide_file_string(&mut self, glide_file: &str) {
88+
self.glide_file = string_to_pathbuf(&glide_file);
89+
}
90+
}
91+
92+
impl Default for GlidePaths {
93+
fn default() -> Self {
94+
GlidePaths {
95+
glide_file: Self::glide_sock_file_path(),
96+
}
97+
}
98+
}

glide-core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ pub use client::ConnectionRequest;
1616
pub mod cluster_scan_container;
1717
pub mod request_type;
1818
pub use telemetrylib::Telemetry;
19+
pub mod glide_paths;

0 commit comments

Comments
 (0)