Skip to content

Commit edc1d2c

Browse files
authored
fix: refresh database before closing allocation (#411)
1 parent 1e4a3cd commit edc1d2c

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

tap-agent/src/agent/sender_allocation.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ impl Actor for SenderAllocation {
179179
}
180180

181181
// update unaggregated_fees
182-
state.unaggregated_fees = state.initialize_unaggregated_receipts().await?;
182+
state.unaggregated_fees = state.recalculate_all_unaggregated_fees().await?;
183183

184184
sender_account_ref.cast(SenderAccountMessage::UpdateReceiptFees(
185185
allocation_id,
@@ -212,6 +212,20 @@ impl Actor for SenderAllocation {
212212
allocation_id = %state.allocation_id,
213213
"Closing SenderAllocation, triggering last rav",
214214
);
215+
loop {
216+
match state.recalculate_all_unaggregated_fees().await {
217+
Ok(value) => {
218+
state.unaggregated_fees = value;
219+
break;
220+
}
221+
Err(err) => {
222+
error!(
223+
error = %err,
224+
"There was an error while calculating the last unaggregated receipts. Retrying in 30 seconds...");
225+
tokio::time::sleep(Duration::from_secs(30)).await;
226+
}
227+
}
228+
}
215229
// Request a RAV and mark the allocation as final.
216230
while state.unaggregated_fees.value > 0 {
217231
if let Err(err) = state.request_rav().await {
@@ -380,7 +394,7 @@ impl SenderAllocationState {
380394
})
381395
}
382396

383-
async fn initialize_unaggregated_receipts(&self) -> Result<UnaggregatedReceipts> {
397+
async fn recalculate_all_unaggregated_fees(&self) -> Result<UnaggregatedReceipts> {
384398
self.calculate_fee_until_last_id(i64::MAX).await
385399
}
386400

@@ -1412,7 +1426,7 @@ pub mod tests {
14121426
}
14131427

14141428
// calculate unaggregated fee
1415-
let total_unaggregated_fees = state.initialize_unaggregated_receipts().await.unwrap();
1429+
let total_unaggregated_fees = state.recalculate_all_unaggregated_fees().await.unwrap();
14161430

14171431
// Check that the unaggregated fees are correct.
14181432
assert_eq!(total_unaggregated_fees.value, 45u128);
@@ -1467,7 +1481,7 @@ pub mod tests {
14671481
.unwrap();
14681482
}
14691483

1470-
let total_unaggregated_fees = state.initialize_unaggregated_receipts().await.unwrap();
1484+
let total_unaggregated_fees = state.recalculate_all_unaggregated_fees().await.unwrap();
14711485

14721486
// Check that the unaggregated fees are correct.
14731487
assert_eq!(total_unaggregated_fees.value, 35u128);

0 commit comments

Comments
 (0)