Skip to content

Commit

Permalink
Add singleton factories to create instance per hub in management SDK (#…
Browse files Browse the repository at this point in the history
…1070)

* Allow customization of the setup of options

* Add singleton factories to create instance per hub in management SDK
  • Loading branch information
Y-Sindo authored Nov 17, 2020
1 parent 346970a commit 1dcfaab
Show file tree
Hide file tree
Showing 14 changed files with 273 additions and 203 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using System.Net.Http;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.Azure.SignalR
{
Expand All @@ -13,21 +12,13 @@ internal class RestClientFactory
private readonly string _userAgent;
private readonly string _serverName;

internal RestClientFactory(string userAgent)
public RestClientFactory(string userAgent, IHttpClientFactory httpClientFactory)
{
var serviceCollection = new ServiceCollection()
.AddHttpClient();
_httpClientFactory = serviceCollection.BuildServiceProvider().GetRequiredService<IHttpClientFactory>();
_httpClientFactory = httpClientFactory;
_userAgent = userAgent;
_serverName = RestApiAccessTokenGenerator.GenerateServerName();
}

protected RestClientFactory(string userAgent, IHttpClientFactory httpClientFactory)
{
_userAgent = userAgent;
_httpClientFactory = httpClientFactory;
}

protected virtual HttpClient CreateHttpClient() => _httpClientFactory.CreateClient();

internal SignalRServiceRestClient Create(ServiceEndpoint endpoint)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Primitives;

namespace Microsoft.Azure.SignalR.Management.Configuration
namespace Microsoft.Azure.SignalR.Management
{
internal class ServiceManagerOptionsSetup : IConfigureOptions<ServiceManagerOptions>, IOptionsChangeTokenSource<ServiceManagerOptions>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.ComponentModel;
using System.Net.Http;
using System.Reflection;
using Microsoft.Azure.SignalR.Management.Configuration;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

Expand All @@ -16,17 +19,7 @@ namespace Microsoft.Azure.SignalR.Management
/// </summary>
public static IServiceCollection AddSignalRServiceManager(this IServiceCollection services)
{
services.AddSingleton<ServiceManagerOptionsSetup>()
.AddSingleton<IConfigureOptions<ServiceManagerOptions>>(sp => sp.GetService<ServiceManagerOptionsSetup>())
.AddSingleton<IOptionsChangeTokenSource<ServiceManagerOptions>>(sp => sp.GetService<ServiceManagerOptionsSetup>());
services.PostConfigure<ServiceManagerOptions>(o => o.ValidateOptions());
services.AddSingleton<ServiceManagerContextSetup>()
.AddSingleton<IConfigureOptions<ServiceManagerContext>>(sp => sp.GetService<ServiceManagerContextSetup>())
.AddSingleton<IOptionsChangeTokenSource<ServiceManagerContext>>(sp => sp.GetService<ServiceManagerContextSetup>());
services.AddSingleton<ServiceOptionsSetup>()
.AddSingleton<IConfigureOptions<ServiceOptions>>(sp => sp.GetService<ServiceOptionsSetup>())
.AddSingleton<IOptionsChangeTokenSource<ServiceOptions>>(sp => sp.GetService<ServiceOptionsSetup>());
return services.TrySetProductInfo();
return services.AddSignalRServiceManager<ServiceManagerOptionsSetup>();
}

/// <summary>
Expand All @@ -38,6 +31,43 @@ public static IServiceCollection AddSignalRServiceManager(this IServiceCollectio
return services.AddSignalRServiceManager();
}

/// <summary>
/// Adds the essential SignalR Service Manager services to the specified services collection.
/// </summary>
/// <remarks>Designed for Azure Function extension where the setup of <see cref="ServiceManagerOptions"/> is different from SDK</remarks>
/// <typeparam name="TOptionsSetup">The type of class used to setup <see cref="ServiceManagerOptions"/>. </typeparam>
[EditorBrowsable(EditorBrowsableState.Never)]
public static IServiceCollection AddSignalRServiceManager<TOptionsSetup>(this IServiceCollection services) where TOptionsSetup : class, IConfigureOptions<ServiceManagerOptions>, IOptionsChangeTokenSource<ServiceManagerOptions>
{
//cascade options setup
services.AddSingleton<TOptionsSetup>()
.AddSingleton<IConfigureOptions<ServiceManagerOptions>>(sp => sp.GetService<TOptionsSetup>())
.AddSingleton<IOptionsChangeTokenSource<ServiceManagerOptions>>(sp => sp.GetService<TOptionsSetup>());
services.PostConfigure<ServiceManagerOptions>(o => o.ValidateOptions());
services.AddSingleton<ServiceManagerContextSetup>()
.AddSingleton<IConfigureOptions<ServiceManagerContext>>(sp => sp.GetService<ServiceManagerContextSetup>())
.AddSingleton<IOptionsChangeTokenSource<ServiceManagerContext>>(sp => sp.GetService<ServiceManagerContextSetup>());

services.AddSignalR()
.AddAzureSignalR<ServiceOptionsSetup>();

//add dependencies for persistent mode only
services
.AddSingleton<ConnectionFactory>()
.AddSingleton<IConnectionFactory,ManagementConnectionFactory>()
.AddSingleton<ConnectionDelegate>((connectionContext) => Task.CompletedTask)
.AddSingleton<IServiceConnectionFactory, ServiceConnectionFactory>()
.AddSingleton<MultiEndpointConnectionContainerFactory>()
.AddSingleton<IConfigureOptions<HubOptions>, ManagementHubOptionsSetup>();

services.AddLogging()
.AddSingleton<ServiceHubContextFactory>()
.AddSingleton<ServiceHubLifetimeManagerFactory>();
services.AddSingleton<IServiceManager, ServiceManager>();
services.AddRestClientFactory();
return services.TrySetProductInfo();
}

/// <summary>
/// Adds product info to <see cref="ServiceManagerContext"/>
/// </summary>
Expand All @@ -53,7 +83,17 @@ private static IServiceCollection TrySetProductInfo(this IServiceCollection serv
{
var assembly = Assembly.GetExecutingAssembly();
var productInfo = ProductInfo.GetProductInfo(assembly);
return services.Configure<ServiceManagerContext>(o => o.ProductInfo = o.ProductInfo ?? productInfo);
return services.Configure<ServiceManagerContext>(o => o.ProductInfo ??= productInfo);
}

private static IServiceCollection AddRestClientFactory(this IServiceCollection services) => services
.AddHttpClient()
.AddSingleton(sp =>
{
var options = sp.GetRequiredService<IOptions<ServiceManagerContext>>().Value;
var productInfo = options.ProductInfo;
var httpClientFactory = sp.GetRequiredService<IHttpClientFactory>();
return new RestClientFactory(productInfo, httpClientFactory);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using Microsoft.Azure.SignalR.Common;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace Microsoft.Azure.SignalR.Management
{
internal class MultiEndpointConnectionContainerFactory
{
private readonly IServiceConnectionFactory _connectionFactory;
private readonly ILoggerFactory _loggerFactory;
private readonly IServiceEndpointManager _endpointManager;
private readonly int _connectionCount;
private readonly IEndpointRouter _router;

public MultiEndpointConnectionContainerFactory(IServiceConnectionFactory connectionFactory, ILoggerFactory loggerFactory, IServiceEndpointManager serviceEndpointManager, IOptions<ServiceManagerContext> options, IEndpointRouter router = null)
{
_connectionFactory = connectionFactory;
_loggerFactory = loggerFactory;
_endpointManager = serviceEndpointManager;
_connectionCount = options.Value.ConnectionCount;
_router = router;
}

public MultiEndpointServiceConnectionContainer Create(string hubName, ILoggerFactory loggerFactoryPerHub = null)
{
var loggerFactory = loggerFactoryPerHub ?? _loggerFactory;
return new MultiEndpointServiceConnectionContainer(
hubName,
endpoint => new WeakServiceConnectionContainer(_connectionFactory, _connectionCount, endpoint, loggerFactory.CreateLogger<WeakServiceConnectionContainer>()),
_endpointManager,
_router,
loggerFactory);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Microsoft.Azure.SignalR.Management
{
internal class ServiceHubContextFactory
{
private readonly ServiceHubLifetimeManagerFactory _managerFactory;

public ServiceHubContextFactory(ServiceHubLifetimeManagerFactory managerFactory)
{
_managerFactory = managerFactory;
}

public async Task<IServiceHubContext> CreateAsync(string hubName, ILoggerFactory loggerFactory = null, CancellationToken cancellationToken = default)
{
var manager = await _managerFactory.CreateAsync(hubName, cancellationToken, loggerFactory);
var servicesPerHub = new ServiceCollection();
servicesPerHub.AddSignalRCore();
servicesPerHub.AddSingleton((HubLifetimeManager<Hub>)manager);
var serviceProviderPerHub = servicesPerHub.BuildServiceProvider();
// The impl of IHubContext<Hub> we want is an internal class. We can only get it by this way.
var hubContext = serviceProviderPerHub.GetRequiredService<IHubContext<Hub>>();
return new ServiceHubContext(hubContext, manager, serviceProviderPerHub);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.ComponentModel;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace Microsoft.Azure.SignalR.Management
{
internal class ServiceHubLifetimeManagerFactory
{
private readonly IServiceProvider _serviceProvider;
private readonly MultiEndpointConnectionContainerFactory _connectionContainerFactory;
private readonly ServiceManagerContext _context;

public ServiceHubLifetimeManagerFactory(IServiceProvider sp, IOptions<ServiceManagerContext> context, MultiEndpointConnectionContainerFactory connectionContainerFactory)
{
_serviceProvider = sp;
_connectionContainerFactory = connectionContainerFactory;
_context = context.Value;
}

public async Task<IServiceHubLifetimeManager> CreateAsync(string hubName, CancellationToken cancellationToken, ILoggerFactory loggerFactoryPerHub = null)
{
switch (_context.ServiceTransportType)
{
case ServiceTransportType.Persistent:
{
var container = _connectionContainerFactory.Create(hubName, loggerFactoryPerHub);
var connectionManager = new ServiceConnectionManager<Hub>();
connectionManager.SetServiceConnection(container);
_ = connectionManager.StartAsync();
await container.ConnectionInitializedTask.OrTimeout(cancellationToken);
return loggerFactoryPerHub == null ? ActivatorUtilities.CreateInstance<WebSocketsHubLifetimeManager<Hub>>(_serviceProvider, connectionManager) : ActivatorUtilities.CreateInstance<WebSocketsHubLifetimeManager<Hub>>(_serviceProvider, connectionManager, loggerFactoryPerHub);
}
case ServiceTransportType.Transient:
{
return new RestHubLifetimeManager(hubName, _context.ServiceEndpoints.Single(), _context.ProductInfo, _context.ApplicationName);
}
default: throw new InvalidEnumArgumentException(nameof(ServiceManagerContext.ServiceTransportType), (int)_context.ServiceTransportType, typeof(ServiceTransportType));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Connections;
using Microsoft.Extensions.Options;

namespace Microsoft.Azure.SignalR.Management
{
internal class ManagementConnectionFactory : IConnectionFactory
{
private readonly string _productInfo;
private readonly IConnectionFactory _connectionFactory;
private readonly ConnectionFactory _connectionFactory;

public ManagementConnectionFactory(string productInfo, IConnectionFactory connectionFactory)
public ManagementConnectionFactory(IOptions<ServiceManagerContext> context, ConnectionFactory connectionFactory)
{
_productInfo = productInfo;
_productInfo = context.Value.ProductInfo;
_connectionFactory = connectionFactory;
}

Expand Down
Loading

0 comments on commit 1dcfaab

Please sign in to comment.