Files
tokens-reef/backend/internal/config/gateway.go
User a4eb4d4c3a refactor(config): split config.go into modular files
Split the monolithic config.go (~120KB) into focused modules:
- auth.go: JWT, TOTP, Turnstile, RateLimit configs
- billing.go: Billing and Pricing configs
- database.go: Database and Redis configs
- gateway.go: Gateway and Upstream configs
- gateway_sub.go: Gateway sub-configurations
- ops_and_cache.go: Ops and Cache configs
- platforms.go: Platform-specific configs
- security.go: Security-related configs
- server.go: Server configuration
- config_defaults.go: Default values
- config_defaults_detail.go: Detailed defaults
- config_helpers.go: Helper functions
- config_validate.go: Validation logic
- config_validate_gateway.go: Gateway validation

This improves:
- Code maintainability and readability
- Faster compilation (smaller files)
- Easier navigation and debugging
- Better separation of concerns
2026-04-17 07:22:55 +08:00

106 lines
6.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package config
import "time"
// GatewayConfig API网关相关配置
type GatewayConfig struct {
ResponseHeaderTimeout int `mapstructure:"response_header_timeout"` // 等待上游响应头超时0=无超时
MaxBodySize int64 `mapstructure:"max_body_size"` // 请求体最大字节数
UpstreamResponseReadMaxBytes int64 `mapstructure:"upstream_response_read_max_bytes"` // 非流式上游响应体读取上限
ProxyProbeResponseReadMaxBytes int64 `mapstructure:"proxy_probe_response_read_max_bytes"` // 代理探测响应读取上限
GeminiDebugResponseHeaders bool `mapstructure:"gemini_debug_response_headers"`
ConnectionPoolIsolation string `mapstructure:"connection_pool_isolation"` // proxy/account/account_proxy
ForceCodexCLI bool `mapstructure:"force_codex_cli"`
ForcedCodexInstructionsTemplateFile string `mapstructure:"forced_codex_instructions_template_file"`
ForcedCodexInstructionsTemplate string `mapstructure:"-"`
OpenAIPassthroughAllowTimeoutHeaders bool `mapstructure:"openai_passthrough_allow_timeout_headers"`
OpenAIWS GatewayOpenAIWSConfig `mapstructure:"openai_ws"`
// HTTP 上游连接池配置
MaxIdleConns int `mapstructure:"max_idle_conns"`
MaxIdleConnsPerHost int `mapstructure:"max_idle_conns_per_host"`
MaxConnsPerHost int `mapstructure:"max_conns_per_host"`
IdleConnTimeoutSeconds int `mapstructure:"idle_conn_timeout_seconds"`
MaxUpstreamClients int `mapstructure:"max_upstream_clients"`
ClientIdleTTLSeconds int `mapstructure:"client_idle_ttl_seconds"`
ConcurrencySlotTTLMinutes int `mapstructure:"concurrency_slot_ttl_minutes"`
SessionIdleTimeoutMinutes int `mapstructure:"session_idle_timeout_seconds"`
StreamDataIntervalTimeout int `mapstructure:"stream_data_interval_timeout"` // 流数据间隔超时0=禁用
StreamKeepaliveInterval int `mapstructure:"stream_keepalive_interval"` // 流式 keepalive 间隔0=禁用
MaxLineSize int `mapstructure:"max_line_size"` // SSE 单行最大字节数
LogUpstreamErrorBody bool `mapstructure:"log_upstream_error_body"`
LogUpstreamErrorBodyMaxBytes int `mapstructure:"log_upstream_error_body_max_bytes"`
InjectBetaForAPIKey bool `mapstructure:"inject_beta_for_apikey"`
FailoverOn400 bool `mapstructure:"failover_on_400"`
// Sora 专用配置
SoraMaxBodySize int64 `mapstructure:"sora_max_body_size"`
SoraStreamTimeoutSeconds int `mapstructure:"sora_stream_timeout_seconds"`
SoraRequestTimeoutSeconds int `mapstructure:"sora_request_timeout_seconds"`
SoraStreamMode string `mapstructure:"sora_stream_mode"`
SoraModelFilters SoraModelFiltersConfig `mapstructure:"sora_model_filters"`
SoraMediaRequireAPIKey bool `mapstructure:"sora_media_require_api_key"`
SoraMediaSigningKey string `mapstructure:"sora_media_signing_key"`
SoraMediaSignedURLTTLSeconds int `mapstructure:"sora_media_signed_url_ttl_seconds"`
MaxAccountSwitches int `mapstructure:"max_account_switches"`
MaxAccountSwitchesGemini int `mapstructure:"max_account_switches_gemini"`
AntigravityFallbackCooldownMinutes int `mapstructure:"antigravity_fallback_cooldown_minutes"`
Scheduling GatewaySchedulingConfig `mapstructure:"scheduling"`
TLSFingerprint TLSFingerprintConfig `mapstructure:"tls_fingerprint"`
UsageRecord GatewayUsageRecordConfig `mapstructure:"usage_record"`
UserGroupRateCacheTTLSeconds int `mapstructure:"user_group_rate_cache_ttl_seconds"`
ModelsListCacheTTLSeconds int `mapstructure:"models_list_cache_ttl_seconds"`
UserMessageQueue UserMessageQueueConfig `mapstructure:"user_message_queue"`
}
// UserMessageQueueConfig 用户消息串行队列配置
type UserMessageQueueConfig struct {
Mode string `mapstructure:"mode"` // serialize/throttle/""
Enabled bool `mapstructure:"enabled"` // 向后兼容
LockTTLMs int `mapstructure:"lock_ttl_ms"`
WaitTimeoutMs int `mapstructure:"wait_timeout_ms"`
MinDelayMs int `mapstructure:"min_delay_ms"`
MaxDelayMs int `mapstructure:"max_delay_ms"`
CleanupIntervalSeconds int `mapstructure:"cleanup_interval_seconds"`
}
func (c *UserMessageQueueConfig) WaitTimeout() time.Duration {
if c.WaitTimeoutMs <= 0 { return 30 * time.Second }
return time.Duration(c.WaitTimeoutMs) * time.Millisecond
}
func (c *UserMessageQueueConfig) GetEffectiveMode() string {
if c.Mode == UMQModeSerialize || c.Mode == UMQModeThrottle { return c.Mode }
if c.Enabled { return UMQModeSerialize }
return ""
}
// GatewaySchedulingConfig 账号调度相关配置
type GatewaySchedulingConfig struct {
StickySessionMaxWaiting int `mapstructure:"sticky_session_max_waiting"`
StickySessionWaitTimeout time.Duration `mapstructure:"sticky_session_wait_timeout"`
FallbackWaitTimeout time.Duration `mapstructure:"fallback_wait_timeout"`
FallbackMaxWaiting int `mapstructure:"fallback_max_waiting"`
FallbackSelectionMode string `mapstructure:"fallback_selection_mode"`
LoadBatchEnabled bool `mapstructure:"load_batch_enabled"`
SnapshotMGetChunkSize int `mapstructure:"snapshot_mget_chunk_size"`
SnapshotWriteChunkSize int `mapstructure:"snapshot_write_chunk_size"`
SlotCleanupInterval time.Duration `mapstructure:"slot_cleanup_interval"`
DbFallbackEnabled bool `mapstructure:"db_fallback_enabled"`
DbFallbackTimeoutSeconds int `mapstructure:"db_fallback_timeout_seconds"`
DbFallbackMaxQPS int `mapstructure:"db_fallback_max_qps"`
OutboxPollIntervalSeconds int `mapstructure:"outbox_poll_interval_seconds"`
OutboxLagWarnSeconds int `mapstructure:"outbox_lag_warn_seconds"`
OutboxLagRebuildSeconds int `mapstructure:"outbox_lag_rebuild_seconds"`
OutboxLagRebuildFailures int `mapstructure:"outbox_lag_rebuild_failures"`
OutboxBacklogRebuildRows int `mapstructure:"outbox_backlog_rebuild_rows"`
FullRebuildIntervalSeconds int `mapstructure:"full_rebuild_interval_seconds"`
}