Background task that reads persisted block notifications from the execution layer and updates the shared atomic counter. Reconnects automatically on error. If `initial_connection` is `None`, the task begins by connecting via `reconnect()`.
(
endpoint: SubscriptionEndpoint,
shared: Arc<SharedState>,
initial_connection: Option<(Client, jsonrpsee::core::client::Subscription<BlockNumHash>)>,
)
| 299 | /// |
| 300 | /// If `initial_connection` is `None`, the task begins by connecting via `reconnect()`. |
| 301 | async fn background_reader( |
| 302 | endpoint: SubscriptionEndpoint, |
| 303 | shared: Arc<SharedState>, |
| 304 | initial_connection: Option<(Client, jsonrpsee::core::client::Subscription<BlockNumHash>)>, |
| 305 | ) { |
| 306 | let (mut client, mut subscription) = match initial_connection { |
| 307 | Some(conn) => conn, |
| 308 | None => reconnect(&endpoint).await, |
| 309 | }; |
| 310 | |
| 311 | loop { |
| 312 | match subscription.next().await { |
| 313 | Some(Ok(block)) => { |
| 314 | // If this is the first notification received since re-establishing |
| 315 | // a connection, transition to ACTIVE |
| 316 | let prev_status = shared |
| 317 | .subscription_status |
| 318 | .swap(SUBSCRIPTION_STATUS_ACTIVE, Ordering::Release); |
| 319 | if prev_status != SUBSCRIPTION_STATUS_ACTIVE { |
| 320 | info!( |
| 321 | persisted_block = block.number, |
| 322 | "Persistence meter: subscription now active" |
| 323 | ); |
| 324 | } |
| 325 | let prev = shared |
| 326 | .last_persisted_block |
| 327 | .fetch_max(block.number, Ordering::Release); |
| 328 | // Only notify if we've advanced, in case they arrive out-of-order |
| 329 | if block.number > prev { |
| 330 | shared.notify.notify_waiters(); |
| 331 | } |
| 332 | debug!( |
| 333 | persisted_block = block.number, |
| 334 | "Persistence meter: received persisted block notification" |
| 335 | ); |
| 336 | } |
| 337 | Some(Err(error)) => { |
| 338 | warn!( |
| 339 | %error, |
| 340 | "Persistence meter: subscription errored; reconnecting" |
| 341 | ); |
| 342 | shared |
| 343 | .subscription_status |
| 344 | .store(SUBSCRIPTION_STATUS_RECONNECTING, Ordering::Release); |
| 345 | shared.notify.notify_waiters(); |
| 346 | drop(subscription); |
| 347 | drop(client); |
| 348 | (client, subscription) = reconnect(&endpoint).await; |
| 349 | } |
| 350 | None => { |
| 351 | warn!("Persistence meter: subscription closed; reconnecting"); |
| 352 | shared |
| 353 | .subscription_status |
| 354 | .store(SUBSCRIPTION_STATUS_RECONNECTING, Ordering::Release); |
| 355 | shared.notify.notify_waiters(); |
| 356 | drop(subscription); |
| 357 | drop(client); |
| 358 | (client, subscription) = reconnect(&endpoint).await; |