Skip to content

fix: fix the proxyURL is empty, not using the default HTTP client configuration && the AWS calling side did not apply the relay timeout. - #2580

Merged
Calcium-Ion merged 1 commit into
QuantumNous:mainfrom
seefs001:fix/aws-proxy-timeout
Jan 5, 2026
Merged

Conversation

@seefs001

@seefs001 seefs001 commented Jan 5, 2026

Copy link
Copy Markdown
Collaborator

Summary by CodeRabbit

  • Bug Fixes

    • Improved timeout and cancellation handling for AWS API invocations to prevent indefinite requests.
  • Chores

    • Optimized HTTP client resource reuse for better efficiency.

✏️ Tip: You can customize this high-level summary in your review settings.

…figuration && the AWS calling side did not apply the relay timeout.
@coderabbitai

coderabbitai Bot commented Jan 5, 2026

Copy link
Copy Markdown
Contributor

Walkthrough

The changes introduce context-based timeout management for AWS API invocations and improve HTTP client reuse in proxy configurations. A new helper function creates request-scoped contexts with configurable timeouts, replacing direct use of request contexts across AWS handlers. Additionally, HTTP client selection logic is updated to promote reuse of existing client instances when proxies are unavailable.

Changes

Cohort / File(s) Summary
AWS Relay Context & Timeout Management
relay/channel/aws/relay-aws.go
Introduces newAwsInvokeContext helper for timeout-aware context creation. Replaces c.Request.Context() with timeout-managed context across awsHandler, awsStreamHandler, and handleNovaRequest. Propagates AWS request objects into adaptor state via a.AwsReq. Adds context and time imports.
HTTP Client Reuse Logic
service/http_client.go
Updates NewProxyHttpClient to return GetHttpClient() when proxyURL is empty (if available), falling back to http.DefaultClient. Promotes reuse of existing HTTP client instances.

Sequence Diagram(s)

sequenceDiagram
    actor Handler as AWS Handler
    participant ContextMgr as Context Manager
    participant SDK as AWS SDK
    participant Cleanup as Cleanup

    Handler->>ContextMgr: newAwsInvokeContext()
    ContextMgr->>ContextMgr: Create context with timeout<br/>(RelayTimeout or Background)
    ContextMgr-->>Handler: Return (ctx, cancel)
    
    rect rgb(200, 220, 240)
    Note over Handler,SDK: AWS Invocation Phase
    Handler->>SDK: InvokeModel(ctx, awsReq)
    SDK-->>Handler: Response/Stream
    end
    
    Handler->>Cleanup: defer cancel()
    Cleanup->>Cleanup: Cancel context
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 A context with timeout bounds,
AWS calls now safer, more sound!
With cancellation deferred with care,
And HTTP clients reused everywhere,
Our relay hops faster through the air! 🚀

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 37.50% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title identifies two specific issues being fixed: proxyURL empty handling and AWS relay timeout application, which align with the changeset modifications.
✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Fix all issues with AI Agents 🤖
In @relay/channel/aws/relay-aws.go:
- Around line 42-47: newAwsInvokeContext currently uses context.Background()
which prevents request cancellation from propagating; change its signature to
accept the request context (e.g., newAwsInvokeContext(c *gin.Context) or
newAwsInvokeContext(ctx context.Context)), create the child context from the
incoming request context (use c.Request.Context() or the passed ctx) and apply
the timeout only if common.RelayTimeout > 0, returning the derived context and
cancel func; then update all call sites that invoked newAwsInvokeContext (the
three places noted) to pass the gin context variable (c) or the request context
so cancellations from the client propagate into AWS SDK calls.
📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 177553a and 5f37a1e.

📒 Files selected for processing (2)
  • relay/channel/aws/relay-aws.go
  • service/http_client.go
🧰 Additional context used
🧬 Code graph analysis (1)
relay/channel/aws/relay-aws.go (1)
common/constants.go (1)
  • RelayTimeout (122-122)
🔇 Additional comments (2)
service/http_client.go (1)

85-87: Good improvement to client reuse.

The change correctly prioritizes the configured httpClient (which includes timeouts, connection pools, and redirect checks from InitHttpClient()) over the bare http.DefaultClient. This ensures consistent behavior when no proxy is specified.

relay/channel/aws/relay-aws.go (1)

129-129: Good fix: Ensures AwsReq is set for Nova model path.

This change makes the Nova model path consistent with other code paths (lines 147, 159) by properly setting a.AwsReq after constructing the request.

Comment on lines +42 to +47
func newAwsInvokeContext() (context.Context, context.CancelFunc) {
if common.RelayTimeout <= 0 {
return context.Background(), func() {}
}
return context.WithTimeout(context.Background(), time.Duration(common.RelayTimeout)*time.Second)
}

@coderabbitai coderabbitai Bot Jan 5, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Critical: Context should inherit from request context.

The function creates a new context from context.Background(), which breaks request cancellation propagation. When a client disconnects or cancels their request, the AWS SDK calls will continue executing, leading to:

  • Wasted AWS API invocations and costs
  • Resource leaks (uncancelled network connections)
  • Delayed error responses
🔎 Proposed fix to inherit from request context

The context should be created from the HTTP request context to properly propagate cancellations:

-func newAwsInvokeContext() (context.Context, context.CancelFunc) {
+func newAwsInvokeContext(c *gin.Context) (context.Context, context.CancelFunc) {
+	reqCtx := c.Request.Context()
 	if common.RelayTimeout <= 0 {
-		return context.Background(), func() {}
+		return reqCtx, func() {}
 	}
-	return context.WithTimeout(context.Background(), time.Duration(common.RelayTimeout)*time.Second)
+	return context.WithTimeout(reqCtx, time.Duration(common.RelayTimeout)*time.Second)
 }

Then update all call sites to pass the gin context:

  • Line 214: ctx, cancel := newAwsInvokeContext(c)
  • Line 244: ctx, cancel := newAwsInvokeContext(c)
  • Line 287: ctx, cancel := newAwsInvokeContext(c)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func newAwsInvokeContext() (context.Context, context.CancelFunc) {
if common.RelayTimeout <= 0 {
return context.Background(), func() {}
}
return context.WithTimeout(context.Background(), time.Duration(common.RelayTimeout)*time.Second)
}
func newAwsInvokeContext(c *gin.Context) (context.Context, context.CancelFunc) {
reqCtx := c.Request.Context()
if common.RelayTimeout <= 0 {
return reqCtx, func() {}
}
return context.WithTimeout(reqCtx, time.Duration(common.RelayTimeout)*time.Second)
}
🤖 Prompt for AI Agents
In @relay/channel/aws/relay-aws.go around lines 42-47, newAwsInvokeContext
currently uses context.Background() which prevents request cancellation from
propagating; change its signature to accept the request context (e.g.,
newAwsInvokeContext(c *gin.Context) or newAwsInvokeContext(ctx
context.Context)), create the child context from the incoming request context
(use c.Request.Context() or the passed ctx) and apply the timeout only if
common.RelayTimeout > 0, returning the derived context and cancel func; then
update all call sites that invoked newAwsInvokeContext (the three places noted)
to pass the gin context variable (c) or the request context so cancellations
from the client propagate into AWS SDK calls.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is exactly what I'm going to do.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seefs001, great! Thank you for addressing this. The fix will ensure proper request cancellation propagation and prevent resource leaks. Let me know if you need any assistance with the implementation.

@Calcium-Ion
Calcium-Ion merged commit d3f3393 into QuantumNous:main Jan 5, 2026
1 check passed
ennnnny pushed a commit to ennnnny/new-api that referenced this pull request Mar 17, 2026
fix: fix the proxyURL is empty, not using the default HTTP client configuration && the AWS calling side did not apply the relay timeout.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants