Skip to content

Commit

Permalink
Transport: Fix ServicePoint for WebAssembly (#1445)
Browse files Browse the repository at this point in the history
* Adding accessor

* webassembly too

* Comment
  • Loading branch information
ealsur committed May 1, 2020
1 parent 5024c15 commit 45e5f91
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Microsoft.Azure.Cosmos/src/DocumentClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,7 @@ internal virtual void Initialize(Uri serviceEndpoint,
this.ConnectionPolicy = connectionPolicy ?? ConnectionPolicy.Default;

#if !NETSTANDARD16
ServicePoint servicePoint = ServicePointManager.FindServicePoint(this.ServiceEndpoint);
ServicePointAccessor servicePoint = ServicePointAccessor.FindServicePoint(this.ServiceEndpoint);
servicePoint.ConnectionLimit = this.ConnectionPolicy.MaxConnectionLimit;
#endif

Expand Down
2 changes: 1 addition & 1 deletion Microsoft.Azure.Cosmos/src/Routing/LocationCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ private bool CanUseMultipleWriteLocations()
private void SetServicePointConnectionLimit(Uri endpoint)
{
#if !NETSTANDARD16
ServicePoint servicePoint = ServicePointManager.FindServicePoint(endpoint);
ServicePointAccessor servicePoint = ServicePointAccessor.FindServicePoint(endpoint);
servicePoint.ConnectionLimit = this.connectionLimit;
#endif
}
Expand Down
57 changes: 57 additions & 0 deletions Microsoft.Azure.Cosmos/src/Util/ServicePointAccessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------

namespace Microsoft.Azure.Cosmos
{
using System;
using System.Net;
using System.Runtime.InteropServices;
using Microsoft.Azure.Cosmos.Core.Trace;

/// <summary>
/// While ServicePoint is a NETStandard 2.0 API, not all runtimes support the operations and some Framework implementations might not support it.
/// </summary>
internal class ServicePointAccessor
{
// WebAssembly detection
private static bool IsBrowser = RuntimeInformation.IsOSPlatform(OSPlatform.Create("BROWSER")) || RuntimeInformation.IsOSPlatform(OSPlatform.Create("WEBASSEMBLY"));

private readonly ServicePoint servicePoint;

private ServicePointAccessor(ServicePoint servicePoint)
{
this.servicePoint = servicePoint ?? throw new ArgumentNullException(nameof(servicePoint));
}

internal static ServicePointAccessor FindServicePoint(Uri endpoint)
{
return new ServicePointAccessor(ServicePointManager.FindServicePoint(endpoint));
}

public int ConnectionLimit
{
get => this.servicePoint.ConnectionLimit;
set => this.TrySetConnectionLimit(value);
}

private void TrySetConnectionLimit(int connectionLimit)
{
if (ServicePointAccessor.IsBrowser)
{
// Workaround for WebAssembly.
// WebAssembly currently throws a SynchronizationLockException and not a PlatformNotSupportedException.
return;
}

try
{
this.servicePoint.ConnectionLimit = connectionLimit;
}
catch (PlatformNotSupportedException)
{
DefaultTrace.TraceWarning("ServicePoint.set_ConnectionLimit - Platform does not support feature.");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------

namespace Microsoft.Azure.Cosmos.Tests
{
using System;
using System.Net;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class ServicePointAccessorTests
{
private static Uri uri = new Uri("https://localhost");

[TestMethod]
public void ServicePointAccessor_SetConnectionLimit()
{
int limit = 10;
ServicePointAccessor accessor = ServicePointAccessor.FindServicePoint(ServicePointAccessorTests.uri);
Assert.IsNotNull(accessor);
accessor.ConnectionLimit = limit;
ServicePoint servicePoint = ServicePointManager.FindServicePoint(ServicePointAccessorTests.uri);
Assert.AreEqual(limit, servicePoint.ConnectionLimit);
}
}
}

0 comments on commit 45e5f91

Please sign in to comment.