-
Notifications
You must be signed in to change notification settings - Fork 491
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Transport: Fix ServicePoint for WebAssembly (#1445)
* Adding accessor * webassembly too * Comment
- Loading branch information
Showing
4 changed files
with
86 additions
and
2 deletions.
There are no files selected for viewing
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
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
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,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."); | ||
} | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/ServicePointAccessorTests.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,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); | ||
} | ||
} | ||
} |