Skip to content

Commit 4c85b2d

Browse files
gusinacioaasseman
authored andcommitted
refactor(tap-agent): use single http client
We want to reuse the same connection or wait for the older one in case we hit 2 rav requests in a row Signed-off-by: Gustavo Inacio <[email protected]>
1 parent cfaf02f commit 4c85b2d

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

tap-agent/src/agent/sender_allocation.rs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,12 @@ pub struct SenderAllocationState {
110110
tap_manager: TapManager,
111111
allocation_id: Address,
112112
sender: Address,
113-
sender_aggregator_endpoint: String,
114113
config: &'static config::Config,
115114
escrow_accounts: Eventual<EscrowAccounts>,
116115
domain_separator: Eip712Domain,
117116
sender_account_ref: ActorRef<SenderAccountMessage>,
117+
118+
http_client: jsonrpsee::http_client::HttpClient,
118119
}
119120

120121
pub struct SenderAllocationArgs {
@@ -151,7 +152,7 @@ impl Actor for SenderAllocation {
151152
) -> std::result::Result<Self::State, ActorProcessingErr> {
152153
let sender_account_ref = args.sender_account_ref.clone();
153154
let allocation_id = args.allocation_id;
154-
let mut state = SenderAllocationState::new(args).await;
155+
let mut state = SenderAllocationState::new(args).await?;
155156

156157
// update invalid receipts
157158
state.invalid_receipts_fees = state.calculate_invalid_receipts_fee().await?;
@@ -306,7 +307,7 @@ impl SenderAllocationState {
306307
sender_aggregator_endpoint,
307308
sender_account_ref,
308309
}: SenderAllocationArgs,
309-
) -> Self {
310+
) -> anyhow::Result<Self> {
310311
let required_checks: Vec<Arc<dyn Check + Send + Sync>> = vec![
311312
Arc::new(AllocationId::new(
312313
sender,
@@ -333,20 +334,24 @@ impl SenderAllocationState {
333334
Checks::new(required_checks),
334335
);
335336

336-
Self {
337+
let http_client = HttpClientBuilder::default()
338+
.request_timeout(Duration::from_secs(config.tap.rav_request_timeout_secs))
339+
.build(&sender_aggregator_endpoint)?;
340+
341+
Ok(Self {
337342
pgpool,
338343
tap_manager,
339344
allocation_id,
340345
sender,
341-
sender_aggregator_endpoint,
342346
config,
343347
escrow_accounts,
344348
domain_separator,
345349
sender_account_ref: sender_account_ref.clone(),
346350
unaggregated_fees: UnaggregatedReceipts::default(),
347351
invalid_receipts_fees: UnaggregatedReceipts::default(),
348352
latest_rav,
349-
}
353+
http_client,
354+
})
350355
}
351356

352357
/// Delete obsolete receipts in the DB w.r.t. the last RAV in DB, then update the tap manager
@@ -503,13 +508,9 @@ impl SenderAllocationState {
503508
),
504509
_ => e.into(),
505510
})?;
506-
let client = HttpClientBuilder::default()
507-
.request_timeout(Duration::from_secs(
508-
self.config.tap.rav_request_timeout_secs,
509-
))
510-
.build(&self.sender_aggregator_endpoint)?;
511511
let rav_response_time_start = Instant::now();
512-
let response: JsonRpcResponse<EIP712SignedMessage<ReceiptAggregateVoucher>> = client
512+
let response: JsonRpcResponse<EIP712SignedMessage<ReceiptAggregateVoucher>> = self
513+
.http_client
513514
.request(
514515
"aggregate_receipts",
515516
rpc_params!(
@@ -1242,7 +1243,7 @@ pub mod tests {
12421243
let args =
12431244
create_sender_allocation_args(pgpool.clone(), DUMMY_URL.to_string(), DUMMY_URL, None)
12441245
.await;
1245-
let state = SenderAllocationState::new(args).await;
1246+
let state = SenderAllocationState::new(args).await.unwrap();
12461247

12471248
// Add receipts to the database.
12481249
for i in 1..10 {
@@ -1264,7 +1265,7 @@ pub mod tests {
12641265
let args =
12651266
create_sender_allocation_args(pgpool.clone(), DUMMY_URL.to_string(), DUMMY_URL, None)
12661267
.await;
1267-
let state = SenderAllocationState::new(args).await;
1268+
let state = SenderAllocationState::new(args).await.unwrap();
12681269

12691270
// Add receipts to the database.
12701271
for i in 1..10 {
@@ -1292,7 +1293,7 @@ pub mod tests {
12921293
let args =
12931294
create_sender_allocation_args(pgpool.clone(), DUMMY_URL.to_string(), DUMMY_URL, None)
12941295
.await;
1295-
let state = SenderAllocationState::new(args).await;
1296+
let state = SenderAllocationState::new(args).await.unwrap();
12961297

12971298
// Add the RAV to the database.
12981299
// This RAV has timestamp 4. The sender_allocation should only consider receipts
@@ -1319,7 +1320,7 @@ pub mod tests {
13191320
let args =
13201321
create_sender_allocation_args(pgpool.clone(), DUMMY_URL.to_string(), DUMMY_URL, None)
13211322
.await;
1322-
let state = SenderAllocationState::new(args).await;
1323+
let state = SenderAllocationState::new(args).await.unwrap();
13231324

13241325
let signed_rav = create_rav(*ALLOCATION_ID_0, SIGNER.0.clone(), 4, 10);
13251326

@@ -1345,7 +1346,7 @@ pub mod tests {
13451346
let args =
13461347
create_sender_allocation_args(pgpool.clone(), DUMMY_URL.to_string(), DUMMY_URL, None)
13471348
.await;
1348-
let mut state = SenderAllocationState::new(args).await;
1349+
let mut state = SenderAllocationState::new(args).await.unwrap();
13491350

13501351
let checks = Checks::new(vec![Arc::new(FailingCheck)]);
13511352

@@ -1376,7 +1377,7 @@ pub mod tests {
13761377
let args =
13771378
create_sender_allocation_args(pgpool.clone(), DUMMY_URL.to_string(), DUMMY_URL, None)
13781379
.await;
1379-
let state = SenderAllocationState::new(args).await;
1380+
let state = SenderAllocationState::new(args).await.unwrap();
13801381

13811382
// mark rav as final
13821383
let result = state.mark_rav_last().await;

0 commit comments

Comments
 (0)