Skip to content

Commit

Permalink
Merge pull request #1352 from vicancy/r1.9.2
Browse files Browse the repository at this point in the history
Release 1.9.2
  • Loading branch information
vicancy authored Jul 16, 2021
2 parents 3092473 + 717c89f commit a7aef3e
Show file tree
Hide file tree
Showing 58 changed files with 434 additions and 303 deletions.
6 changes: 6 additions & 0 deletions src/Microsoft.Azure.SignalR.Common/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,11 @@ public static class Protocol
{
public const string BlazorPack = "blazorpack";
}

public static class Headers
{
public const string AsrsHeaderPrefix = "X-ASRS-";
public const string AsrsMessageTracingId = AsrsHeaderPrefix + "Message-Tracing-Id";
}
}
}
19 changes: 18 additions & 1 deletion src/Microsoft.Azure.SignalR.Common/Utilities/RestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,20 @@ internal class RestClient
{
private readonly JsonSerializerSettings _jsonSerializerSettings;
private readonly IHttpClientFactory _httpClientFactory;
private readonly bool _enableMessageTracing;

public RestClient(IHttpClientFactory httpClientFactory, JsonSerializerSettings jsonSerializerSettings)
public RestClient(IHttpClientFactory httpClientFactory, JsonSerializerSettings jsonSerializerSettings, bool enableMessageTracing)
{
_httpClientFactory = httpClientFactory;
_jsonSerializerSettings = jsonSerializerSettings;
_enableMessageTracing = enableMessageTracing;
}

public RestClient()
{
_httpClientFactory = HttpClientFactory.Instance;
_jsonSerializerSettings = new JsonSerializerSettings();
_enableMessageTracing = false;
}

public Task SendAsync(
Expand Down Expand Up @@ -141,6 +144,10 @@ private static Uri GetUri(string url, IDictionary<string, StringValues> query)
private HttpRequestMessage BuildRequest(RestApiEndpoint api, HttpMethod httpMethod, string productInfo, string methodName = null, object[] args = null)
{
var payload = httpMethod == HttpMethod.Post ? new PayloadMessage { Target = methodName, Arguments = args } : null;
if (_enableMessageTracing)
{
AddTracingId(api);
}
return GenerateHttpRequest(api.Audience, api.Query, httpMethod, payload, api.Token, productInfo);
}

Expand All @@ -152,5 +159,15 @@ private HttpRequestMessage GenerateHttpRequest(string url, IDictionary<string, S
request.Content = new StringContent(JsonConvert.SerializeObject(payload, _jsonSerializerSettings), Encoding.UTF8, "application/json");
return request;
}

private void AddTracingId(RestApiEndpoint api)
{
var id = MessageWithTracingIdHelper.Generate();
if (api.Query == null)
{
api.Query = new Dictionary<string, StringValues>();
}
api.Query.Add(Constants.Headers.AsrsMessageTracingId, id.ToString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public IServiceHubLifetimeManager Create(string hubName)
payloadSerializerSettings = newtonsoftServiceHubProtocolOptions.Value.PayloadSerializerSettings;
}
var httpClientFactory = _serviceProvider.GetRequiredService<IHttpClientFactory>();
var restClient = new RestClient(httpClientFactory, payloadSerializerSettings);
var restClient = new RestClient(httpClientFactory, payloadSerializerSettings, _options.EnableMessageTracing);
return new RestHubLifetimeManager(hubName, new ServiceEndpoint(_options.ConnectionString), _options.ProductInfo, _options.ApplicationName, restClient);
}
default: throw new InvalidEnumArgumentException(nameof(ServiceManagerOptions.ServiceTransportType), (int)_options.ServiceTransportType, typeof(ServiceTransportType));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Microsoft.Azure.SignalR.Management
{
internal class NewtonsoftServiceHubProtocolOptions
public class NewtonsoftServiceHubProtocolOptions
{
/// <summary>
/// Gets or sets the settings used to serialize invocation arguments and return values.
Expand Down
17 changes: 12 additions & 5 deletions src/Microsoft.Azure.SignalR.Management/ServiceManagerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,12 @@ public ServiceManagerBuilder WithRouter(IEndpointRouter router)
return this;
}

// todo: make public
internal ServiceManagerBuilder WithNewtonsoftJsonHubProtocol(Action<NewtonsoftServiceHubProtocolOptions> configure)
/// <summary>
/// Uses Newtonsoft.Json library to serialize messages sent to SignalR.
/// </summary>
/// <param name="configure">A delegate that can be used to configure the <see cref="NewtonsoftServiceHubProtocolOptions"/>.</param>
/// <returns>The <see cref="ServiceManagerBuilder"/> instance itself.</returns>
public ServiceManagerBuilder WithNewtonsoftJson(Action<NewtonsoftServiceHubProtocolOptions> configure)
{
if (configure is null)
{
Expand All @@ -63,10 +67,13 @@ internal ServiceManagerBuilder WithNewtonsoftJsonHubProtocol(Action<NewtonsoftSe
return this;
}

// todo: make public
internal ServiceManagerBuilder WithNewtonsoftJsonHubProtocol()
/// <summary>
/// Uses Newtonsoft.Json library to serialize messages sent to SignalR.
/// </summary>
/// <returns>The <see cref="ServiceHubContextBuilder"/> instance itself.</returns>
public ServiceManagerBuilder WithNewtonsoftJson()
{
return WithNewtonsoftJsonHubProtocol(o => { });
return WithNewtonsoftJson(o => { });
}

[EditorBrowsable(EditorBrowsableState.Never)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public Task UserAddToGroupAsync(string userId, string groupName, TimeSpan ttl, C
{
throw new ArgumentOutOfRangeException(nameof(ttl), TtlOutOfRangeErrorMessage);
}
var message = new UserJoinGroupMessage(userId, groupName) { Ttl = (int)ttl.TotalSeconds }.WithTracingId();
var message = AppendMessageTracingId(new UserJoinGroupMessage(userId, groupName) { Ttl = (int)ttl.TotalSeconds });
if (message.TracingId != null)
{
MessageLog.StartToAddUserToGroup(Logger, message);
Expand All @@ -80,7 +80,7 @@ public Task UserRemoveFromGroupAsync(string userId, string groupName, Cancellati
throw new ArgumentException(NullOrEmptyStringErrorMessage, nameof(groupName));
}

var message = new UserLeaveGroupMessage(userId, groupName).WithTracingId();
var message = AppendMessageTracingId(new UserLeaveGroupMessage(userId, groupName));
if (message.TracingId != null)
{
MessageLog.StartToRemoveUserFromGroup(Logger, message);
Expand All @@ -95,7 +95,7 @@ public Task UserRemoveFromAllGroupsAsync(string userId, CancellationToken cancel
throw new ArgumentException(NullOrEmptyStringErrorMessage, nameof(userId));
}

var message = new UserLeaveGroupMessage(userId, null).WithTracingId();
var message = AppendMessageTracingId(new UserLeaveGroupMessage(userId, null));
if (message.TracingId != null)
{
MessageLog.StartToRemoveUserFromGroup(Logger, message);
Expand All @@ -115,7 +115,7 @@ public Task<bool> IsUserInGroup(string userId, string groupName, CancellationTok
throw new ArgumentException(NullOrEmptyStringErrorMessage, nameof(groupName));
}

var message = new CheckUserInGroupWithAckMessage(userId, groupName).WithTracingId();
var message = AppendMessageTracingId(new CheckUserInGroupWithAckMessage(userId, groupName));
if (message.TracingId != null)
{
MessageLog.StartToCheckIfUserInGroup(Logger, message);
Expand All @@ -130,7 +130,7 @@ public Task CloseConnectionAsync(string connectionId, string reason, Cancellatio
throw new ArgumentException(NullOrEmptyStringErrorMessage, nameof(connectionId));
}

var message = new CloseConnectionMessage(connectionId, reason).WithTracingId();
var message = AppendMessageTracingId(new CloseConnectionMessage(connectionId, reason));
if (message.TracingId != null)
{
MessageLog.StartToCloseConnection(Logger, message);
Expand Down
21 changes: 10 additions & 11 deletions src/Microsoft.Azure.SignalR/HubHost/ServiceLifetimeManagerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public override Task SendAllAsync(string methodName, object[] args, Cancellation
throw new ArgumentException(NullOrEmptyStringErrorMessage, nameof(methodName));
}

// todo: apply to other methods
var message = AppendMessageTracingId(new BroadcastDataMessage(null, SerializeAllProtocols(methodName, args)));
if (message.TracingId != null)
{
Expand All @@ -62,7 +61,7 @@ public override Task SendAllExceptAsync(string methodName, object[] args, IReadO
throw new ArgumentException(NullOrEmptyStringErrorMessage, nameof(methodName));
}

var message = new BroadcastDataMessage(excludedIds, SerializeAllProtocols(methodName, args)).WithTracingId();
var message = AppendMessageTracingId(new BroadcastDataMessage(excludedIds, SerializeAllProtocols(methodName, args)));
if (message.TracingId != null)
{
MessageLog.StartToBroadcastMessage(Logger, message);
Expand All @@ -82,7 +81,7 @@ public override Task SendConnectionAsync(string connectionId, string methodName,
throw new ArgumentException(NullOrEmptyStringErrorMessage, nameof(methodName));
}

var message = new MultiConnectionDataMessage(new[] { connectionId }, SerializeAllProtocols(methodName, args)).WithTracingId();
var message = AppendMessageTracingId(new MultiConnectionDataMessage(new[] { connectionId }, SerializeAllProtocols(methodName, args)));
if (message.TracingId != null)
{
MessageLog.StartToSendMessageToConnections(Logger, message);
Expand All @@ -102,7 +101,7 @@ public override Task SendConnectionsAsync(IReadOnlyList<string> connectionIds, s
throw new ArgumentException(NullOrEmptyStringErrorMessage, nameof(methodName));
}

var message = new MultiConnectionDataMessage(connectionIds, SerializeAllProtocols(methodName, args)).WithTracingId();
var message = AppendMessageTracingId(new MultiConnectionDataMessage(connectionIds, SerializeAllProtocols(methodName, args)));
if (message.TracingId != null)
{
MessageLog.StartToSendMessageToConnections(Logger, message);
Expand All @@ -122,7 +121,7 @@ public override Task SendGroupAsync(string groupName, string methodName, object[
throw new ArgumentException(NullOrEmptyStringErrorMessage, nameof(methodName));
}

var message = new GroupBroadcastDataMessage(groupName, null, SerializeAllProtocols(methodName, args)).WithTracingId();
var message = AppendMessageTracingId(new GroupBroadcastDataMessage(groupName, null, SerializeAllProtocols(methodName, args)));
if (message.TracingId != null)
{
MessageLog.StartToBroadcastMessageToGroup(Logger, message);
Expand All @@ -142,7 +141,7 @@ public override Task SendGroupsAsync(IReadOnlyList<string> groupNames, string me
throw new ArgumentException(NullOrEmptyStringErrorMessage, nameof(methodName));
}

var message = new MultiGroupBroadcastDataMessage(groupNames, SerializeAllProtocols(methodName, args)).WithTracingId();
var message = AppendMessageTracingId(new MultiGroupBroadcastDataMessage(groupNames, SerializeAllProtocols(methodName, args)));
if (message.TracingId != null)
{
MessageLog.StartToBroadcastMessageToGroups(Logger, message);
Expand All @@ -164,7 +163,7 @@ public override Task SendGroupExceptAsync(string groupName, string methodName, o
throw new ArgumentException(NullOrEmptyStringErrorMessage, nameof(methodName));
}

var message = new GroupBroadcastDataMessage(groupName, excludedIds, SerializeAllProtocols(methodName, args)).WithTracingId();
var message = AppendMessageTracingId(new GroupBroadcastDataMessage(groupName, excludedIds, SerializeAllProtocols(methodName, args)));
if (message.TracingId != null)
{
MessageLog.StartToBroadcastMessageToGroup(Logger, message);
Expand All @@ -184,7 +183,7 @@ public override Task SendUserAsync(string userId, string methodName, object[] ar
throw new ArgumentException(NullOrEmptyStringErrorMessage, nameof(methodName));
}

var message = new UserDataMessage(userId, SerializeAllProtocols(methodName, args)).WithTracingId();
var message = AppendMessageTracingId(new UserDataMessage(userId, SerializeAllProtocols(methodName, args)));
if (message.TracingId != null)
{
MessageLog.StartToSendMessageToUser(Logger, message);
Expand All @@ -205,7 +204,7 @@ public override Task SendUsersAsync(IReadOnlyList<string> userIds, string method
throw new ArgumentException(NullOrEmptyStringErrorMessage, nameof(methodName));
}

var message = new MultiUserDataMessage(userIds, SerializeAllProtocols(methodName, args)).WithTracingId();
var message = AppendMessageTracingId(new MultiUserDataMessage(userIds, SerializeAllProtocols(methodName, args)));
if (message.TracingId != null)
{
MessageLog.StartToSendMessageToUsers(Logger, message);
Expand All @@ -225,7 +224,7 @@ public override Task AddToGroupAsync(string connectionId, string groupName, Canc
throw new ArgumentException(NullOrEmptyStringErrorMessage, nameof(groupName));
}

var message = new JoinGroupWithAckMessage(connectionId, groupName).WithTracingId();
var message = AppendMessageTracingId(new JoinGroupWithAckMessage(connectionId, groupName));
if (message.TracingId != null)
{
MessageLog.StartToAddConnectionToGroup(Logger, message);
Expand All @@ -245,7 +244,7 @@ public override Task RemoveFromGroupAsync(string connectionId, string groupName,
throw new ArgumentException(NullOrEmptyStringErrorMessage, nameof(groupName));
}

var message = new LeaveGroupWithAckMessage(connectionId, groupName).WithTracingId();
var message = AppendMessageTracingId(new LeaveGroupWithAckMessage(connectionId, groupName));
if (message.TracingId != null)
{
MessageLog.StartToRemoveConnectionFromGroup(Logger, message);
Expand Down
35 changes: 14 additions & 21 deletions src/Microsoft.Azure.SignalR/ServerConnections/ServiceConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ private async Task ProcessClientConnectionAsync(ClientConnectionContext connecti
var transport = ProcessOutgoingMessagesAsync(connection, connection.OutgoingAborted);

// Waiting for the application to shutdown so we can clean up the connection
var app = ProcessIncomingMessageAsync(connection);
var app = ProcessApplicationTaskAsyncCore(connection);

var task = await Task.WhenAny(app, transport);

Expand Down Expand Up @@ -224,8 +224,7 @@ private async Task ProcessClientConnectionAsync(ClientConnectionContext connecti

try
{
// Wait on the application task to complete
// We wait gracefully here to be consistent with self-host SignalR
// always wait for the application to complete
await app;
}
catch (Exception e)
Expand Down Expand Up @@ -373,23 +372,6 @@ private void AddClientConnection(ClientConnectionContext connection, OpenConnect
_connectionIds.TryAdd(connection.ConnectionId, instanceId);
}

private async Task ProcessIncomingMessageAsync(ClientConnectionContext connection)
{
// Wait for the application task to complete
// application task can end when exception, or Context.Abort() from hub
var app = ProcessApplicationTaskAsyncCore(connection);

var task = await Task.WhenAny(app, Task.Delay(_closeTimeOutMilliseconds));

if (!app.IsCompleted)
{
Log.DetectedLongRunningApplicationTask(Logger, connection.ConnectionId);
}

// always wait for the application to complete
await app;
}

private async Task ProcessApplicationTaskAsyncCore(ClientConnectionContext connection)
{
Exception exception = null;
Expand Down Expand Up @@ -427,7 +409,18 @@ private async Task PerformDisconnectAsyncCore(string connectionId)
connection.CompleteIncoming();

// wait for the connection's lifetime task to end
await connection.LifetimeTask;
var lifetime = connection.LifetimeTask;

// Wait on the application task to complete
// We wait gracefully here to be consistent with self-host SignalR
await Task.WhenAny(lifetime, Task.Delay(_closeTimeOutMilliseconds));

if (!lifetime.IsCompleted)
{
Log.DetectedLongRunningApplicationTask(Logger, connectionId);
}

await lifetime;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Testing.xunit;
using Microsoft.Azure.SignalR.Tests.Common;
Expand Down
4 changes: 2 additions & 2 deletions test/Microsoft.Azure.SignalR.AspNet.E2ETests/TestClientSet.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.AspNet.SignalR.Client;
using Microsoft.Azure.SignalR.Tests.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR.Client;
using Microsoft.Azure.SignalR.Tests.Common;
using Xunit.Abstractions;

namespace Microsoft.Azure.SignalR.AspNet.Tests
Expand Down
2 changes: 1 addition & 1 deletion test/Microsoft.Azure.SignalR.AspNet.E2ETests/TestHub.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Threading.Tasks;
using Microsoft.AspNet.SignalR;
using Microsoft.Azure.SignalR.Tests.Common;
using System.Threading.Tasks;

namespace Microsoft.Azure.SignalR.AspNet.Tests
{
Expand Down
10 changes: 5 additions & 5 deletions test/Microsoft.Azure.SignalR.AspNet.E2ETests/TestServer.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.AspNet.SignalR;
using Microsoft.Azure.SignalR.Tests.Common;
using Microsoft.Extensions.Logging;
using Microsoft.Owin.Hosting;
using Owin;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR;
using Microsoft.Azure.SignalR.Tests.Common;
using Microsoft.Extensions.Logging;
using Microsoft.Owin.Hosting;
using Owin;
using Xunit.Abstractions;

namespace Microsoft.Azure.SignalR.AspNet.Tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using System.Web.WebSockets;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Transports;
using Microsoft.Azure.SignalR.AspNet.Tests.TestHubs;
using Microsoft.Azure.SignalR.Protocol;
using Microsoft.Azure.SignalR.Tests.Common;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Testing;
using Microsoft.Extensions.Primitives;
using Xunit;
using Xunit.Abstractions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.SignalR.Tests.Common;
using Microsoft.IdentityModel.Tokens;
using Xunit;

Expand Down
Loading

0 comments on commit a7aef3e

Please sign in to comment.