Skip to content

Commit 4a02b29

Browse files
committed
Http throttle defaulting (#2113)
1 parent 1f5b408 commit 4a02b29

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using Microsoft.Azure.WebJobs.Extensions.Http;
5+
using Microsoft.Extensions.Options;
6+
7+
namespace Microsoft.Azure.WebJobs.Script.WebHost.Configuration
8+
{
9+
public class HttpOptionsSetup : IConfigureOptions<HttpOptions>
10+
{
11+
internal const int DefaultMaxConcurrentRequests = 100;
12+
internal const int DefaultMaxOutstandingRequests = 200;
13+
private readonly IEnvironment _environment;
14+
15+
public HttpOptionsSetup(IEnvironment environment)
16+
{
17+
_environment = environment;
18+
}
19+
20+
public void Configure(HttpOptions options)
21+
{
22+
if (_environment.IsDynamic())
23+
{
24+
// when running under dynamic, we choose some default
25+
// throttle settings.
26+
// these can be overridden by the user in host.json
27+
if (_environment.IsAppServiceEnvironment())
28+
{
29+
// dynamic throttles are based on sandbox counters
30+
// which only exist in AppService
31+
options.DynamicThrottlesEnabled = true;
32+
}
33+
options.MaxConcurrentRequests = DefaultMaxConcurrentRequests;
34+
options.MaxOutstandingRequests = DefaultMaxOutstandingRequests;
35+
}
36+
}
37+
}
38+
}

src/WebJobs.Script.WebHost/WebScriptHostBuilderExtension.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.Azure.WebJobs.Host.Executors;
77
using Microsoft.Azure.WebJobs.Host.Timers;
88
using Microsoft.Azure.WebJobs.Script.Diagnostics;
9+
using Microsoft.Azure.WebJobs.Script.WebHost.Configuration;
910
using Microsoft.Azure.WebJobs.Script.WebHost.DependencyInjection;
1011
using Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics;
1112
using Microsoft.Extensions.DependencyInjection;
@@ -23,6 +24,12 @@ public static IHostBuilder AddWebScriptHost(this IHostBuilder builder, IServiceP
2324
ILoggerFactory configLoggerFactory = rootServiceProvider.GetService<ILoggerFactory>();
2425

2526
builder.UseServiceProviderFactory(new JobHostScopedServiceProviderFactory(rootServiceProvider, rootScopeFactory))
27+
.ConfigureServices(services =>
28+
{
29+
// register default configuration
30+
// must happen before the script host is added below
31+
services.ConfigureOptions<HttpOptionsSetup>();
32+
})
2633
.AddScriptHost(webHostOptions, configLoggerFactory, webJobsBuilder =>
2734
{
2835
webJobsBuilder
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System.Threading.Tasks.Dataflow;
5+
using Microsoft.Azure.WebJobs.Extensions.Http;
6+
using Microsoft.Azure.WebJobs.Script.WebHost.Configuration;
7+
using Moq;
8+
using Xunit;
9+
10+
namespace Microsoft.Azure.WebJobs.Script.Tests.Configuration
11+
{
12+
public class HttpOptionsSetupTests
13+
{
14+
[Fact]
15+
public void Configure_Dynamic_AppService_Defaults()
16+
{
17+
var mockEnvironment = new Mock<IEnvironment>(MockBehavior.Strict);
18+
mockEnvironment.Setup(p => p.GetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteInstanceId)).Returns("1234");
19+
mockEnvironment.Setup(p => p.GetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteSku)).Returns(ScriptConstants.DynamicSku);
20+
21+
var setup = new HttpOptionsSetup(mockEnvironment.Object);
22+
var options = new HttpOptions();
23+
setup.Configure(options);
24+
25+
Assert.True(options.DynamicThrottlesEnabled);
26+
Assert.Equal(HttpOptionsSetup.DefaultMaxConcurrentRequests, options.MaxConcurrentRequests);
27+
Assert.Equal(HttpOptionsSetup.DefaultMaxOutstandingRequests, options.MaxOutstandingRequests);
28+
}
29+
30+
[Fact]
31+
public void Configure_Dynamic_NonAppService_Defaults()
32+
{
33+
var mockEnvironment = new Mock<IEnvironment>(MockBehavior.Strict);
34+
mockEnvironment.Setup(p => p.GetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteInstanceId)).Returns((string)null);
35+
mockEnvironment.Setup(p => p.GetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteSku)).Returns(ScriptConstants.DynamicSku);
36+
37+
var setup = new HttpOptionsSetup(mockEnvironment.Object);
38+
var options = new HttpOptions();
39+
setup.Configure(options);
40+
41+
Assert.False(options.DynamicThrottlesEnabled);
42+
Assert.Equal(HttpOptionsSetup.DefaultMaxConcurrentRequests, options.MaxConcurrentRequests);
43+
Assert.Equal(HttpOptionsSetup.DefaultMaxOutstandingRequests, options.MaxOutstandingRequests);
44+
}
45+
46+
[Fact]
47+
public void Configure_Dedicated_DoesNotDefault()
48+
{
49+
var mockEnvironment = new Mock<IEnvironment>(MockBehavior.Strict);
50+
mockEnvironment.Setup(p => p.GetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteInstanceId)).Returns("1234");
51+
mockEnvironment.Setup(p => p.GetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteSku)).Returns("Dedicated");
52+
53+
var setup = new HttpOptionsSetup(mockEnvironment.Object);
54+
var options = new HttpOptions();
55+
setup.Configure(options);
56+
57+
Assert.False(options.DynamicThrottlesEnabled);
58+
Assert.Equal(DataflowBlockOptions.Unbounded, options.MaxConcurrentRequests);
59+
Assert.Equal(DataflowBlockOptions.Unbounded, options.MaxOutstandingRequests);
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)