Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
change
sglang
Commits
dd665f96
Unverified
Commit
dd665f96
authored
Aug 09, 2025
by
Simo Lin
Committed by
GitHub
Aug 09, 2025
Browse files
[router] upgrade rand to latest version (#9017)
parent
3817a37d
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
20 additions
and
20 deletions
+20
-20
sgl-router/Cargo.toml
sgl-router/Cargo.toml
+1
-1
sgl-router/src/middleware.rs
sgl-router/src/middleware.rs
+5
-5
sgl-router/src/policies/power_of_two.rs
sgl-router/src/policies/power_of_two.rs
+4
-4
sgl-router/src/policies/random.rs
sgl-router/src/policies/random.rs
+2
-2
sgl-router/src/tree.rs
sgl-router/src/tree.rs
+8
-8
No files found.
sgl-router/Cargo.toml
View file @
dd665f96
...
...
@@ -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"
...
...
sgl-router/src/middleware.rs
View file @
dd665f96
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
();
...
...
sgl-router/src/policies/power_of_two.rs
View file @
dd665f96
...
...
@@ -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
];
...
...
sgl-router/src/policies/random.rs
View file @
dd665f96
...
...
@@ -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
);
...
...
sgl-router/src/tree.rs
View file @
dd665f96
...
...
@@ -661,9 +661,9 @@ impl Tree {
// Unit tests
#[cfg(test)]
mod
tests
{
use
rand
::
distr
ibutions
::
Alphanumeric
;
use
rand
::
distr
ibutions
::
Dist
String
;
use
rand
::
thread_rng
;
use
rand
::
distr
::
Alphanumeric
;
use
rand
::
distr
::
Sample
String
;
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
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment