forked from Azure/azure-functions-host
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Http throttle defaulting (Azure#2113)
- Loading branch information
Showing
3 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
src/WebJobs.Script.WebHost/Configuration/HttpOptionsSetup.cs
This file contains 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,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; | ||
} | ||
} | ||
} | ||
} |
This file contains 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
62 changes: 62 additions & 0 deletions
62
test/WebJobs.Script.Tests/Configuration/HttpOptionsSetupTests.cs
This file contains 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,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); | ||
} | ||
} | ||
} |