From 816cc5a45ee60a7891ffe489e97733219f647a7d Mon Sep 17 00:00:00 2001 From: j82w Date: Tue, 23 Feb 2021 13:56:47 -0800 Subject: [PATCH] Performance: Adds an optimization to disable Nagle Algorithm for HttpRequest on NetFx (#2249) Disable Nagle for HTTP gateway mode requests; this improves latency/throughput for Gatewaymode customers on .net Framework. This is already the default on .net core so they should be no-op for those customers. --- .../src/Util/ServicePointAccessor.cs | 17 +++++++++++++++++ .../ServicePointAccessorTests.cs | 9 +++++++++ 2 files changed, 26 insertions(+) diff --git a/Microsoft.Azure.Cosmos/src/Util/ServicePointAccessor.cs b/Microsoft.Azure.Cosmos/src/Util/ServicePointAccessor.cs index 14e415f002..6816eb015a 100644 --- a/Microsoft.Azure.Cosmos/src/Util/ServicePointAccessor.cs +++ b/Microsoft.Azure.Cosmos/src/Util/ServicePointAccessor.cs @@ -22,6 +22,7 @@ internal class ServicePointAccessor private ServicePointAccessor(ServicePoint servicePoint) { this.servicePoint = servicePoint ?? throw new ArgumentNullException(nameof(servicePoint)); + this.TryDisableUseNagleAlgorithm(); } internal static ServicePointAccessor FindServicePoint(Uri endpoint) @@ -35,6 +36,22 @@ public int ConnectionLimit set => this.TrySetConnectionLimit(value); } + /// + /// Disable Nagle for HTTP requests. + /// This improves latency/throughput for Gateway operations on.net Framework + /// + private void TryDisableUseNagleAlgorithm() + { + try + { + this.servicePoint.UseNagleAlgorithm = false; + } + catch (PlatformNotSupportedException) + { + DefaultTrace.TraceWarning("ServicePoint.set_UseNagleAlgorithm - Platform does not support feature."); + } + } + private void TrySetConnectionLimit(int connectionLimit) { if (ServicePointAccessor.IsBrowser) diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/ServicePointAccessorTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/ServicePointAccessorTests.cs index f5258f2b0a..5ff15ed9a3 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/ServicePointAccessorTests.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/ServicePointAccessorTests.cs @@ -23,5 +23,14 @@ public void ServicePointAccessor_SetConnectionLimit() ServicePoint servicePoint = ServicePointManager.FindServicePoint(ServicePointAccessorTests.uri); Assert.AreEqual(limit, servicePoint.ConnectionLimit); } + + [TestMethod] + public void ServicePointAccessor_DisableNagle() + { + ServicePointAccessor accessor = ServicePointAccessor.FindServicePoint(ServicePointAccessorTests.uri); + Assert.IsNotNull(accessor); + ServicePoint servicePoint = ServicePointManager.FindServicePoint(ServicePointAccessorTests.uri); + Assert.IsFalse(servicePoint.UseNagleAlgorithm); + } } }