WHAT’S NEW • controller/ratio_sync.go – Deleted unused local structs (TestResult, DifferenceItem, SyncableChannel). – Centralised config with constants: defaultTimeoutSeconds, defaultEndpoint, maxConcurrentFetches, ratioTypes. – Replaced magic numbers; added semaphore-based concurrency limit and shared http.Client (with TLS & Expect-Continue timeouts). – Added comprehensive error handling and context-aware logging via common.Log* helpers. – Checked DB errors from GetChannelsByIds; early-return on failures or empty upstream list. – Removed custom-channel support; logic now relies solely on ChannelIDs. – Minor clean-ups: import grouping, string trimming, endpoint normalisation. • dto/ratio_sync.go – Simplified UpstreamRequest: dropped unused CustomChannels field. WHY These improvements harden the ratio-sync endpoint for production use by preventing silent failures, controlling resource usage, and making behaviour configurable and observable. HOW No business logic change—only structural refactor, logging, and safeguards—so existing API contracts (aside from removed custom_channels) remain intact.
49 lines
1.4 KiB
Go
49 lines
1.4 KiB
Go
package dto
|
||
|
||
// UpstreamDTO 提交到后端同步倍率的上游渠道信息
|
||
// Endpoint 可以为空,后端会默认使用 /api/ratio_config
|
||
// BaseURL 必须以 http/https 开头,不要以 / 结尾
|
||
// 例如: https://api.example.com
|
||
// Endpoint: /api/ratio_config
|
||
// 提交示例:
|
||
// {
|
||
// "name": "openai",
|
||
// "base_url": "https://api.openai.com",
|
||
// "endpoint": "/ratio_config"
|
||
// }
|
||
|
||
type UpstreamDTO struct {
|
||
Name string `json:"name" binding:"required"`
|
||
BaseURL string `json:"base_url" binding:"required"`
|
||
Endpoint string `json:"endpoint"`
|
||
}
|
||
|
||
type UpstreamRequest struct {
|
||
ChannelIDs []int64 `json:"channel_ids"`
|
||
Timeout int `json:"timeout"`
|
||
}
|
||
|
||
// TestResult 上游测试连通性结果
|
||
type TestResult struct {
|
||
Name string `json:"name"`
|
||
Status string `json:"status"`
|
||
Error string `json:"error,omitempty"`
|
||
}
|
||
|
||
// DifferenceItem 差异项
|
||
// Current 为本地值,可能为 nil
|
||
// Upstreams 为各渠道的上游值,具体数值 / "same" / nil
|
||
|
||
type DifferenceItem struct {
|
||
Current interface{} `json:"current"`
|
||
Upstreams map[string]interface{} `json:"upstreams"`
|
||
}
|
||
|
||
// SyncableChannel 可同步的渠道信息(base_url 不为空)
|
||
|
||
type SyncableChannel struct {
|
||
ID int `json:"id"`
|
||
Name string `json:"name"`
|
||
BaseURL string `json:"base_url"`
|
||
Status int `json:"status"`
|
||
} |