config.py
Overview
The config.py file in the InfiniFlow project is responsible for configuring and managing the lifecycle of the container execution environment within the application. It primarily sets up asynchronous lifecycle management for the FastAPI application, including initialization and teardown of container pools used to execute sandboxed tasks.
The file reads environment variables to configure global timeout settings and the size of the container executor pool. It provides an asynchronous context manager _lifespan that integrates with FastAPI's lifespan events, ensuring container resources are properly initialized before the app starts serving requests and cleaned up on shutdown.
Detailed Explanation of Components
Constants
TIMEOUT
Type:
int(duration in seconds or an internal duration format)Description: Represents the global timeout duration for sandbox execution tasks.
Source: Read from environment variable
SANDBOX_TIMEOUT, defaults to"10s"if not set.Parsing: Uses the utility function
parse_timeout_duration()to convert the string duration into a usable format.
TIMEOUT = parse_timeout_duration(os.getenv("SANDBOX_TIMEOUT", "10s"))
Functions
_lifespan(app: FastAPI)
Type: Asynchronous context manager (
asynccontextmanager)Parameters:
app: FastAPI— The FastAPI application instance whose lifecycle is being managed.
Returns: Async generator that yields control to the FastAPI lifespan event.
Description:
Manages the lifecycle of container pools used for sandboxed execution within the FastAPI app. It:Reads the pool size from the environment variable
SANDBOX_EXECUTOR_MANAGER_POOL_SIZE(default 1).Calls
init_containers(size)to initialize container instances.Logs the result of initialization.
Yields control back to FastAPI to continue application startup.
Upon shutdown, calls
teardown_containers()to cleanly release container resources.
Usage example:
app = FastAPI(lifespan=config._lifespan)
Here, the _lifespan function is passed to the FastAPI app to handle startup and shutdown lifecycle.
init()
Type: Function
Parameters: None
Returns: The
_lifespanasync context manager function.Description:
Logs the global timeout value in a human-readable format usingformat_timeout_duration(), then returns the_lifespanfunction to be used as the FastAPI lifespan manager.Usage example:
lifespan = config.init()
app = FastAPI(lifespan=lifespan)
This function is a helper to prepare the lifespan context manager while logging configuration details.
Important Implementation Details
Asynchronous Context Manager:
The use of the@asynccontextmanagerdecorator fromcontextlibenables the_lifespanfunction to be employed as an asynchronous startup/shutdown hook in FastAPI, ensuring proper container lifecycle management without blocking the event loop.Environment Configuration:
All critical parameters such as timeout duration and container pool size are configurable through environment variables, providing flexibility for deployment and testing.Container Pool Management:
The file depends on thecore.containermodule’sinit_containersandteardown_containersfunctions:init_containers(size)asynchronously initializes a pool of container executors.teardown_containers()asynchronously destroys or cleans up the container pool.
This design abstracts container management and keeps configuration and lifecycle concerns separated.
Logging:
Uses thecore.logger.loggerinstance to log important lifecycle events, including successful container initialization and global timeout settings. This aids in debugging and operational monitoring.
Interaction with Other Parts of the System
FastAPI Application:
The_lifespanasync context manager returned byinit()is intended to be passed to FastAPI’slifespanparameter, tying together container lifecycle with the web server lifecycle.Utility Module (
util):
Functionsformat_timeout_durationandparse_timeout_durationare used to handle human-readable timeout strings and convert them to internal duration formats, enhancing usability and configurability.Core Modules:
core.container: Provides container initialization and teardown logic.core.logger: Provides centralized logging capabilities.
Environment Variables:
SANDBOX_TIMEOUT: Defines the global timeout for sandbox execution tasks.SANDBOX_EXECUTOR_MANAGER_POOL_SIZE: Defines how many container executors are initialized in parallel.
Visual Diagram
flowchart TD
A[Start: FastAPI app startup] --> B[_lifespan async context manager]
B --> C[Read SANDBOX_EXECUTOR_MANAGER_POOL_SIZE]
C --> D[Call init_containers(size)]
D --> E[Log container initialization status]
E --> F[Yield control to FastAPI app running]
F --> G[App runs: handles requests using containers]
G --> H[App shutdown triggered]
H --> I[Call teardown_containers()]
I --> J[Containers cleaned up]
J --> K[End: FastAPI app shutdown]
Summary
config.py is a critical configuration and lifecycle management file that ensures container pools used for sandboxed execution are properly initialized and cleaned up in alignment with the FastAPI application's lifecycle. It leverages environment variables for configurability, uses asynchronous context management for seamless startup and shutdown, and integrates logging to provide operational insight.
By exporting a ready-to-use lifespan manager through the init() function, it cleanly encapsulates the complexity of container lifecycle management and supports scalable and robust sandbox execution within the InfiniFlow system.