Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove problematic rate limiter middle for now and add test for api #41

Merged
merged 3 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 0 additions & 105 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ url = "2.5.0"
futures = "0.3.30"
sha256 = "1.5.0"
tower = { version = "0.4.13", features = ["full"] }
tower_governor = { version = "0.3.2", features = ["axum"] }
governor = { version = "0.6.0" }
tower-http = { version = "0.5.2", features = ["cors", "compression-full", "trace"] }
alloy = { git = "https://github.com/alloy-rs/alloy", rev = "17633df", features = [
"sol-types",
Expand Down
29 changes: 6 additions & 23 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ use crate::controller::{ControllerCommands, ControllerInterface, DBQuery};
use crate::rules::Results;
use crate::storage;
use crate::types::PremintTypes;
use alloy_signer::k256::sha2::digest::typenum::Quot;
use axum::error_handling::HandleErrorLayer;
use axum::extract::State;
use axum::http::StatusCode;
use axum::middleware::from_fn_with_state;
use axum::routing::{get, post};
use axum::{Json, Router};
use governor::{Quota, RateLimiter};
use serde::Serialize;
use sqlx::SqlitePool;
use std::num::NonZeroU32;
use std::time::Duration;
use tokio::net::TcpListener;
use tower::limit::RateLimitLayer;
use tower::{BoxError, ServiceBuilder};
use tower_governor::governor::GovernorConfigBuilder;
use tower_governor::GovernorLayer;

#[derive(Clone)]
pub struct AppState {
Expand All @@ -32,6 +34,7 @@ impl AppState {
.await
.unwrap();
let db = recv.await.unwrap().expect("Failed to get db");

Self {
db,
controller,
Expand All @@ -41,23 +44,6 @@ impl AppState {
}

pub fn router_with_defaults(config: &Config) -> Router<AppState> {
let governor_conf = Box::new(
GovernorConfigBuilder::default()
.per_second(1)
.burst_size(config.rate_limit_rps)
.finish()
.unwrap(),
);
// rate limiter annoyingly needs a process to clean up the state
// a separate background task to clean up
let governor_limiter = governor_conf.limiter().clone();
tokio::spawn(async move {
loop {
tokio::time::sleep(Duration::from_secs(60)).await;
governor_limiter.retain_recent();
}
});

Router::new()
.route("/health", get(health))
.route("/list-all", get(list_all))
Expand All @@ -75,12 +61,9 @@ pub fn router_with_defaults(config: &Config) -> Router<AppState> {
.layer(tower_http::cors::CorsLayer::new().allow_origin(tower_http::cors::Any))
.layer(tower_http::compression::CompressionLayer::new().gzip(true)),
)
.layer(GovernorLayer {
config: Box::leak(governor_conf),
})
}

fn with_admin_routes(state: AppState, router: Router<AppState>) -> Router<AppState> {
pub fn with_admin_routes(state: AppState, router: Router<AppState>) -> Router<AppState> {
let admin = Router::new()
.route("/admin/node", get(admin::node_info))
.route("/admin/add-peer", post(admin::add_peer))
Expand Down
1 change: 0 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ pub struct Config {
}

impl Config {
#[cfg(test)]
pub fn test_default() -> Self {
Config {
seed: rand::random(),
Expand Down
39 changes: 39 additions & 0 deletions tests/api_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#[cfg(test)]
mod api_test {
use axum::body::Body;
use axum::extract::Request;
use axum::http::StatusCode;
use axum::Router;
use mintpool::api;
use mintpool::api::{with_admin_routes, AppState};

use mintpool::config::Config;
use mintpool::rules::RulesEngine;
use mintpool::run::start_p2p_services;
use tower::ServiceExt;

#[tokio::test]
async fn test_routes() {
let mut config = Config::test_default();
config.api_port = 1111;

let router = make_test_router(&config).await;

let res = router
.oneshot(Request::get("/health").body(Body::empty()).unwrap())
.await
.unwrap();
assert_eq!(res.status(), StatusCode::OK);
}

async fn make_test_router(config: &Config) -> Router {
let mut rules = RulesEngine::new(config);
rules.add_default_rules();
let ctl = start_p2p_services(config, rules).await.unwrap();

let router = api::router_with_defaults(config);
let state = AppState::from(config, ctl.clone()).await;

with_admin_routes(state.clone(), router).with_state(state)
}
}
Loading