Skip to content

Commit 74eff2f

Browse files
committed
refactor: use arc instead of leak
Signed-off-by: Gustavo Inacio <[email protected]>
1 parent 7ae8fc1 commit 74eff2f

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

crates/service/src/middleware/auth.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub use tap::tap_receipt_authorize;
1111

1212
#[cfg(test)]
1313
mod tests {
14+
use std::sync::Arc;
1415
use std::time::Duration;
1516

1617
use alloy::primitives::{address, Address};
@@ -34,11 +35,11 @@ mod tests {
3435
pgpool: PgPool,
3536
) -> impl Service<Request<Body>, Response = Response<Body>, Error = impl std::fmt::Debug> {
3637
let context = IndexerTapContext::new(pgpool.clone(), TAP_EIP712_DOMAIN.clone()).await;
37-
let tap_manager = Box::leak(Box::new(Manager::new(
38+
let tap_manager = Arc::new(Manager::new(
3839
TAP_EIP712_DOMAIN.clone(),
3940
context,
4041
CheckList::empty(),
41-
)));
42+
));
4243

4344
let registry = prometheus::Registry::new();
4445
let metric = Box::leak(Box::new(

crates/service/src/middleware/auth/tap.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use crate::{error::IndexerServiceError, middleware::prometheus_metrics::MetricLa
3030
///
3131
/// Requires SignedReceipt, MetricLabels and Arc<Context> extensions
3232
pub fn tap_receipt_authorize<T, B>(
33-
tap_manager: &'static Manager<T>,
33+
tap_manager: Arc<Manager<T>>,
3434
failed_receipt_metric: &'static prometheus::CounterVec,
3535
) -> impl AsyncAuthorizeRequest<
3636
B,
@@ -40,17 +40,18 @@ pub fn tap_receipt_authorize<T, B>(
4040
> + Clone
4141
+ Send
4242
where
43-
T: ReceiptStore + Sync + Send,
43+
T: ReceiptStore + Sync + Send + 'static,
4444
B: Send,
4545
{
46-
|request: Request<B>| {
46+
move |request: Request<B>| {
4747
let receipt = request.extensions().get::<SignedReceipt>().cloned();
4848
// load labels from previous middlewares
4949
let labels = request.extensions().get::<MetricLabels>().cloned();
5050
// load context from previous middlewares
5151
let ctx = request.extensions().get::<Arc<Context>>().cloned();
52+
let tap_manager = tap_manager.clone();
5253

53-
async {
54+
async move {
5455
let execute = || async {
5556
let receipt = receipt.ok_or(IndexerServiceError::ReceiptNotFound)?;
5657
// Verify the receipt and store it in the database
@@ -148,11 +149,11 @@ mod tests {
148149
}
149150
}
150151

151-
let manager = Box::leak(Box::new(Manager::new(
152+
let manager = Arc::new(Manager::new(
152153
TAP_EIP712_DOMAIN.clone(),
153154
context,
154155
CheckList::new(vec![Arc::new(MyCheck)]),
155-
)));
156+
));
156157
let tap_auth = tap_receipt_authorize(manager, metric);
157158
let authorization_middleware = AsyncRequireAuthorizationLayer::new(tap_auth);
158159

crates/service/src/service/router.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,11 @@ impl ServiceRouter {
289289
)
290290
.await;
291291
// Returned static Manager
292-
Box::leak(Box::new(Manager::new(
292+
Arc::new(Manager::new(
293293
self.domain_separator.clone(),
294294
indexer_context,
295295
CheckList::new(checks),
296-
)))
296+
))
297297
};
298298

299299
let mut handler = post(request_handler);

0 commit comments

Comments
 (0)