Use SQLAlchemy scoped_session and use a single unique session for each gRPC request
Context
We currently open multiple SQLAlchemy sessions to handle grpc requests. Every time we access the database we open a new session and close it after use, potentially opening e.g. 5-6 sessions for a single grpc request.
This could accelerate the exhaustion of database connection limits.
SQLAlchemy provides the scoped_session
and the scope is configurable.
It would be ideal for us to configure it to the scope of a single grpc request.
In order to achieve that, we need a way of identifying the grpc requests. (e.g. using a UUID). Additionally, UUID at the grpc request level could help us track the progress of a single request throughout it's fulfillment.
To Do
-
Assign UUIDs to each grpc request - See if we can use grpc interceptors (that would make it easy to just attach to the grpc server once)
- Alternatively, use decorator-interceptors, similar to how we have implemented the
authorize
functionality.
-
Store those UUIDs: - make it part of the context object we pass around if that is relatively easy
- add utility functions to read/write to/from
threading.local()
and invoke those from each thread.
Edited by Marios Hadjimichael