-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add singleton factories to create instance per hub in management SDK (#…
…1070) * Allow customization of the setup of options * Add singleton factories to create instance per hub in management SDK
- Loading branch information
Showing
14 changed files
with
273 additions
and
203 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
40 changes: 40 additions & 0 deletions
40
....Azure.SignalR.Management/HubInstanceFactories/MultiEndpointConnectionContainerFactory.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,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); | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/Microsoft.Azure.SignalR.Management/HubInstanceFactories/ServiceHubContextFactory.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,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); | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...crosoft.Azure.SignalR.Management/HubInstanceFactories/ServiceHubLifetimeManagerFactory.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,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)); | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.