fix: properly manage request context lifecycle by moving cancel calls to appropriate error and completion paths
This commit is contained in:
@@ -271,10 +271,10 @@ func handleStreaming(
|
|||||||
) error {
|
) error {
|
||||||
// Direct HTTP request to upstream (bypass proxy.Forward for streaming)
|
// Direct HTTP request to upstream (bypass proxy.Forward for streaming)
|
||||||
ctx, cancel := context.WithTimeout(c.Context(), time.Duration(cfg.RequestTimeoutSeconds)*time.Second)
|
ctx, cancel := context.WithTimeout(c.Context(), time.Duration(cfg.RequestTimeoutSeconds)*time.Second)
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
req, err := http.NewRequestWithContext(ctx, "POST", targetURL, bytes.NewReader(body))
|
req, err := http.NewRequestWithContext(ctx, "POST", targetURL, bytes.NewReader(body))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
cancel()
|
||||||
return c.Status(fiber.StatusInternalServerError).SendString("build request failed")
|
return c.Status(fiber.StatusInternalServerError).SendString("build request failed")
|
||||||
}
|
}
|
||||||
for key, vals := range headers {
|
for key, vals := range headers {
|
||||||
@@ -287,11 +287,13 @@ func handleStreaming(
|
|||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
cancel()
|
||||||
logger.Warn("[ANTHROPIC] Stream error [IP: %s]: %v", c.IP(), err)
|
logger.Warn("[ANTHROPIC] Stream error [IP: %s]: %v", c.IP(), err)
|
||||||
return c.Status(fiber.StatusBadGateway).SendString("upstream error")
|
return c.Status(fiber.StatusBadGateway).SendString("upstream error")
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode != 200 {
|
if resp.StatusCode != 200 {
|
||||||
|
cancel()
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
body, _ := io.ReadAll(resp.Body)
|
body, _ := io.ReadAll(resp.Body)
|
||||||
logger.Warn("[ANTHROPIC] Stream upstream %d [IP: %s]: %s", resp.StatusCode, c.IP(), string(body))
|
logger.Warn("[ANTHROPIC] Stream upstream %d [IP: %s]: %s", resp.StatusCode, c.IP(), string(body))
|
||||||
@@ -308,6 +310,7 @@ func handleStreaming(
|
|||||||
c.Set("Connection", "keep-alive")
|
c.Set("Connection", "keep-alive")
|
||||||
|
|
||||||
c.Response().SetBodyStreamWriter(func(w *bufio.Writer) {
|
c.Response().SetBodyStreamWriter(func(w *bufio.Writer) {
|
||||||
|
defer cancel()
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
scanner := bufio.NewScanner(resp.Body)
|
scanner := bufio.NewScanner(resp.Body)
|
||||||
|
|||||||
Reference in New Issue
Block a user