Skip to content
This repository was archived by the owner on Feb 20, 2024. It is now read-only.

Commit 97771f9

Browse files
committed
perf: initialize the router components as singleton for the lambda runtime type
1 parent 70fc9cf commit 97771f9

4 files changed

Lines changed: 16 additions & 7 deletions

File tree

src/lib/api_app/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ itertools = { workspace = true }
1919
libmath = { workspace = true }
2020
stopwatch = { workspace = true }
2121
utoipa = { workspace = true }
22+
lazy_static = "1.4.0"
2223

2324
app = { path = "./src/lib/app" }
2425
utils = { path = "./src/lib/utils" }

src/lib/api_app/src/api/routes/application.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub async fn docs(_request: ParsedRequest) -> APIResponse {
4747
}))
4848
}
4949

50-
pub async fn openapi_spec(openapi: OpenApi) -> APIResponse {
50+
pub async fn openapi_spec(openapi: &OpenApi) -> APIResponse {
5151
let json_spec_orig_serialized = openapi.to_json().expect("Failed to parse openapi spec");
5252

5353
// Inject the app specific securitySchemas to the spec as a workaround for utoipa::OpenApi zeroing the parse_meta output of derived OpenApi structs when using the security attribute

src/lib/api_app/src/api/routes/mod.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
use futures::{future::BoxFuture, FutureExt};
2+
use lazy_static::lazy_static;
3+
use utoipa::OpenApi;
4+
15
use app::{
26
responses::APIResponse,
37
router::{OpenApiRouter, ParsedRequest},
48
};
5-
use futures::{future::BoxFuture, FutureExt};
6-
use utoipa::OpenApi;
79

810
pub mod application;
911
pub mod jmf;
@@ -54,12 +56,18 @@ pub mod testbed;
5456
)]
5557
struct Api;
5658

59+
// Create a singleton instance of the router components
60+
lazy_static! {
61+
static ref OPENAPI_INSTANCE: utoipa::openapi::OpenApi = Api::openapi();
62+
static ref ROUTER_INSTANCE: Api = Api;
63+
}
64+
5765
/**
5866
* API router
5967
*/
6068
pub async fn get_router_response(parsed_request: ParsedRequest) -> APIResponse {
61-
let openapi = Api::openapi(); // @TODO: ensure as singelton
62-
let router = Api;
69+
let openapi = &*OPENAPI_INSTANCE;
70+
let router = &*ROUTER_INSTANCE;
6371

6472
match (parsed_request.method.as_str(), parsed_request.path.as_str()) {
6573
// System routes

src/lib/api_app/src/lib/app/src/router.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub trait OpenApiRouter {
2727
closure()
2828
}
2929

30-
fn handle(&self, openapi: OpenApi, parsed_request: ParsedRequest) -> Self::FutureType {
30+
fn handle(&self, openapi: &OpenApi, parsed_request: ParsedRequest) -> Self::FutureType {
3131
// Resolve the operation id
3232
let operation_id = get_openapi_operation_id(
3333
openapi,
@@ -77,7 +77,7 @@ pub fn parse_router_request(request: Request) -> ParsedRequest {
7777
pub mod openapi {
7878
use utoipa::openapi::{OpenApi, PathItem, PathItemType};
7979

80-
pub fn get_openapi_operation_id(openapi: OpenApi, method: &str, path: &str) -> String {
80+
pub fn get_openapi_operation_id(openapi: &OpenApi, method: &str, path: &str) -> String {
8181
let path = openapi.paths.get_path_item(path);
8282
match path {
8383
Some(path) => resolve_operation_id(path, method),

0 commit comments

Comments
 (0)