MCPcopy Index your code
hub / github.com/aiprodcoder/MIXAPI / Relay

Function Relay

controller/relay.go:72–116  ·  view source on GitHub ↗
(c *gin.Context)

Source from the content-addressed store, hash-verified

70}
71
72func Relay(c *gin.Context) {
73 log.Println("===========Relay==========", c)
74 relayMode := relayconstant.Path2RelayMode(c.Request.URL.Path)
75 requestId := c.GetString(common.RequestIdKey)
76 group := c.GetString("group")
77 originalModel := c.GetString("original_model")
78 var newAPIError *types.NewAPIError
79
80 for i := 0; i <= common.RetryTimes; i++ {
81 channel, err := getChannel(c, group, originalModel, i)
82 if err != nil {
83 common.LogError(c, err.Error())
84 newAPIError = err
85 break
86 }
87
88 newAPIError = relayRequest(c, relayMode, channel)
89
90 if newAPIError == nil {
91 return // 成功处理请求,直接返回
92 }
93
94 go processChannelError(c, *types.NewChannelError(channel.Id, channel.Type, channel.Name, channel.ChannelInfo.IsMultiKey, common.GetContextKeyString(c, constant.ContextKeyChannelKey), channel.GetAutoBan()), newAPIError)
95
96 if !shouldRetry(c, newAPIError, common.RetryTimes-i) {
97 break
98 }
99 }
100 useChannel := c.GetStringSlice("use_channel")
101 if len(useChannel) > 1 {
102 retryLogStr := fmt.Sprintf("重试:%s", strings.Trim(strings.Join(strings.Fields(fmt.Sprint(useChannel)), "->"), "[]"))
103 common.LogInfo(c, retryLogStr)
104 }
105
106 if newAPIError != nil {
107 //if newAPIError.StatusCode == http.StatusTooManyRequests {
108 // common.LogError(c, fmt.Sprintf("origin 429 error: %s", newAPIError.Error()))
109 // newAPIError.SetMessage("当前分组上游负载已饱和,请稍后再试")
110 //}
111 newAPIError.SetMessage(common.MessageWithRequestId(newAPIError.Error(), requestId))
112 c.JSON(newAPIError.StatusCode, gin.H{
113 "error": newAPIError.ToOpenAIError(),
114 })
115 }
116}
117
118var upgrader = websocket.Upgrader{
119 Subprotocols: []string{"realtime"}, // WS 握手支持的协议,如果有使用 Sec-WebSocket-Protocol,则必须在此声明对应的 Protocol TODO add other protocol

Callers 1

PlaygroundFunction · 0.85

Calls 13

ErrorMethod · 0.95
SetMessageMethod · 0.95
ToOpenAIErrorMethod · 0.95
LogErrorFunction · 0.92
NewChannelErrorFunction · 0.92
GetContextKeyStringFunction · 0.92
LogInfoFunction · 0.92
MessageWithRequestIdFunction · 0.92
getChannelFunction · 0.85
relayRequestFunction · 0.85
processChannelErrorFunction · 0.85
shouldRetryFunction · 0.85

Tested by

no test coverage detected