Unverified Commit dd665f96 authored by Simo Lin's avatar Simo Lin Committed by GitHub
Browse files

[router] upgrade rand to latest version (#9017)

parent 3817a37d
......@@ -16,7 +16,7 @@ tower-http = { version = "0.6", features = ["trace", "compression-gzip", "cors",
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
bytes = "1.8.0"
rand = "0.8.5"
rand = "0.9.2"
reqwest = { version = "0.12.8", features = ["stream", "blocking", "json"] }
futures-util = "0.3"
futures = "0.3"
......
use axum::{extract::Request, http::HeaderValue, response::Response};
use rand::Rng;
use std::sync::Arc;
use std::time::Instant;
use tower::{Layer, Service};
......@@ -18,13 +19,12 @@ fn generate_request_id(path: &str) -> String {
};
// Generate a random string similar to OpenAI's format
let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let mut rng = rand::rng();
let random_part: String = (0..24)
.map(|_| {
let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
chars
.chars()
.nth(rand::random::<usize>() % chars.len())
.unwrap()
let idx = rng.random_range(0..chars.len());
chars.chars().nth(idx).unwrap()
})
.collect();
......
......@@ -55,13 +55,13 @@ impl LoadBalancingPolicy for PowerOfTwoPolicy {
}
// Select two random workers
let mut rng = rand::thread_rng();
let idx1 = rng.gen_range(0..healthy_indices.len());
let mut idx2 = rng.gen_range(0..healthy_indices.len());
let mut rng = rand::rng();
let idx1 = rng.random_range(0..healthy_indices.len());
let mut idx2 = rng.random_range(0..healthy_indices.len());
// Ensure we pick two different workers
while idx2 == idx1 {
idx2 = rng.gen_range(0..healthy_indices.len());
idx2 = rng.random_range(0..healthy_indices.len());
}
let worker_idx1 = healthy_indices[idx1];
......
......@@ -29,8 +29,8 @@ impl LoadBalancingPolicy for RandomPolicy {
return None;
}
let mut rng = rand::thread_rng();
let random_idx = rng.gen_range(0..healthy_indices.len());
let mut rng = rand::rng();
let random_idx = rng.random_range(0..healthy_indices.len());
let worker = workers[healthy_indices[random_idx]].url();
RouterMetrics::record_processed_request(worker);
......
......@@ -661,9 +661,9 @@ impl Tree {
// Unit tests
#[cfg(test)]
mod tests {
use rand::distributions::Alphanumeric;
use rand::distributions::DistString;
use rand::thread_rng;
use rand::distr::Alphanumeric;
use rand::distr::SampleString;
use rand::rng as thread_rng;
use rand::Rng;
use std::thread;
use std::time::Instant;
......@@ -1256,27 +1256,27 @@ mod tests {
for thread_id in 0..4 {
let tree = Arc::clone(&tree);
let handle = thread::spawn(move || {
let mut rng = rand::thread_rng();
let mut rng = rand::rng();
let tenant = format!("tenant{}", thread_id + 1);
let prefix = format!("prefix{}", thread_id);
while start_time.elapsed() < test_duration {
// Random decision: match or insert (70% match, 30% insert)
if rng.gen_bool(0.7) {
if rng.random_bool(0.7) {
// Perform match operation
let random_len = rng.gen_range(3..10);
let random_len = rng.random_range(3..10);
let search_str = format!("{}{}", prefix, random_string(random_len));
let (_matched, _) = tree.prefix_match(&search_str);
} else {
// Perform insert operation
let random_len = rng.gen_range(5..15);
let random_len = rng.random_range(5..15);
let insert_str = format!("{}{}", prefix, random_string(random_len));
tree.insert(&insert_str, &tenant);
// println!("Thread {} inserted: {}", thread_id, insert_str);
}
// Small random sleep to vary timing
thread::sleep(Duration::from_millis(rng.gen_range(10..100)));
thread::sleep(Duration::from_millis(rng.random_range(10..100)));
}
});
handles.push(handle);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment