Skip to content

Commit ef0ed2a

Browse files
authored
refactor: add connection info (#547)
1 parent 45022ea commit ef0ed2a

File tree

8 files changed

+41
-33
lines changed

8 files changed

+41
-33
lines changed

Cargo.lock

Lines changed: 10 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/service/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ tap_core.workspace = true
4040
uuid.workspace = true
4141
alloy.workspace = true
4242
typed-builder.workspace = true
43-
tower_governor = "0.4.3"
44-
governor = "0.6.0"
43+
tower_governor = { version = "0.5.0", features = ["axum"] }
44+
governor = "0.8.0"
4545
tower-http = { version = "0.6.2", features = [
4646
"auth",
4747
"cors",

crates/service/src/service.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use std::time::Duration;
4+
use std::{net::SocketAddr, time::Duration};
55

66
use anyhow::anyhow;
77
use axum::{extract::Request, serve, ServiceExt};
@@ -119,7 +119,7 @@ pub async fn run() -> anyhow::Result<()> {
119119
let app = router.create_router().await?;
120120
let router = NormalizePath::trim_trailing_slash(app);
121121
//
122-
let service = ServiceExt::<Request>::into_make_service(router);
122+
let service = ServiceExt::<Request>::into_make_service_with_connect_info::<SocketAddr>(router);
123123
Ok(serve(listener, service)
124124
.with_graceful_shutdown(shutdown_handler())
125125
.await?)

crates/service/src/service/router.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use reqwest::Method;
2626
use tap_core::{manager::Manager, receipt::checks::CheckList};
2727
use tower::ServiceBuilder;
2828
use tower_governor::{
29-
governor::GovernorConfigBuilder, key_extractor::PeerIpKeyExtractor, GovernorLayer,
29+
governor::GovernorConfigBuilder, key_extractor::SmartIpKeyExtractor, GovernorLayer,
3030
};
3131
use tower_http::{
3232
auth::AsyncRequireAuthorizationLayer,
@@ -434,12 +434,13 @@ impl ServiceRouter {
434434
fn create_rate_limiter(
435435
burst_per_millisecond: u64,
436436
burst_size: u32,
437-
) -> GovernorLayer<PeerIpKeyExtractor, NoOpMiddleware<QuantaInstant>> {
437+
) -> GovernorLayer<SmartIpKeyExtractor, NoOpMiddleware<QuantaInstant>> {
438438
GovernorLayer {
439439
config: Arc::new(
440440
GovernorConfigBuilder::default()
441441
.per_millisecond(burst_per_millisecond)
442442
.burst_size(burst_size)
443+
.key_extractor(SmartIpKeyExtractor)
443444
.finish()
444445
.expect("Failed to set up rate limiting"),
445446
),

crates/service/tests/router_test.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use std::time::Duration;
4+
use std::{net::SocketAddr, time::Duration};
55

66
use alloy::primitives::Address;
7-
use axum::{body::to_bytes, http::Request};
7+
use axum::{body::to_bytes, extract::ConnectInfo, http::Request, Extension};
88
use axum_extra::headers::Header;
99
use indexer_config::{BlockchainConfig, GraphNodeConfig, IndexerConfig, NonZeroGRT};
1010
use indexer_monitor::EscrowAccounts;
@@ -95,7 +95,20 @@ async fn full_integration_test(database: PgPool) {
9595
.allocations(allocations)
9696
.build();
9797

98-
let mut app = router.create_router().await.unwrap();
98+
let socket_info = Extension(ConnectInfo(SocketAddr::from(([0, 0, 0, 0], 1337))));
99+
let mut app = router.create_router().await.unwrap().layer(socket_info);
100+
101+
let res = app
102+
.call(Request::get("/").body(String::new()).unwrap())
103+
.await
104+
.unwrap();
105+
106+
assert_eq!(res.status(), StatusCode::OK);
107+
108+
let graphql_response = res.into_body();
109+
let bytes = to_bytes(graphql_response, usize::MAX).await.unwrap();
110+
let res = String::from_utf8(bytes.into()).unwrap();
111+
insta::assert_snapshot!(res);
99112

100113
let receipt = create_signed_receipt(
101114
SignedReceiptRequest::builder()

crates/service/tests/snapshots/router_test__full_integration_test-2.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ source: crates/service/tests/router_test.rs
33
expression: res
44
snapshot_kind: text
55
---
6-
{"message":"No Tap receipt was found in the request"}
6+
{"graphQLResponse":"\n {\n \"data\": {\n \"graphNetwork\": {\n \"currentEpoch\": 960\n }\n }\n }\n ","attestation":null}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
source: crates/service/tests/router_test.rs
3+
expression: res
4+
snapshot_kind: text
5+
---
6+
{"message":"No Tap receipt was found in the request"}

crates/service/tests/snapshots/router_test__full_integration_test.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ source: crates/service/tests/router_test.rs
33
expression: res
44
snapshot_kind: text
55
---
6-
{"graphQLResponse":"\n {\n \"data\": {\n \"graphNetwork\": {\n \"currentEpoch\": 960\n }\n }\n }\n ","attestation":null}
6+
Service is up and running

0 commit comments

Comments
 (0)