1
- use std:: { collections:: HashMap , sync:: Arc } ;
1
+ use std:: { collections:: HashMap , sync:: Arc , time :: Duration } ;
2
2
3
3
use once_cell:: sync:: Lazy ;
4
4
use ott_balancer_protocol:: collector:: BalancerState ;
@@ -74,11 +74,17 @@ impl Collector {
74
74
}
75
75
76
76
pub fn handle_discovery ( & mut self , msg : ServiceDiscoveryMsg ) {
77
+ debug ! (
78
+ "Balancer discovery: {} added, {} removed" ,
79
+ msg. added. len( ) ,
80
+ msg. removed. len( )
81
+ ) ;
77
82
self . balancers . retain ( |conf| !msg. removed . contains ( conf) ) ;
78
83
self . balancers . extend ( msg. added ) ;
79
84
}
80
85
81
86
pub async fn collect ( & mut self ) -> anyhow:: Result < SystemState > {
87
+ info ! ( "Collecting system state" ) ;
82
88
let client = reqwest:: Client :: new ( ) ;
83
89
let mut states = vec ! [ ] ;
84
90
for conf in & self . balancers {
@@ -89,6 +95,7 @@ impl Collector {
89
95
let resp = client
90
96
. get ( url)
91
97
. header ( "Authorization" , format ! ( "Bearer {}" , self . balancer_api_key) )
98
+ . timeout ( Duration :: from_secs ( 3 ) )
92
99
. send ( )
93
100
. await ?;
94
101
if !resp. status ( ) . is_success ( ) {
@@ -105,6 +112,7 @@ impl Collector {
105
112
if self . stream_tasks . contains_key ( conf) {
106
113
continue ;
107
114
}
115
+ debug ! ( "Starting stream from balancer: {:?}" , & conf) ;
108
116
let _conf = conf. clone ( ) ;
109
117
let events_tx = self . events_tx . clone ( ) ;
110
118
let _balancer_api_key = self . balancer_api_key . clone ( ) ;
@@ -121,6 +129,9 @@ impl Collector {
121
129
self . stream_tasks . insert ( conf. clone ( ) , task) ;
122
130
}
123
131
132
+ // cleanup stream tasks that have finished
133
+ self . stream_tasks . retain ( |_conf, task| !task. is_finished ( ) ) ;
134
+
124
135
Ok ( SystemState ( states) )
125
136
}
126
137
@@ -149,25 +160,28 @@ impl Collector {
149
160
loop {
150
161
tokio:: select! {
151
162
msg = ws. next( ) => {
152
- if let Some ( Ok ( msg) ) = msg {
153
- if msg. is_close( ) {
154
- break ;
155
- }
156
- let msg = msg. to_string( ) ;
157
- if !should_send( & msg) {
158
- continue ;
159
- }
160
- if let Err ( err) = events_tx. try_send( msg) {
161
- match err {
162
- tokio:: sync:: mpsc:: error:: TrySendError :: Full ( _) => {
163
- warn!( "Event bus is full, dropping event" ) ;
164
- }
165
- tokio:: sync:: mpsc:: error:: TrySendError :: Closed ( _) => {
166
- warn!( "Event bus is closed, stopping stream" ) ;
167
- break ;
163
+ match msg {
164
+ Some ( Ok ( msg) ) => {
165
+ if msg. is_close( ) {
166
+ break ;
167
+ }
168
+ let msg = msg. to_string( ) ;
169
+ if !should_send( & msg) {
170
+ continue ;
171
+ }
172
+ if let Err ( err) = events_tx. try_send( msg) {
173
+ match err {
174
+ tokio:: sync:: mpsc:: error:: TrySendError :: Full ( _) => {
175
+ warn!( "Event bus is full, dropping event" ) ;
176
+ }
177
+ tokio:: sync:: mpsc:: error:: TrySendError :: Closed ( _) => {
178
+ warn!( "Event bus is closed, stopping stream" ) ;
179
+ break ;
180
+ }
168
181
}
169
182
}
170
183
}
184
+ _ => break ,
171
185
}
172
186
}
173
187
else => {
0 commit comments