Skip to content

Commit

Permalink
Http throttle defaulting (Azure#2113)
Browse files Browse the repository at this point in the history
  • Loading branch information
mathewc committed Sep 17, 2018
1 parent 1f5b408 commit 4a02b29
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/WebJobs.Script.WebHost/Configuration/HttpOptionsSetup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Options;

namespace Microsoft.Azure.WebJobs.Script.WebHost.Configuration
{
public class HttpOptionsSetup : IConfigureOptions<HttpOptions>
{
internal const int DefaultMaxConcurrentRequests = 100;
internal const int DefaultMaxOutstandingRequests = 200;
private readonly IEnvironment _environment;

public HttpOptionsSetup(IEnvironment environment)
{
_environment = environment;
}

public void Configure(HttpOptions options)
{
if (_environment.IsDynamic())
{
// when running under dynamic, we choose some default
// throttle settings.
// these can be overridden by the user in host.json
if (_environment.IsAppServiceEnvironment())
{
// dynamic throttles are based on sandbox counters
// which only exist in AppService
options.DynamicThrottlesEnabled = true;
}
options.MaxConcurrentRequests = DefaultMaxConcurrentRequests;
options.MaxOutstandingRequests = DefaultMaxOutstandingRequests;
}
}
}
}
7 changes: 7 additions & 0 deletions src/WebJobs.Script.WebHost/WebScriptHostBuilderExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.Azure.WebJobs.Host.Executors;
using Microsoft.Azure.WebJobs.Host.Timers;
using Microsoft.Azure.WebJobs.Script.Diagnostics;
using Microsoft.Azure.WebJobs.Script.WebHost.Configuration;
using Microsoft.Azure.WebJobs.Script.WebHost.DependencyInjection;
using Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -23,6 +24,12 @@ public static IHostBuilder AddWebScriptHost(this IHostBuilder builder, IServiceP
ILoggerFactory configLoggerFactory = rootServiceProvider.GetService<ILoggerFactory>();

builder.UseServiceProviderFactory(new JobHostScopedServiceProviderFactory(rootServiceProvider, rootScopeFactory))
.ConfigureServices(services =>
{
// register default configuration
// must happen before the script host is added below
services.ConfigureOptions<HttpOptionsSetup>();
})
.AddScriptHost(webHostOptions, configLoggerFactory, webJobsBuilder =>
{
webJobsBuilder
Expand Down
62 changes: 62 additions & 0 deletions test/WebJobs.Script.Tests/Configuration/HttpOptionsSetupTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System.Threading.Tasks.Dataflow;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Script.WebHost.Configuration;
using Moq;
using Xunit;

namespace Microsoft.Azure.WebJobs.Script.Tests.Configuration
{
public class HttpOptionsSetupTests
{
[Fact]
public void Configure_Dynamic_AppService_Defaults()
{
var mockEnvironment = new Mock<IEnvironment>(MockBehavior.Strict);
mockEnvironment.Setup(p => p.GetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteInstanceId)).Returns("1234");
mockEnvironment.Setup(p => p.GetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteSku)).Returns(ScriptConstants.DynamicSku);

var setup = new HttpOptionsSetup(mockEnvironment.Object);
var options = new HttpOptions();
setup.Configure(options);

Assert.True(options.DynamicThrottlesEnabled);
Assert.Equal(HttpOptionsSetup.DefaultMaxConcurrentRequests, options.MaxConcurrentRequests);
Assert.Equal(HttpOptionsSetup.DefaultMaxOutstandingRequests, options.MaxOutstandingRequests);
}

[Fact]
public void Configure_Dynamic_NonAppService_Defaults()
{
var mockEnvironment = new Mock<IEnvironment>(MockBehavior.Strict);
mockEnvironment.Setup(p => p.GetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteInstanceId)).Returns((string)null);
mockEnvironment.Setup(p => p.GetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteSku)).Returns(ScriptConstants.DynamicSku);

var setup = new HttpOptionsSetup(mockEnvironment.Object);
var options = new HttpOptions();
setup.Configure(options);

Assert.False(options.DynamicThrottlesEnabled);
Assert.Equal(HttpOptionsSetup.DefaultMaxConcurrentRequests, options.MaxConcurrentRequests);
Assert.Equal(HttpOptionsSetup.DefaultMaxOutstandingRequests, options.MaxOutstandingRequests);
}

[Fact]
public void Configure_Dedicated_DoesNotDefault()
{
var mockEnvironment = new Mock<IEnvironment>(MockBehavior.Strict);
mockEnvironment.Setup(p => p.GetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteInstanceId)).Returns("1234");
mockEnvironment.Setup(p => p.GetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteSku)).Returns("Dedicated");

var setup = new HttpOptionsSetup(mockEnvironment.Object);
var options = new HttpOptions();
setup.Configure(options);

Assert.False(options.DynamicThrottlesEnabled);
Assert.Equal(DataflowBlockOptions.Unbounded, options.MaxConcurrentRequests);
Assert.Equal(DataflowBlockOptions.Unbounded, options.MaxOutstandingRequests);
}
}
}

0 comments on commit 4a02b29

Please sign in to comment.