Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

main -> dev #3620

Merged
merged 9 commits into from
Aug 28, 2024
Merged
2 changes: 1 addition & 1 deletion Automate/Speckle.Automate.Sdk/AutomationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ public async Task StoreFileResult(string filePath)
formData.Add(streamContent, "files", Path.GetFileName(filePath));
HttpResponseMessage? request = await SpeckleClient.GQLClient.HttpClient
.PostAsync(
new Uri($"{AutomationRunData.SpeckleServerUrl}/api/stream/{AutomationRunData.ProjectId}/blob"),
new Uri($"{AutomationRunData.SpeckleServerUrl}api/stream/{AutomationRunData.ProjectId}/blob"),
formData
)
.ConfigureAwait(false);
Expand Down
5 changes: 5 additions & 0 deletions ConnectorCore/DllConflictManagement/DllConflictManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ private static bool MajorAndMinorVersionsEqual(Version version1, Version version
// is core which will be checked as a dependency of many other libraries.
// there are a couple other random types that will trigger this exception as well
}
catch (FileLoadException)
{
// this is new for .NET Core and is invalid for Speckle assemblies. Not catching causes other issues
// with other plugins so swallowing is the best we can do.
}
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion Core/Core/Api/GraphQL/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ private static GraphQLHttpClient CreateGraphQLClient(Account account, HttpClient

private static HttpClient CreateHttpClient(Account account)
{
var httpClient = Http.GetHttpProxyClient(null, TimeSpan.FromSeconds(30));
var httpClient = Http.GetHttpProxyClient(new SpeckleHttpClientHandler(Http.HttpAsyncPolicy(timeoutSeconds: 30)));
Http.AddAuthHeader(httpClient, account.token);

httpClient.DefaultRequestHeaders.Add("apollographql-client-name", Setup.HostApplication);
Expand Down
36 changes: 25 additions & 11 deletions Core/Core/Helpers/Http.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using Polly;
using Polly.Contrib.WaitAndRetry;
using Polly.Extensions.Http;
using Polly.Retry;
using Polly.Timeout;
using Serilog.Context;
using Speckle.Core.Credentials;
using Speckle.Core.Logging;
Expand All @@ -19,15 +19,21 @@ namespace Speckle.Core.Helpers;

public static class Http
{
public const int DEFAULT_TIMEOUT_SECONDS = 60;

public static IEnumerable<TimeSpan> DefaultDelay()
{
return Backoff.DecorrelatedJitterBackoffV2(TimeSpan.FromMilliseconds(100), 5);
return Backoff.DecorrelatedJitterBackoffV2(TimeSpan.FromMilliseconds(200), 5);
}

public static AsyncRetryPolicy<HttpResponseMessage> HttpAsyncPolicy(IEnumerable<TimeSpan>? delay = null)
public static IAsyncPolicy<HttpResponseMessage> HttpAsyncPolicy(
IEnumerable<TimeSpan>? delay = null,
int timeoutSeconds = DEFAULT_TIMEOUT_SECONDS
)
{
return HttpPolicyExtensions
var retryPolicy = HttpPolicyExtensions
.HandleTransientHttpError()
.Or<TimeoutRejectedException>()
.WaitAndRetryAsync(
delay ?? DefaultDelay(),
(ex, timeSpan, retryAttempt, context) =>
Expand All @@ -36,6 +42,10 @@ public static AsyncRetryPolicy<HttpResponseMessage> HttpAsyncPolicy(IEnumerable<
context.Add("retryCount", retryAttempt);
}
);

var timeoutPolicy = Policy.TimeoutAsync<HttpResponseMessage>(timeoutSeconds);

return Policy.WrapAsync(retryPolicy, timeoutPolicy);
}

/// <summary>
Expand Down Expand Up @@ -145,13 +155,17 @@ public static async Task<HttpResponseMessage> HttpPing(Uri uri)
}
}

public static HttpClient GetHttpProxyClient(SpeckleHttpClientHandler? handler = null, TimeSpan? timeout = null)
public static HttpClient GetHttpProxyClient(SpeckleHttpClientHandler? speckleHttpClientHandler = null)
{
IWebProxy proxy = WebRequest.GetSystemWebProxy();
proxy.Credentials = CredentialCache.DefaultCredentials;

handler ??= new SpeckleHttpClientHandler();
var client = new HttpClient(handler) { Timeout = timeout ?? TimeSpan.FromSeconds(100) };
speckleHttpClientHandler ??= new SpeckleHttpClientHandler(HttpAsyncPolicy());

var client = new HttpClient(speckleHttpClientHandler)
{
Timeout = Timeout.InfiniteTimeSpan //timeout is configured on the SpeckleHttpClientHandler through policy
};
return client;
}

Expand All @@ -178,11 +192,11 @@ public static void AddAuthHeader(HttpClient client, string? authToken)

public sealed class SpeckleHttpClientHandler : HttpClientHandler
{
private readonly IEnumerable<TimeSpan> _delay;
private readonly IAsyncPolicy<HttpResponseMessage> _resiliencePolicy;

public SpeckleHttpClientHandler(IEnumerable<TimeSpan>? delay = null)
public SpeckleHttpClientHandler(IAsyncPolicy<HttpResponseMessage> resiliencePolicy)
{
_delay = delay ?? Http.DefaultDelay();
_resiliencePolicy = resiliencePolicy;
}

/// <exception cref="OperationCanceledException"><paramref name="cancellationToken"/> requested cancel</exception>
Expand All @@ -206,7 +220,7 @@ CancellationToken cancellationToken

request.Headers.Add("x-request-id", context.CorrelationId.ToString());

var policyResult = await Http.HttpAsyncPolicy(_delay)
var policyResult = await _resiliencePolicy
.ExecuteAndCaptureAsync(
ctx =>
{
Expand Down
13 changes: 6 additions & 7 deletions Core/Core/Kits/Units.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,17 @@ public static class Units
public const string Miles = "mi";
public const string None = "none";

/// <summary>US Survey foot</summary>
/// <remarks>Considered an obsolete unit, superseded by the international foot <seealso cref="Feet"/></remarks>
public const string USFeet = "us_ft";
/// <summary>US Survey foot, now not supported by Speckle, kept privately for backwards compatibility</summary>
private const string USFeet = "us_ft";

private static readonly List<string> s_supportedUnits =
new() { Millimeters, Centimeters, Meters, Kilometers, Inches, Feet, USFeet, Yards, Miles, None };
internal static readonly List<string> SupportedUnits =
new() { Millimeters, Centimeters, Meters, Kilometers, Inches, Feet, Yards, Miles, None };

/// <param name="unit"></param>
/// <returns><see langword="true"/> if <paramref name="unit"/> is a recognised/supported unit string, otherwise <see langword="false"/></returns>
public static bool IsUnitSupported(string unit)
{
return s_supportedUnits.Contains(unit);
return SupportedUnits.Contains(unit);
}

/// <summary>
Expand Down Expand Up @@ -98,7 +97,7 @@ public static double GetConversionFactor(string? from, string? to)
case Centimeters:
return 100;
case Kilometers:
return 1000;
return 0.001;
case Inches:
return 39.3701;
case Feet:
Expand Down
2 changes: 1 addition & 1 deletion Core/Core/Transports/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private void Initialize(string baseUri, string streamId, string authorizationTok
StreamId = streamId;

Client = Http.GetHttpProxyClient(
new SpeckleHttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip }
new SpeckleHttpClientHandler(Http.HttpAsyncPolicy()) { AutomaticDecompression = DecompressionMethods.GZip }
);

Client.BaseAddress = new Uri(baseUri);
Expand Down
9 changes: 5 additions & 4 deletions Core/Core/Transports/ServerUtils/ServerAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,24 @@ public sealed class ServerApi : IDisposable, IServerApi

private readonly HttpClient _client;

public ServerApi(string baseUri, string? authorizationToken, string blobStorageFolder, int timeoutSeconds = 60)
public ServerApi(string baseUri, string? authorizationToken, string blobStorageFolder, int timeoutSeconds = 120)
{
CancellationToken = CancellationToken.None;

BlobStorageFolder = blobStorageFolder;

_client = Http.GetHttpProxyClient(
new SpeckleHttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip }
new SpeckleHttpClientHandler(Http.HttpAsyncPolicy(timeoutSeconds: timeoutSeconds))
{
AutomaticDecompression = DecompressionMethods.GZip
}
);

_client.BaseAddress = new Uri(baseUri);
_client.Timeout = TimeSpan.FromSeconds(timeoutSeconds);

Http.AddAuthHeader(_client, authorizationToken);
}

private int RetriedCount { get; set; }
public CancellationToken CancellationToken { get; set; }
public bool CompressPayloads { get; set; } = true;

Expand Down
4 changes: 2 additions & 2 deletions Core/Core/Transports/ServerV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void SaveBlob(Blob obj)
{
if (IsInErrorState)
{
return;
throw new TransportException("Server transport is in an errored state", _exception);
}

_sendBuffer.Add(($"blob:{hash}", obj.filePath));
Expand Down Expand Up @@ -223,7 +223,7 @@ public void SaveObject(string id, string serializedObject)
{
if (IsInErrorState)
{
return;
throw new TransportException("Server transport is in an errored state", _exception);
}

_sendBuffer.Add((id, serializedObject));
Expand Down
27 changes: 27 additions & 0 deletions Core/Tests/Speckle.Core.Tests.Unit/Kits/UnitsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using NUnit.Framework;
using Speckle.Core.Kits;

namespace Speckle.Core.Tests.Unit.Kits;

[TestOf(typeof(Units))]
public class UnitsTest
{
private const double EPS = 0.00022;

[Test, Combinatorial]
[DefaultFloatingPointTolerance(EPS)]
public void TestUnitConversion(
[ValueSource(typeof(Units), nameof(Units.SupportedUnits))] string from,
[ValueSource(typeof(Units), nameof(Units.SupportedUnits))] string to
)
{
var forwards = Units.GetConversionFactor(from, to);
var backwards = Units.GetConversionFactor(to, from);

Assert.That(
backwards * forwards,
Is.EqualTo(1d),
$"Behaviour says that 1{from} == {forwards}{to}, and 1{to} == {backwards}{from}"
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,46 @@ public partial class ConverterRevit
{
public RevitMEPConnector ConnectorToSpeckle(Connector connector)
{
var speckleMEPConnector = new RevitMEPConnector
var speckleMepConnector = new RevitMEPConnector
{
applicationId = connector.GetUniqueApplicationId(),
origin = PointToSpeckle(connector.Origin, Doc),
shape = connector.Shape.ToString(),
systemName = connector.MEPSystem?.Name ?? connector.Owner.Category?.Name,
systemName = (connector.MEPSystem?.Name ?? connector.Owner.Category?.Name) ?? string.Empty
};

try
{
speckleMepConnector.origin = PointToSpeckle(connector.Origin, Doc);
}
catch (Autodesk.Revit.Exceptions.InvalidOperationException)
{
// ignore this exception if there is no origin, we cant report it.
// and yet there is no discovery of a Physical connector type.
}

if (connector.Domain is Domain.DomainHvac or Domain.DomainPiping or Domain.DomainCableTrayConduit)
{
speckleMEPConnector.angle = connector.Angle;
speckleMepConnector.angle = connector.Angle;
}

if (connector.Shape is ConnectorProfileType.Rectangular)
if (connector.Shape == ConnectorProfileType.Rectangular)
{
speckleMEPConnector.height = ScaleToSpeckle(connector.Height);
speckleMEPConnector.width = ScaleToSpeckle(connector.Width);
speckleMepConnector.height = ScaleToSpeckle(connector.Height);
speckleMepConnector.width = ScaleToSpeckle(connector.Width);
}
else if (connector.Shape is ConnectorProfileType.Round)
else if (connector.Shape == ConnectorProfileType.Round)
{
speckleMEPConnector.radius = ScaleToSpeckle(connector.Radius);
speckleMepConnector.radius = ScaleToSpeckle(connector.Radius);
}

foreach (var reference in connector.AllRefs.Cast<Connector>())
{
if (connector.IsConnectedTo(reference))
{
speckleMEPConnector.connectedConnectorIds.Add(reference.GetUniqueApplicationId());
speckleMepConnector.connectedConnectorIds.Add(reference.GetUniqueApplicationId());
}
}
return speckleMEPConnector;

return speckleMepConnector;
}
}