@@ -4,8 +4,10 @@ use crate::tasks::oauth::Authenticator;
4
4
use oauth2:: TokenResponse ;
5
5
use reqwest:: Url ;
6
6
use serde:: { Deserialize , Serialize } ;
7
- use tokio:: sync:: mpsc:: { UnboundedReceiver , unbounded_channel} ;
7
+ use tokio:: sync:: mpsc:: { UnboundedReceiver , UnboundedSender , unbounded_channel} ;
8
8
use tokio:: task:: JoinHandle ;
9
+ use tokio:: time;
10
+ use tracing:: { Instrument , debug, trace} ;
9
11
use zenith_types:: ZenithEthBundle ;
10
12
11
13
/// Holds a bundle from the cache with a unique ID and a Zenith bundle.
@@ -69,22 +71,46 @@ impl BundlePoller {
69
71
Ok ( resp. bundles )
70
72
}
71
73
72
- /// Spawns a task that sends bundles it finds to its channel sender.
73
- pub fn spawn ( mut self ) -> ( UnboundedReceiver < Bundle > , JoinHandle < ( ) > ) {
74
- let ( outbound, inbound) = unbounded_channel ( ) ;
75
- let jh = tokio:: spawn ( async move {
76
- loop {
77
- if let Ok ( bundles) = self . check_bundle_cache ( ) . await {
74
+ async fn task_future ( mut self , outbound : UnboundedSender < Bundle > ) {
75
+ loop {
76
+ let span = tracing:: debug_span!( "BundlePoller::loop" , url = %self . config. tx_pool_url) ;
77
+
78
+ // Enter the span for the next check.
79
+ let _guard = span. enter ( ) ;
80
+
81
+ // Check this here to avoid making the web request if we know
82
+ // we don't need the results.
83
+ if outbound. is_closed ( ) {
84
+ trace ! ( "No receivers left, shutting down" ) ;
85
+ break ;
86
+ }
87
+ // exit the span after the check.
88
+ drop ( _guard) ;
89
+
90
+ match self . check_bundle_cache ( ) . instrument ( span. clone ( ) ) . await {
91
+ Ok ( bundles) => {
78
92
tracing:: debug!( count = ?bundles. len( ) , "found bundles" ) ;
79
93
for bundle in bundles. into_iter ( ) {
80
94
if let Err ( err) = outbound. send ( bundle) {
81
95
tracing:: error!( err = ?err, "Failed to send bundle - channel is dropped" ) ;
82
96
}
83
97
}
84
98
}
85
- tokio:: time:: sleep ( tokio:: time:: Duration :: from_millis ( self . poll_interval_ms ) ) . await ;
99
+ // If fetching was an error, we log and continue. We expect
100
+ // these to be transient network issues.
101
+ Err ( e) => {
102
+ debug ! ( error = %e, "Error fetching bundles" ) ;
103
+ }
86
104
}
87
- } ) ;
105
+ time:: sleep ( time:: Duration :: from_millis ( self . poll_interval_ms ) ) . await ;
106
+ }
107
+ }
108
+
109
+ /// Spawns a task that sends bundles it finds to its channel sender.
110
+ pub fn spawn ( self ) -> ( UnboundedReceiver < Bundle > , JoinHandle < ( ) > ) {
111
+ let ( outbound, inbound) = unbounded_channel ( ) ;
112
+
113
+ let jh = tokio:: spawn ( self . task_future ( outbound) ) ;
88
114
89
115
( inbound, jh)
90
116
}
0 commit comments