33
44use anyhow:: anyhow;
55use bigdecimal:: num_bigint:: BigInt ;
6+ use lazy_static:: lazy_static;
7+ use prometheus:: { register_gauge, Gauge } ;
68use sqlx:: { types:: BigDecimal , PgPool } ;
79use tap_core:: {
810 manager:: adapters:: ReceiptStore ,
911 receipt:: { state:: Checking , ReceiptWithState } ,
1012} ;
1113use thegraph_core:: alloy:: { hex:: ToHexExt , sol_types:: Eip712Domain } ;
12- use tokio:: { sync:: mpsc:: Receiver , task:: JoinHandle } ;
14+ use tokio:: { sync:: mpsc:: UnboundedReceiver , task:: JoinHandle } ;
1315use tokio_util:: sync:: CancellationToken ;
1416
1517use super :: { AdapterError , IndexerTapContext } ;
1618
19+ lazy_static ! {
20+ pub static ref RECEIPTS_IN_QUEUE : Gauge = register_gauge!(
21+ "indexer_receipts_in_queue_total" ,
22+ "Total receipts waiting to be stored" ,
23+ )
24+ . unwrap( ) ;
25+ }
26+
1727#[ derive( Clone ) ]
1828pub struct InnerContext {
1929 pub pgpool : PgPool ,
@@ -74,7 +84,7 @@ impl InnerContext {
7484impl IndexerTapContext {
7585 pub fn spawn_store_receipt_task (
7686 inner_context : InnerContext ,
77- mut receiver : Receiver < DatabaseReceipt > ,
87+ mut receiver : UnboundedReceiver < DatabaseReceipt > ,
7888 cancelation_token : CancellationToken ,
7989 ) -> JoinHandle < ( ) > {
8090 const BUFFER_SIZE : usize = 100 ;
@@ -83,7 +93,8 @@ impl IndexerTapContext {
8393 let mut buffer = Vec :: with_capacity ( BUFFER_SIZE ) ;
8494 tokio:: select! {
8595 biased;
86- _ = receiver. recv_many( & mut buffer, BUFFER_SIZE ) => {
96+ size = receiver. recv_many( & mut buffer, BUFFER_SIZE ) => {
97+ RECEIPTS_IN_QUEUE . sub( size as f64 ) ;
8798 if let Err ( e) = inner_context. store_receipts( buffer) . await {
8899 tracing:: error!( "Failed to store receipts: {}" , e) ;
89100 }
@@ -104,10 +115,11 @@ impl ReceiptStore for IndexerTapContext {
104115 receipt : ReceiptWithState < Checking > ,
105116 ) -> Result < u64 , Self :: AdapterError > {
106117 let db_receipt = DatabaseReceipt :: from_receipt ( receipt, & self . domain_separator ) ?;
107- self . receipt_producer . send ( db_receipt) . await . map_err ( |e| {
118+ self . receipt_producer . send ( db_receipt) . map_err ( |e| {
108119 tracing:: error!( "Failed to queue receipt for storage: {}" , e) ;
109120 anyhow ! ( e)
110121 } ) ?;
122+ RECEIPTS_IN_QUEUE . inc ( ) ;
111123
112124 // We don't need receipt_ids
113125 Ok ( 0 )
0 commit comments