-
-
Notifications
You must be signed in to change notification settings - Fork 2
[SUGGESTION] ENH: streamline controller dependency injection in routes and tests #66
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
Open
MarcosMulari
wants to merge
11
commits into
RocketPy-Team:master
Choose a base branch
from
MarcosMulari:di-controllers
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
897afee
ENH: Add dependency injection for controller singletons
MarcosMulari 81d64f5
ENH: Refactor flight routes to use dependency injection
MarcosMulari a85b9cb
ENH: Refactor rocket routes to use dependency injection
MarcosMulari a75f8d2
ENH: Refactor motor routes to use dependency injection
MarcosMulari 9a7f0d7
ENH: Refactor environment routes to use dependency injection
MarcosMulari b6f27b4
ENH: update route tests to use dependency injection with cache clearing
MarcosMulari 24a0dfc
DOC: add Windows uvloop compatibility notice and Docker recommendatio…
MarcosMulari 7980854
Apply formatting suggestions from code review
GabrielBarberini c212b9f
Update tests/unit/test_routes/test_environments_route.py
GabrielBarberini 472dd21
Update src/routes/rocket.py
GabrielBarberini d8b43e3
Update tests/unit/test_routes/test_motors_route.py
GabrielBarberini File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| from functools import lru_cache | ||
| from typing import Annotated | ||
|
|
||
| from fastapi import Depends | ||
|
|
||
| from src.controllers.rocket import RocketController | ||
| from src.controllers.motor import MotorController | ||
| from src.controllers.environment import EnvironmentController | ||
| from src.controllers.flight import FlightController | ||
|
|
||
| @lru_cache(maxsize=1) | ||
| def get_rocket_controller() -> RocketController: | ||
| """ | ||
| Provides a singleton RocketController instance. | ||
|
|
||
| The controller is stateless and can be safely reused across requests. | ||
| Using lru_cache ensures thread-safe singleton behavior. | ||
|
|
||
| Returns: | ||
| RocketController: Shared controller instance for rocket operations. | ||
| """ | ||
| return RocketController() | ||
|
|
||
|
|
||
| @lru_cache(maxsize=1) | ||
| def get_motor_controller() -> MotorController: | ||
| """ | ||
| Provides a singleton MotorController instance. | ||
|
|
||
| Returns: | ||
| MotorController: Shared controller instance for motor operations. | ||
| """ | ||
| return MotorController() | ||
|
|
||
|
|
||
| @lru_cache(maxsize=1) | ||
| def get_environment_controller() -> EnvironmentController: | ||
| """ | ||
| Provides a singleton EnvironmentController instance. | ||
|
|
||
| Returns: | ||
| EnvironmentController: Shared controller instance for environment operations. | ||
| """ | ||
| return EnvironmentController() | ||
|
|
||
|
|
||
| @lru_cache(maxsize=1) | ||
| def get_flight_controller() -> FlightController: | ||
| """ | ||
| Provides a singleton FlightController instance. | ||
|
|
||
| Returns: | ||
| FlightController: Shared controller instance for flight operations. | ||
| """ | ||
| return FlightController() | ||
|
|
||
| RocketControllerDep = Annotated[RocketController, Depends(get_rocket_controller)] | ||
| MotorControllerDep = Annotated[MotorController, Depends(get_motor_controller)] | ||
| EnvironmentControllerDep = Annotated[ | ||
| EnvironmentController, Depends(get_environment_controller) | ||
| ] | ||
| FlightControllerDep = Annotated[FlightController, Depends(get_flight_controller)] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MarcosMulari what would you say to the idea of using simple @cache instead?
Since there are no args here maxsize=1 will act as a Noop and cache === lru_cache(maxsize=None)