MCPcopy
hub / github.com/QuantumNous/new-api / FetchUpstreamRatios

Function FetchUpstreamRatios

controller/ratio_sync.go:142–534  ·  view source on GitHub ↗
(c *gin.Context)

Source from the content-addressed store, hash-verified

140}
141
142func FetchUpstreamRatios(c *gin.Context) {
143 var req dto.UpstreamRequest
144 if err := c.ShouldBindJSON(&req); err != nil {
145 common.SysError("failed to bind upstream request: " + err.Error())
146 c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": "请求参数格式错误"})
147 return
148 }
149
150 if req.Timeout <= 0 {
151 req.Timeout = defaultTimeoutSeconds
152 }
153
154 var upstreams []dto.UpstreamDTO
155
156 if len(req.Upstreams) > 0 {
157 for _, u := range req.Upstreams {
158 if strings.HasPrefix(u.BaseURL, "http") {
159 if u.Endpoint == "" {
160 u.Endpoint = defaultEndpoint
161 }
162 u.BaseURL = strings.TrimRight(u.BaseURL, "/")
163 upstreams = append(upstreams, u)
164 }
165 }
166 } else if len(req.ChannelIDs) > 0 {
167 intIds := make([]int, 0, len(req.ChannelIDs))
168 for _, id64 := range req.ChannelIDs {
169 intIds = append(intIds, int(id64))
170 }
171 dbChannels, err := model.GetChannelsByIds(intIds)
172 if err != nil {
173 logger.LogError(c.Request.Context(), "failed to query channels: "+err.Error())
174 c.JSON(http.StatusInternalServerError, gin.H{"success": false, "message": "查询渠道失败"})
175 return
176 }
177 for _, ch := range dbChannels {
178 if base := ch.GetBaseURL(); strings.HasPrefix(base, "http") {
179 upstreams = append(upstreams, dto.UpstreamDTO{
180 ID: ch.Id,
181 Name: ch.Name,
182 BaseURL: strings.TrimRight(base, "/"),
183 Endpoint: "",
184 })
185 }
186 }
187 }
188
189 if len(upstreams) == 0 {
190 c.JSON(http.StatusOK, gin.H{"success": false, "message": "无有效上游渠道"})
191 return
192 }
193
194 var wg sync.WaitGroup
195 ch := make(chan upstreamResult, len(upstreams))
196
197 sem := make(chan struct{}, maxConcurrentFetches)
198
199 dialer := &net.Dialer{Timeout: 10 * time.Second}

Callers

nothing calls this directly

Calls 15

SysErrorFunction · 0.92
GetChannelsByIdsFunction · 0.92
LogErrorFunction · 0.92
LogWarnFunction · 0.92
GetChannelByIdFunction · 0.92
DecodeJsonFunction · 0.92
UnmarshalFunction · 0.92
isModelsDevAPIEndpointFunction · 0.85
valueMapFunction · 0.85
closeFunction · 0.85

Tested by

no test coverage detected