(c *gin.Context)
| 32 | } |
| 33 | |
| 34 | func FetchUpstreamRatios(c *gin.Context) { |
| 35 | var req dto.UpstreamRequest |
| 36 | if err := c.ShouldBindJSON(&req); err != nil { |
| 37 | c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": err.Error()}) |
| 38 | return |
| 39 | } |
| 40 | |
| 41 | if req.Timeout <= 0 { |
| 42 | req.Timeout = defaultTimeoutSeconds |
| 43 | } |
| 44 | |
| 45 | var upstreams []dto.UpstreamDTO |
| 46 | |
| 47 | if len(req.Upstreams) > 0 { |
| 48 | for _, u := range req.Upstreams { |
| 49 | if strings.HasPrefix(u.BaseURL, "http") { |
| 50 | if u.Endpoint == "" { |
| 51 | u.Endpoint = defaultEndpoint |
| 52 | } |
| 53 | u.BaseURL = strings.TrimRight(u.BaseURL, "/") |
| 54 | upstreams = append(upstreams, u) |
| 55 | } |
| 56 | } |
| 57 | } else if len(req.ChannelIDs) > 0 { |
| 58 | intIds := make([]int, 0, len(req.ChannelIDs)) |
| 59 | for _, id64 := range req.ChannelIDs { |
| 60 | intIds = append(intIds, int(id64)) |
| 61 | } |
| 62 | dbChannels, err := model.GetChannelsByIds(intIds) |
| 63 | if err != nil { |
| 64 | common.LogError(c.Request.Context(), "failed to query channels: "+err.Error()) |
| 65 | c.JSON(http.StatusInternalServerError, gin.H{"success": false, "message": "查询渠道失败"}) |
| 66 | return |
| 67 | } |
| 68 | for _, ch := range dbChannels { |
| 69 | if base := ch.GetBaseURL(); strings.HasPrefix(base, "http") { |
| 70 | upstreams = append(upstreams, dto.UpstreamDTO{ |
| 71 | ID: ch.Id, |
| 72 | Name: ch.Name, |
| 73 | BaseURL: strings.TrimRight(base, "/"), |
| 74 | Endpoint: "", |
| 75 | }) |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | if len(upstreams) == 0 { |
| 81 | c.JSON(http.StatusOK, gin.H{"success": false, "message": "无有效上游渠道"}) |
| 82 | return |
| 83 | } |
| 84 | |
| 85 | var wg sync.WaitGroup |
| 86 | ch := make(chan upstreamResult, len(upstreams)) |
| 87 | |
| 88 | sem := make(chan struct{}, maxConcurrentFetches) |
| 89 | |
| 90 | client := &http.Client{Transport: &http.Transport{MaxIdleConns: 100, IdleConnTimeout: 90 * time.Second, TLSHandshakeTimeout: 10 * time.Second, ExpectContinueTimeout: 1 * time.Second}} |
| 91 |
nothing calls this directly
no test coverage detected