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

Function WssRelay

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

Source from the content-addressed store, hash-verified

123}
124
125func WssRelay(c *gin.Context) {
126 // 将 HTTP 连接升级为 WebSocket 连接
127
128 ws, err := upgrader.Upgrade(c.Writer, c.Request, nil)
129 defer ws.Close()
130
131 if err != nil {
132 helper.WssError(c, ws, types.NewError(err, types.ErrorCodeGetChannelFailed).ToOpenAIError())
133 return
134 }
135
136 relayMode := relayconstant.Path2RelayMode(c.Request.URL.Path)
137 requestId := c.GetString(common.RequestIdKey)
138 group := c.GetString("group")
139 //wss://api.openai.com/v1/realtime?model=gpt-4o-realtime-preview-2024-10-01
140 originalModel := c.GetString("original_model")
141 var newAPIError *types.NewAPIError
142
143 for i := 0; i <= common.RetryTimes; i++ {
144 channel, err := getChannel(c, group, originalModel, i)
145 if err != nil {
146 common.LogError(c, err.Error())
147 newAPIError = err
148 break
149 }
150
151 newAPIError = wssRequest(c, ws, relayMode, channel)
152
153 if newAPIError == nil {
154 return // 成功处理请求,直接返回
155 }
156
157 go processChannelError(c, *types.NewChannelError(channel.Id, channel.Type, channel.Name, channel.ChannelInfo.IsMultiKey, common.GetContextKeyString(c, constant.ContextKeyChannelKey), channel.GetAutoBan()), newAPIError)
158
159 if !shouldRetry(c, newAPIError, common.RetryTimes-i) {
160 break
161 }
162 }
163 useChannel := c.GetStringSlice("use_channel")
164 if len(useChannel) > 1 {
165 retryLogStr := fmt.Sprintf("重试:%s", strings.Trim(strings.Join(strings.Fields(fmt.Sprint(useChannel)), "->"), "[]"))
166 common.LogInfo(c, retryLogStr)
167 }
168
169 if newAPIError != nil {
170 //if newAPIError.StatusCode == http.StatusTooManyRequests {
171 // newAPIError.SetMessage("当前分组上游负载已饱和,请稍后再试")
172 //}
173 newAPIError.SetMessage(common.MessageWithRequestId(newAPIError.Error(), requestId))
174 helper.WssError(c, ws, newAPIError.ToOpenAIError())
175 }
176}
177
178func RelayClaude(c *gin.Context) {
179 //relayMode := constant.Path2RelayMode(c.Request.URL.Path)

Callers

nothing calls this directly

Calls 15

ToOpenAIErrorMethod · 0.95
ErrorMethod · 0.95
SetMessageMethod · 0.95
WssErrorFunction · 0.92
NewErrorFunction · 0.92
LogErrorFunction · 0.92
NewChannelErrorFunction · 0.92
GetContextKeyStringFunction · 0.92
LogInfoFunction · 0.92
MessageWithRequestIdFunction · 0.92
getChannelFunction · 0.85
wssRequestFunction · 0.85

Tested by

no test coverage detected