@@ -18,10 +18,12 @@ pub enum Router {
18
18
worker_urls : Arc < RwLock < Vec < String > > > ,
19
19
current_index : AtomicUsize ,
20
20
timeout_secs : u64 ,
21
+ interval_secs : u64 ,
21
22
} ,
22
23
Random {
23
24
worker_urls : Arc < RwLock < Vec < String > > > ,
24
25
timeout_secs : u64 ,
26
+ interval_secs : u64 ,
25
27
} ,
26
28
CacheAware {
27
29
/*
@@ -92,6 +94,7 @@ pub enum Router {
92
94
balance_abs_threshold : usize ,
93
95
balance_rel_threshold : f32 ,
94
96
timeout_secs : u64 ,
97
+ interval_secs : u64 ,
95
98
_eviction_thread : Option < thread:: JoinHandle < ( ) > > ,
96
99
} ,
97
100
}
@@ -100,9 +103,11 @@ pub enum Router {
100
103
pub enum PolicyConfig {
101
104
RandomConfig {
102
105
timeout_secs : u64 ,
106
+ interval_secs : u64 ,
103
107
} ,
104
108
RoundRobinConfig {
105
109
timeout_secs : u64 ,
110
+ interval_secs : u64 ,
106
111
} ,
107
112
CacheAwareConfig {
108
113
cache_threshold : f32 ,
@@ -111,31 +116,50 @@ pub enum PolicyConfig {
111
116
eviction_interval_secs : u64 ,
112
117
max_tree_size : usize ,
113
118
timeout_secs : u64 ,
119
+ interval_secs : u64 ,
114
120
} ,
115
121
}
116
122
117
123
impl Router {
118
124
pub fn new ( worker_urls : Vec < String > , policy_config : PolicyConfig ) -> Result < Self , String > {
119
- // Get timeout from policy config
120
- let timeout_secs = match & policy_config {
121
- PolicyConfig :: RandomConfig { timeout_secs } => * timeout_secs,
122
- PolicyConfig :: RoundRobinConfig { timeout_secs } => * timeout_secs,
123
- PolicyConfig :: CacheAwareConfig { timeout_secs, .. } => * timeout_secs,
125
+ // Get timeout and interval from policy config
126
+ let ( timeout_secs, interval_secs) = match & policy_config {
127
+ PolicyConfig :: RandomConfig {
128
+ timeout_secs,
129
+ interval_secs,
130
+ } => ( * timeout_secs, * interval_secs) ,
131
+ PolicyConfig :: RoundRobinConfig {
132
+ timeout_secs,
133
+ interval_secs,
134
+ } => ( * timeout_secs, * interval_secs) ,
135
+ PolicyConfig :: CacheAwareConfig {
136
+ timeout_secs,
137
+ interval_secs,
138
+ ..
139
+ } => ( * timeout_secs, * interval_secs) ,
124
140
} ;
125
141
126
142
// Wait until all workers are healthy
127
- Self :: wait_for_healthy_workers ( & worker_urls, timeout_secs, 10 ) ?;
143
+ Self :: wait_for_healthy_workers ( & worker_urls, timeout_secs, interval_secs ) ?;
128
144
129
145
// Create router based on policy...
130
146
Ok ( match policy_config {
131
- PolicyConfig :: RandomConfig { timeout_secs } => Router :: Random {
147
+ PolicyConfig :: RandomConfig {
148
+ timeout_secs,
149
+ interval_secs,
150
+ } => Router :: Random {
132
151
worker_urls : Arc :: new ( RwLock :: new ( worker_urls) ) ,
133
152
timeout_secs,
153
+ interval_secs,
134
154
} ,
135
- PolicyConfig :: RoundRobinConfig { timeout_secs } => Router :: RoundRobin {
155
+ PolicyConfig :: RoundRobinConfig {
156
+ timeout_secs,
157
+ interval_secs,
158
+ } => Router :: RoundRobin {
136
159
worker_urls : Arc :: new ( RwLock :: new ( worker_urls) ) ,
137
160
current_index : std:: sync:: atomic:: AtomicUsize :: new ( 0 ) ,
138
161
timeout_secs,
162
+ interval_secs,
139
163
} ,
140
164
PolicyConfig :: CacheAwareConfig {
141
165
cache_threshold,
@@ -144,6 +168,7 @@ impl Router {
144
168
eviction_interval_secs,
145
169
max_tree_size,
146
170
timeout_secs,
171
+ interval_secs,
147
172
} => {
148
173
let mut running_queue = HashMap :: new ( ) ;
149
174
for url in & worker_urls {
@@ -195,6 +220,7 @@ impl Router {
195
220
balance_abs_threshold,
196
221
balance_rel_threshold,
197
222
timeout_secs,
223
+ interval_secs,
198
224
_eviction_thread : Some ( eviction_thread) ,
199
225
}
200
226
}
@@ -594,11 +620,22 @@ impl Router {
594
620
}
595
621
596
622
pub async fn add_worker ( & self , worker_url : & str ) -> Result < String , String > {
597
- let interval_secs = 10 ; // check every 10 seconds
598
- let timeout_secs = match self {
599
- Router :: Random { timeout_secs, .. } => * timeout_secs,
600
- Router :: RoundRobin { timeout_secs, .. } => * timeout_secs,
601
- Router :: CacheAware { timeout_secs, .. } => * timeout_secs,
623
+ let ( timeout_secs, interval_secs) = match self {
624
+ Router :: Random {
625
+ timeout_secs,
626
+ interval_secs,
627
+ ..
628
+ } => ( * timeout_secs, * interval_secs) ,
629
+ Router :: RoundRobin {
630
+ timeout_secs,
631
+ interval_secs,
632
+ ..
633
+ } => ( * timeout_secs, * interval_secs) ,
634
+ Router :: CacheAware {
635
+ timeout_secs,
636
+ interval_secs,
637
+ ..
638
+ } => ( * timeout_secs, * interval_secs) ,
602
639
} ;
603
640
604
641
let start_time = std:: time:: Instant :: now ( ) ;
0 commit comments