Skip to content

Commit 379b71a

Browse files
committed
run check_all until scores are stable
1 parent 14186d1 commit 379b71a

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

crates/shadowsocks-service/src/local/loadbalancing/ping_balancer.rs

+29
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Load Balancer chooses server by statistic latency data collected from active probing
22
33
use std::{
4+
cmp,
45
fmt::{self, Debug, Display},
56
io,
67
iter::Iterator,
@@ -29,6 +30,7 @@ use shadowsocks::{
2930
use spin::Mutex as SpinMutex;
3031
use tokio::{
3132
io::{AsyncBufReadExt, AsyncWriteExt, BufReader},
33+
sync::Notify,
3234
task::JoinHandle,
3335
time,
3436
};
@@ -208,6 +210,7 @@ struct PingBalancerContext {
208210
max_server_rtt: Duration,
209211
check_interval: Duration,
210212
check_best_interval: Option<Duration>,
213+
best_task_notify: Notify,
211214
}
212215

213216
impl PingBalancerContext {
@@ -303,6 +306,7 @@ impl PingBalancerContext {
303306
max_server_rtt,
304307
check_interval,
305308
check_best_interval,
309+
best_task_notify: Notify::new(),
306310
};
307311

308312
balancer_context.init_score().await;
@@ -602,6 +606,27 @@ impl PingBalancerContext {
602606
}
603607

604608
async fn checker_task_all_servers(&self) {
609+
if let Some(check_best_interval) = self.check_best_interval {
610+
// Get at least 10 points to get the precise scores
611+
612+
let interval = cmp::min(check_best_interval, self.check_interval);
613+
614+
let mut count = 0;
615+
while count < EXPECTED_CHECK_POINTS_IN_CHECK_WINDOW {
616+
time::sleep(interval).await;
617+
618+
// Sleep before check.
619+
// PingBalancer already checked once when constructing
620+
self.check_once(false).await;
621+
622+
count += 1;
623+
}
624+
625+
self.best_task_notify.notify_one();
626+
627+
trace!("finished initializing server scores");
628+
}
629+
605630
loop {
606631
time::sleep(self.check_interval).await;
607632

@@ -612,6 +637,10 @@ impl PingBalancerContext {
612637
}
613638

614639
async fn checker_task_best_server(&self) {
640+
// Wait until checker_task_all_servers notify.
641+
// Because when server starts, the scores are unstable, so we have to run check_all for multiple times
642+
self.best_task_notify.notified().await;
643+
615644
let check_best_interval = self.check_best_interval.unwrap();
616645

617646
loop {

0 commit comments

Comments
 (0)