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

Upfront initialization of path params & other expensive work #1041

Open
patricktyndall opened this issue Jan 14, 2025 · 2 comments
Open

Upfront initialization of path params & other expensive work #1041

patricktyndall opened this issue Jan 14, 2025 · 2 comments

Comments

@patricktyndall
Copy link

Is your feature request related to a problem? Please describe.
See discussion here. Even after the library is initialized, there is a separate "cold start" which occurs on the first request. For those of us in a serverless architecture, this makes the impact of this library more diffcult to measure / solve with standard tools like provisioned concurrency - a "warm" container might still incur this cost.

  • As seen in the linked issue, the pathParamsMiddleware alone can incur a >400ms cost when it initializes. requestMiddleware also implicated.

Describe the solution you'd like
An option to .init() an instance before the first request, to frontload all that expensive work. E.g. to imperatively trigger the initialization that is done here and here.

Describe alternatives you've considered
I guess the alternative would be to trigger a "dummy" request against the app instance? Seems ugly.

Additional context
More context / converstaion available on the discussion here

Thanks! Keywords: Lambda, serverless, cold start, initialization, init, performance, latency

@cdimascio
Copy link
Owner

cdimascio commented Jan 19, 2025

hi @patricktyndall, here is a thought to eagerly invoke the middleware on startup using the eov as-is. The idea is to initialize the middleware as normal. however, proactively trigger it using a mock request

Let me know how it goes

// Create a mock request which we'll eager invoke to force the middleware to run
// We arbitrarily choose GET /v1/pets
const mockReq = {
  method: 'GET',
  url: '/v1/pets',
  originalUrl: '/v1/pets',
  path: '/v1/pets',
  headers: {},
  body: {},
  query: {},
  params: {},
  app,
};

// 1. Initialize middleware
const oav = OpenApiValidator.middleware({ apiSpec });

// 2. Proactively run the validator middleware using the mockReq
oav.forEach(o => o(mockReq, {}, (req, res, next) => console.log('Middleware has finished running')));

// 3. Install the OpenApiValidator on your express app
app.use(oav);

// NOTE: 2. and 3. can be done in any order

@patricktyndall
Copy link
Author

Thanks @cdimascio that's helpful. I'll let you know when give it a try.

I personally still think this library should expose this / this should not be necessary in future (I'd argue it should be the lib's default behavior, an option, or at least an officially documented approach).

I really appreciate the example code and will incorporate soon. This will definitely help eliminate variables when profiling other middleware slowness.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants