-
Notifications
You must be signed in to change notification settings - Fork 466
Stop retrying http requests when script invocation result is failed #11210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
satvu
merged 10 commits into
dev
from
satvu/http-streaming-observes-script-invocation-result
Aug 27, 2025
Merged
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1057af8
initial commit
satvu 324150f
update log
satvu ad8e862
update to respond to comments
satvu 9cac291
update retry proxy handler
satvu 153c611
test updates
satvu 2a002cd
update order of checks
satvu aa02d8f
resolve comments
satvu 1c81831
nit cleanup
satvu 861f7d6
resolve comment, update exception throw
satvu 832bb61
resolve typo
satvu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/WebJobs.Script/Http/ScriptInvocationRequestTransformer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Azure.WebJobs.Script.Description; | ||
using Yarp.ReverseProxy.Forwarder; | ||
|
||
namespace Microsoft.Azure.WebJobs.Script.Http | ||
{ | ||
internal class ScriptInvocationRequestTransformer : HttpTransformer | ||
satvu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
public override async ValueTask TransformRequestAsync(HttpContext httpContext, HttpRequestMessage proxyRequest, string destinationPrefix, CancellationToken cancellationToken) | ||
{ | ||
// this preserves previous behavior (which called the default transformer) - base method is also called inside of here | ||
await HttpTransformer.Default.TransformRequestAsync(httpContext, proxyRequest, destinationPrefix, cancellationToken); | ||
satvu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (httpContext.Items.TryGetValue(ScriptConstants.HttpProxyScriptInvocationContext, out object result) | ||
&& result is ScriptInvocationContext scriptContext) | ||
{ | ||
proxyRequest.Options.TryAdd(ScriptConstants.HttpProxyScriptInvocationContext, scriptContext); | ||
} | ||
} | ||
} | ||
} | ||
satvu marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
test/WebJobs.Script.Tests/HttpProxyService/ScriptInvocationRequestTransformerTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Azure.WebJobs.Script.Description; | ||
using Microsoft.Azure.WebJobs.Script.Http; | ||
using Xunit; | ||
|
||
namespace Microsoft.Azure.WebJobs.Script.Tests.Http | ||
{ | ||
public class ScriptInvocationRequestTransformerTests | ||
{ | ||
private readonly ScriptInvocationRequestTransformer _transformer; | ||
|
||
public ScriptInvocationRequestTransformerTests() | ||
{ | ||
_transformer = new ScriptInvocationRequestTransformer(); | ||
} | ||
|
||
[Theory] | ||
[InlineData(true)] | ||
[InlineData(false)] | ||
public async Task TransformRequestAsync_IncludesXForwardedHeaders(bool includeScriptInvocationContext) | ||
{ | ||
var httpContext = new DefaultHttpContext(); | ||
httpContext.Request.Scheme = "https"; | ||
httpContext.Request.Host = new HostString("example.com", 443); | ||
httpContext.Request.PathBase = "/api"; | ||
httpContext.Request.Path = "/test"; | ||
httpContext.Request.QueryString = new QueryString("?param=value"); | ||
|
||
var remoteAddress = "192.168.1.100"; | ||
httpContext.Connection.RemoteIpAddress = System.Net.IPAddress.Parse(remoteAddress); | ||
|
||
if (includeScriptInvocationContext) | ||
{ | ||
var scriptContext = new ScriptInvocationContext | ||
{ | ||
FunctionMetadata = new FunctionMetadata { Name = "TestFunction" }, | ||
ExecutionContext = new ExecutionContext { InvocationId = Guid.NewGuid() } | ||
}; | ||
|
||
httpContext.Items[ScriptConstants.HttpProxyScriptInvocationContext] = scriptContext; | ||
} | ||
|
||
var proxyRequest = new HttpRequestMessage(HttpMethod.Get, "http://localhost:7071/api/test"); | ||
const string destinationPrefix = "http://localhost:7071"; | ||
|
||
await _transformer.TransformRequestAsync(httpContext, proxyRequest, destinationPrefix, CancellationToken.None); | ||
|
||
Assert.True(proxyRequest.Headers.Contains("X-Forwarded-For"), "X-Forwarded-For header should be present"); | ||
Assert.True(proxyRequest.Headers.Contains("X-Forwarded-Host"), "X-Forwarded-Host header should be present"); | ||
Assert.True(proxyRequest.Headers.Contains("X-Forwarded-Proto"), "X-Forwarded-Proto header should be present"); | ||
|
||
var forwardedFor = proxyRequest.Headers.GetValues("X-Forwarded-For"); | ||
Assert.Contains(remoteAddress, forwardedFor); | ||
|
||
var forwardedHost = proxyRequest.Headers.GetValues("X-Forwarded-Host"); | ||
Assert.Contains("example.com:443", forwardedHost); | ||
|
||
var forwardedProto = proxyRequest.Headers.GetValues("X-Forwarded-Proto"); | ||
Assert.Contains("https", forwardedProto); | ||
} | ||
|
||
[Fact] | ||
public async Task TransformRequestAsync_WithScriptInvocationContext_AddsContextToRequestOptions() | ||
{ | ||
var httpContext = new DefaultHttpContext(); | ||
httpContext.Request.Scheme = "http"; | ||
httpContext.Request.Host = new HostString("localhost", 7071); | ||
var remoteAddress = "192.168.1.100"; | ||
httpContext.Connection.RemoteIpAddress = System.Net.IPAddress.Parse(remoteAddress); | ||
|
||
var scriptContext = new ScriptInvocationContext | ||
{ | ||
FunctionMetadata = new FunctionMetadata { Name = "TestFunction" }, | ||
ExecutionContext = new ExecutionContext { InvocationId = Guid.NewGuid() } | ||
}; | ||
|
||
httpContext.Items[ScriptConstants.HttpProxyScriptInvocationContext] = scriptContext; | ||
|
||
var proxyRequest = new HttpRequestMessage(HttpMethod.Get, "http://localhost:7071/api/test"); | ||
const string destinationPrefix = "http://localhost:7071"; | ||
|
||
await _transformer.TransformRequestAsync(httpContext, proxyRequest, destinationPrefix, CancellationToken.None); | ||
|
||
Assert.True(proxyRequest.Options.TryGetValue(ScriptConstants.HttpProxyScriptInvocationContext, out ScriptInvocationContext contextValue)); | ||
Assert.Equal(scriptContext.ExecutionContext.InvocationId, contextValue.ExecutionContext.InvocationId); | ||
|
||
Assert.True(proxyRequest.Headers.Contains("X-Forwarded-For")); | ||
Assert.True(proxyRequest.Headers.Contains("X-Forwarded-Host")); | ||
Assert.True(proxyRequest.Headers.Contains("X-Forwarded-Proto")); | ||
} | ||
|
||
[Fact] | ||
public async Task TransformRequestAsync_PreservesExistingXForwardedHeaders() | ||
{ | ||
var httpContext = new DefaultHttpContext(); | ||
httpContext.Request.Scheme = "https"; | ||
httpContext.Request.Host = new HostString("proxy.example.com"); | ||
var requestRemoteAddress = "172.16.0.1"; | ||
httpContext.Connection.RemoteIpAddress = System.Net.IPAddress.Parse(requestRemoteAddress); | ||
|
||
// Add existing X-Forwarded headers to simulate request through multiple proxies | ||
var originalFor = "203.0.113.195," + requestRemoteAddress; | ||
var originalHost = "proxy.example.com"; | ||
var originalProto = "https"; | ||
httpContext.Request.Headers["X-Forwarded-For"] = originalFor; | ||
httpContext.Request.Headers["X-Forwarded-Host"] = originalHost; | ||
httpContext.Request.Headers["X-Forwarded-Proto"] = originalProto; | ||
|
||
var proxyRequest = new HttpRequestMessage(HttpMethod.Get, "http://localhost:7071/api/test"); | ||
const string destinationPrefix = "http://localhost:7071"; | ||
|
||
await _transformer.TransformRequestAsync(httpContext, proxyRequest, destinationPrefix, CancellationToken.None); | ||
|
||
Assert.True(proxyRequest.Headers.Contains("X-Forwarded-For")); | ||
Assert.True(proxyRequest.Headers.Contains("X-Forwarded-Host")); | ||
Assert.True(proxyRequest.Headers.Contains("X-Forwarded-Proto")); | ||
|
||
var forwardedFor = proxyRequest.Headers.GetValues("X-Forwarded-For"); | ||
Assert.Contains(requestRemoteAddress, forwardedFor); | ||
|
||
var forwardedHost = proxyRequest.Headers.GetValues("X-Forwarded-Host"); | ||
Assert.Contains(originalHost, forwardedHost); | ||
|
||
var forwardedProto = proxyRequest.Headers.GetValues("X-Forwarded-Proto"); | ||
Assert.Contains(originalProto, forwardedProto); | ||
} | ||
} | ||
} | ||
satvu marked this conversation as resolved.
Show resolved
Hide resolved
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.