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