API stability fixes and dependency modernisation#36
Merged
Conversation
Member
Author
|
I need to add |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
This PR fixes a critical stability issue where a single malformed query to
/disco/eventscould exhaust all database connections and block the entire API. It also modernises the codebase by migrating all repositories to the SQLAlchemy 2.0 query API and resolving Pydantic v2 deprecation warnings.Root cause: The endpoint had no time parameter requirement, no guard against unknown parameters, and loaded probe details for every event by default — triggering 130k individual SQL queries per request. Combined with synchronous SQLAlchemy blocking the async event loop, one bad request was enough to stall all connections.
Changes
API-wide
main.py./disco/eventsfixesstarttime,starttime__gte,starttime__lte,endtime,endtime__gte,endtime__lte); requests without one return HTTP 400.include_probe_detailsboolean parameter (defaultFalse) controls this; when enabled, a singlejoinedloadreplaces the previous 130k lazy-load queries.Database connection pool
QueuePoolwithpool_size=10,max_overflow=5,pool_timeout=30.statement_timeout=60000msto kill runaway queries and release connections.pool_pre_ping=Trueto drop stale connections before use.For more details read: https://docs.sqlalchemy.org/en/20/core/engines.html#sqlalchemy.create_engine
SQLAlchemy 2.0 migration (all 14 repositories)
db.query(Model)withselect(Model)+db.scalars()/db.scalar().query.filter()with.where(),query.count()withdb.scalar(select(func.count())...)..join(Alias, Model.rel)to.join(Model.rel.of_type(Alias)).contains_eager(rel, alias=Alias)tocontains_eager(rel.of_type(Alias))..unique()onscalars()results to deduplicate ORM objects from joined loads.Deprecation warnings
class Config: from_attributes = Truewithmodel_config = ConfigDict(from_attributes=True)across all 16 DTOs (Pydantic v2).from_orm()withmodel_validate()(Pydantic v2).declarative_base()with class-basedDeclarativeBase(SQLAlchemy 2.0).Documentation
docs/add_new_endpoint.mdwith 2.0-style repository and DTO examples.How Has This Been Tested?
Local testing.
Types of changes
Checklist