config.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. """
  2. 性能配置汇总模块
  3. 集中管理所有性能相关配置,使用 Pydantic Settings 实现:
  4. - 支持从环境变量读取配置
  5. - 支持从 .env 文件读取配置
  6. - 使用 lru_cache 缓存配置实例
  7. 需求引用: 10.1, 10.2, 10.3, 10.4
  8. """
  9. from functools import lru_cache
  10. from typing import Optional
  11. from pydantic_settings import BaseSettings
  12. from pydantic import Field
  13. class PerformanceSettings(BaseSettings):
  14. """性能相关配置
  15. 所有配置项都有合理的默认值,可通过环境变量或 .env 文件覆盖。
  16. """
  17. # ============================================
  18. # 数据库连接池配置 (需求 10.1)
  19. # ============================================
  20. db_host: str = Field(default="localhost", description="数据库主机地址")
  21. db_port: int = Field(default=5432, description="数据库端口")
  22. db_user: str = Field(default="postgres", description="数据库用户名")
  23. db_password: str = Field(default="", description="数据库密码")
  24. db_name: str = Field(default="model_square", description="数据库名称")
  25. db_pool_size: int = Field(default=20, description="连接池大小")
  26. db_max_overflow: int = Field(default=40, description="连接池最大溢出数")
  27. db_pool_timeout: int = Field(default=30, description="连接池获取超时时间(秒)")
  28. db_pool_recycle: int = Field(default=1800, description="连接回收时间(秒)")
  29. # ============================================
  30. # Redis 配置 (需求 10.2)
  31. # ============================================
  32. redis_url: str = Field(default="redis://localhost:6379", description="Redis 连接地址")
  33. redis_password: str = Field(default="", description="Redis 密码")
  34. redis_pool_size: int = Field(default=50, description="Redis 连接池大小")
  35. cache_default_ttl: int = Field(default=300, description="缓存默认过期时间(秒)")
  36. # ============================================
  37. # 限流配置 (需求 10.3)
  38. # ============================================
  39. rate_limit_default: int = Field(default=100, description="默认限流阈值(次/窗口)")
  40. rate_limit_window: int = Field(default=60, description="限流时间窗口(秒)")
  41. # ============================================
  42. # Gunicorn/应用配置 (需求 10.4)
  43. # ============================================
  44. gunicorn_workers: int = Field(default=4, description="Gunicorn Worker 数量")
  45. app_host: str = Field(default="0.0.0.0", description="应用监听地址")
  46. app_port: int = Field(default=8010, description="应用监听端口")
  47. log_level: str = Field(default="info", description="日志级别")
  48. # ============================================
  49. # 慢查询配置 (需求 7.4)
  50. # ============================================
  51. slow_query_threshold_ms: int = Field(default=1000, description="慢查询阈值(毫秒)")
  52. # ============================================
  53. # 调试配置
  54. # ============================================
  55. debug: bool = Field(default=False, description="调试模式")
  56. # ============================================
  57. # 爬虫同步配置
  58. # ============================================
  59. crawler_base_url: str = Field(default="http://localhost:8000", description="爬虫系统地址")
  60. crawler_referer: str = Field(default="aigcspace.com", description="请求爬虫时的 Referer 域名")
  61. apikey_encrypt_key: str = Field(default="", description="爬虫 API Key 解密密钥")
  62. model_config = {
  63. "env_file": ".env",
  64. "env_file_encoding": "utf-8",
  65. "extra": "ignore" # 忽略未定义的环境变量
  66. }
  67. @property
  68. def async_database_url(self) -> str:
  69. """构建异步数据库连接 URL"""
  70. return f"postgresql+asyncpg://{self.db_user}:{self.db_password}@{self.db_host}:{self.db_port}/{self.db_name}"
  71. @property
  72. def sync_database_url(self) -> str:
  73. """构建同步数据库连接 URL"""
  74. return f"postgresql://{self.db_user}:{self.db_password}@{self.db_host}:{self.db_port}/{self.db_name}"
  75. @lru_cache()
  76. def get_performance_settings() -> PerformanceSettings:
  77. """获取性能配置单例
  78. 使用 lru_cache 确保配置只加载一次,
  79. 后续调用直接返回缓存的实例。
  80. Returns:
  81. PerformanceSettings: 性能配置实例
  82. """
  83. return PerformanceSettings()
  84. # 导出配置实例,方便直接导入使用
  85. settings = get_performance_settings()