HandleProxy is the main entry point for proxy requests, refactored based on the stable .bak logic.
(c *gin.Context)
| 59 | |
| 60 | // HandleProxy is the main entry point for proxy requests, refactored based on the stable .bak logic. |
| 61 | func (ps *ProxyServer) HandleProxy(c *gin.Context) { |
| 62 | startTime := time.Now() |
| 63 | groupName := c.Param("group_name") |
| 64 | |
| 65 | originalGroup, err := ps.groupManager.GetGroupByName(groupName) |
| 66 | if err != nil { |
| 67 | response.Error(c, app_errors.ParseDBError(err)) |
| 68 | return |
| 69 | } |
| 70 | |
| 71 | // Select sub-group if this is an aggregate group |
| 72 | subGroupName, err := ps.subGroupManager.SelectSubGroup(originalGroup) |
| 73 | if err != nil { |
| 74 | logrus.WithFields(logrus.Fields{ |
| 75 | "aggregate_group": originalGroup.Name, |
| 76 | "error": err, |
| 77 | }).Error("Failed to select sub-group from aggregate") |
| 78 | response.Error(c, app_errors.NewAPIError(app_errors.ErrNoKeysAvailable, "No available sub-groups")) |
| 79 | return |
| 80 | } |
| 81 | |
| 82 | group := originalGroup |
| 83 | if subGroupName != "" { |
| 84 | group, err = ps.groupManager.GetGroupByName(subGroupName) |
| 85 | if err != nil { |
| 86 | response.Error(c, app_errors.ParseDBError(err)) |
| 87 | return |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | channelHandler, err := ps.channelFactory.GetChannel(group) |
| 92 | if err != nil { |
| 93 | response.Error(c, app_errors.NewAPIError(app_errors.ErrInternalServer, fmt.Sprintf("Failed to get channel for group '%s': %v", groupName, err))) |
| 94 | return |
| 95 | } |
| 96 | |
| 97 | bodyBytes, err := io.ReadAll(c.Request.Body) |
| 98 | if err != nil { |
| 99 | logrus.Errorf("Failed to read request body: %v", err) |
| 100 | response.Error(c, app_errors.NewAPIError(app_errors.ErrBadRequest, "Failed to read request body")) |
| 101 | return |
| 102 | } |
| 103 | c.Request.Body.Close() |
| 104 | |
| 105 | finalBodyBytes, err := ps.applyParamOverrides(bodyBytes, group) |
| 106 | if err != nil { |
| 107 | response.Error(c, app_errors.NewAPIError(app_errors.ErrInternalServer, fmt.Sprintf("Failed to apply parameter overrides: %v", err))) |
| 108 | return |
| 109 | } |
| 110 | |
| 111 | isStream := channelHandler.IsStreamRequest(c, bodyBytes) |
| 112 | |
| 113 | ps.executeRequestWithRetry(c, channelHandler, originalGroup, group, finalBodyBytes, isStream, startTime, 0) |
| 114 | } |
| 115 | |
| 116 | // executeRequestWithRetry is the core recursive function for handling requests and retries. |
| 117 | func (ps *ProxyServer) executeRequestWithRetry( |
nothing calls this directly
no test coverage detected