@@ -21,7 +21,6 @@ use crate::api::public::pubsub::MatcherUpsertError;
21
21
pub type UpdateBroadcastCache = HashMap < Uuid , broadcast:: Sender < Bytes > > ;
22
22
pub type SharedUpdateBroadcastCache = Arc < TokioRwLock < UpdateBroadcastCache > > ;
23
23
24
- const MAX_UNSUB_TIME : Duration = Duration :: from_secs ( 120 ) ;
25
24
// this should be a fraction of the MAX_UNSUB_TIME
26
25
const RECEIVERS_CHECK_INTERVAL : Duration = Duration :: from_secs ( 30 ) ;
27
26
@@ -107,88 +106,51 @@ pub async fn process_update_channel(
107
106
) {
108
107
let mut buf = BytesMut :: new ( ) ;
109
108
110
- let mut deadline = if tx. receiver_count ( ) == 0 {
111
- Some ( Box :: pin ( tokio:: time:: sleep ( MAX_UNSUB_TIME ) ) )
112
- } else {
113
- None
114
- } ;
115
-
116
- // even if there are no more subscribers
109
+ // interval check for receivers
117
110
// useful for queries that don't change often so we can cleanup...
118
111
let mut subs_check = tokio:: time:: interval ( RECEIVERS_CHECK_INTERVAL ) ;
119
112
120
113
loop {
121
- let deadline_check = async {
122
- if let Some ( sleep) = deadline. as_mut ( ) {
123
- sleep. await
124
- } else {
125
- futures:: future:: pending ( ) . await
126
- }
127
- } ;
128
-
129
- let notify_evt = tokio:: select! {
114
+ tokio:: select! {
130
115
biased;
131
- Some ( query_evt) = evt_rx. recv( ) => query_evt,
132
- _ = deadline_check => {
133
- if tx. receiver_count( ) == 0 {
134
- info!( sub_id = %id, "All listeners for subscription are gone and didn't come back within {MAX_UNSUB_TIME:?}" ) ;
135
- break ;
136
- }
137
-
138
- // reset the deadline if there are receivers!
139
- deadline = None ;
140
- continue ;
116
+ Some ( query_evt) = evt_rx. recv( ) => {
117
+ match make_query_event_bytes( & mut buf, & query_evt) {
118
+ Ok ( b) => {
119
+ if tx. send( b) . is_err( ) {
120
+ break ;
121
+ }
122
+ } ,
123
+ Err ( e) => {
124
+ match make_query_event_bytes( & mut buf, & NotifyEvent :: Error ( e. to_compact_string( ) ) ) {
125
+ Ok ( b) => {
126
+ let _ = tx. send( b) ;
127
+ }
128
+ Err ( e) => {
129
+ warn!( update_id = %id, "failed to send error in update channel: {e}" ) ;
130
+ }
131
+ }
132
+ break ;
133
+ }
134
+ } ;
141
135
} ,
142
136
_ = subs_check. tick( ) => {
143
137
if tx. receiver_count( ) == 0 {
144
- if deadline. is_none( ) {
145
- deadline = Some ( Box :: pin( tokio:: time:: sleep( MAX_UNSUB_TIME ) ) ) ;
146
- }
147
- } else {
148
- deadline = None ;
138
+ break ;
149
139
} ;
150
- continue ;
151
140
} ,
152
- else => {
153
- break ;
154
- }
155
141
} ;
156
-
157
- let is_still_active = match make_query_event_bytes ( & mut buf, & notify_evt) {
158
- Ok ( b) => tx. send ( b) . is_ok ( ) ,
159
- Err ( e) => {
160
- match make_query_event_bytes ( & mut buf, & NotifyEvent :: Error ( e. to_compact_string ( ) ) ) {
161
- Ok ( b) => {
162
- let _ = tx. send ( b) ;
163
- }
164
- Err ( e) => {
165
- warn ! ( update_id = %id, "error sending error: {e}" ) ;
166
- }
167
- }
168
- break ;
169
- }
170
- } ;
171
-
172
- if is_still_active {
173
- deadline = None ;
174
- } else {
175
- debug ! ( sub_id = %id, "no active listeners to receive subscription event: {notify_evt:?}" ) ;
176
- if deadline. is_none ( ) {
177
- deadline = Some ( Box :: pin ( tokio:: time:: sleep ( MAX_UNSUB_TIME ) ) ) ;
178
- }
179
- }
180
142
}
181
143
182
- warn ! ( sub_id = %id, "subscription query channel done" ) ;
144
+ warn ! ( sub_id = %id, "updates channel done" ) ;
183
145
184
146
// remove and get handle from the agent's "matchers"
185
147
let handle = match updates. remove ( & id) {
186
148
Some ( h) => {
187
- info ! ( update_id = %id, "Removed update from process_update_channel" ) ;
149
+ info ! ( update_id = %id, "Removed update handle from process_update_channel" ) ;
188
150
h
189
151
}
190
152
None => {
191
- warn ! ( update_id = %id, "subscription handle was already gone. odd!" ) ;
153
+ warn ! ( update_id = %id, "update handle was already gone. odd!" ) ;
192
154
return ;
193
155
}
194
156
} ;
@@ -234,7 +196,7 @@ async fn forward_update_bytes_to_body_sender(
234
196
buf. extend_from_slice( & event_buf) ;
235
197
if buf. len( ) >= 64 * 1024 {
236
198
if let Err ( e) = tx. send_data( buf. split( ) . freeze( ) ) . await {
237
- warn!( update_id = %update. id( ) , "could not forward subscription query event to receiver: {e}" ) ;
199
+ warn!( update_id = %update. id( ) , "could not forward update query event to receiver: {e}" ) ;
238
200
return ;
239
201
}
240
202
} ;
@@ -265,8 +227,8 @@ async fn forward_update_bytes_to_body_sender(
265
227
}
266
228
} ,
267
229
_ = update. cancelled( ) => {
268
- // info!(update_id = %update.id(), "update cancelled, aborting forwarding bytes to subscriber");
269
- // return;
230
+ info!( update_id = %update. id( ) , "update cancelled, aborting forwarding bytes to subscriber" ) ;
231
+ return ;
270
232
} ,
271
233
_ = & mut tripwire => {
272
234
break ;
0 commit comments