-
Notifications
You must be signed in to change notification settings - Fork 103
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
Add singleton factories to create instance per hub in management SDK #1070
Conversation
b6c5984
to
46f2113
Compare
...Microsoft.Azure.SignalR.Management/HubInstanceFactories/IServiceHubLifetimeManagerFactory.cs
Outdated
Show resolved
Hide resolved
...Microsoft.Azure.SignalR.Management/HubInstanceFactories/IServiceHubLifetimeManagerFactory.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Azure.SignalR.Management/HubInstanceFactories/RestHubLifetimeManagerFactory.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Azure.SignalR.Management/HubInstanceFactories/ServiceHubContextFactory.cs
Outdated
Show resolved
Hide resolved
...crosoft.Azure.SignalR.Management/HubInstanceFactories/WebSocketsHubLifetimeManagerFactory.cs
Outdated
Show resolved
Hide resolved
def780d
to
b0ed9c4
Compare
src/Microsoft.Azure.SignalR.Management/DependencyInjectionExtensions.cs
Outdated
Show resolved
Hide resolved
b0ed9c4
to
fe5b720
Compare
src/Microsoft.Azure.SignalR.Management/DependencyInjectionExtensions.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Azure.SignalR.Management/DependencyInjectionExtensions.cs
Outdated
Show resolved
Hide resolved
public async Task<IServiceHubContext> Create(string hubName, ILoggerFactory loggerFactory = null, CancellationToken cancellationToken = default) | ||
{ | ||
var manager = await _managerFactory.Create(hubName, cancellationToken, loggerFactory); | ||
var servicesPerHub = new ServiceCollection(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why the service collection is for each hub? isn't it enough for each service manager?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because IHubContext<Hub>
is for each hub, and one ServiceManager
could have multiple HubContext
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah I am also confused here, why DI ctor not working ? public ServiceHubContextFactory(ServiceHubLifetimeManagerFactory managerFactory, IHubContext<Hub> context)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For two reasons:
IHubContext<Hub>
is not singleton. And its implHubContext<>
is an internal class, so we can't doservices.AddTransient(typeof(IHubContext<>),typeof(HubContext<>));
.HubContext<Hub>
depends onHubLiftTimeManager<>
, which depends on hub name in our impl.
var container = containerFactory.Create(hubName,loggerFactoryPerHub); | ||
var connectionManager = new ServiceConnectionManager<Hub>(); | ||
connectionManager.SetServiceConnection(container); | ||
_ = connectionManager.StartAsync(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This class is a factory for creating ServiceHubLifetimeManager, but it also start connection manager here. start should be called in other place
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strictly speaking, CreateServiceHubContext()
is a method for creating ServiceHubContext
, but we start connection manager inside it, and this is unavoidable. Given this, I don't think starting connection manager in another factory is unacceptable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about moving the preparison for connection manager into WebSocketsHubLifetimeManager? So that WebSocketsHubLifetimeManager is no knowledge of hub name, and is also able to put into service collection, and make it transient? And set the connection container and start the container in ServiceHubContextFactory.CreateAsync like the old version? @vicancy
var containerFactory = _serviceProvider.GetRequiredService<MultiEndpointConnectionContainerFactory>();
var container = containerFactory.Create(hubName,loggerFactoryPerHub);
var connectionManager = new ServiceConnectionManager<Hub>();
connectionManager.SetServiceConnection(container);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I understand correctly, WebSocketsHubLifetimeManager
is transient means that IServiceHubContext
is transient too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you plan to get a new one for each request?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The key is that HubContext<>
is an internal class, and we can't add it as transient.
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since you already put most of the class as a service into service collection, why not put WebSocketsHubLifetimeManager and RestHubLifetimeManager into service collection as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WebSocketsHubLifetimeManager
and RestHubLifetimeManager
are not singletons, they are for hub. And we don't know the hub name until CreateServiceHubContext(...)
is called, while hub name is bound to ServiceHubLifetimeManager
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thinks it's better prepare all the thing before actually start the container, so put the startAsync to ServiceHubContextFactory.CreateAsync
is better
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the actual difference between starting connection manager in ServiceHubLifetimeManagerFactory
and ServiceHubContextFactory
? Now ServiceHubContextFactory
doesn't need to know the actual impl of IServiceHubLifetimeManager
. I think to put connectionManager.StartAsync()
in ServiceHubContextFactory
breaks the hierarchy of the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what I mean is to put the used classes into service collections if possible, when everything is ready, start the connections
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not singleton and we can't create connectionManager until we know hub name?
src/Microsoft.Azure.SignalR.Management/HubInstanceFactories/ServiceHubLifetimeManagerFactory.cs
Outdated
Show resolved
Hide resolved
.AddSingleton<MultiEndpointConnectionContainerFactory>() | ||
.AddSingleton<IConfigureOptions<HubOptions>, ManagementHubOptionsSetup>(); | ||
|
||
services.AddLogging() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shall we let users decide if they AddLogging
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about replace AddLogging()
with services.TryAdd(ServiceDescriptor.Singleton<ILoggerFactory, LoggerFactory>());
to make sure we have a default impl of ILoggerFctory
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can follow the general practice for a library package, for example, does SignalR pakage add default logger?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SignalR package doesn't addLogger. But since we already use ILoggerFactory, why we don't add default logger for user? If no default loggerFactory is set, program throws error when users don't specify logger. User can add their own loggerFactory and the default one won't take effect.
public async Task<IServiceHubContext> Create(string hubName, ILoggerFactory loggerFactory = null, CancellationToken cancellationToken = default) | ||
{ | ||
var manager = await _managerFactory.Create(hubName, cancellationToken, loggerFactory); | ||
var servicesPerHub = new ServiceCollection(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah I am also confused here, why DI ctor not working ? public ServiceHubContextFactory(ServiceHubLifetimeManagerFactory managerFactory, IHubContext<Hub> context)
src/Microsoft.Azure.SignalR.Management/HubInstanceFactories/ServiceHubLifetimeManagerFactory.cs
Outdated
Show resolved
Hide resolved
test/Microsoft.Azure.SignalR.E2ETests/Management/ServiceHubContextE2EFacts.cs
Outdated
Show resolved
Hide resolved
test/Microsoft.Azure.SignalR.E2ETests/Management/ServiceHubContextE2EFacts.cs
Outdated
Show resolved
Hide resolved
var serviceHubContext = await serviceManager.CreateHubContextAsync("hub", loggerFactory); | ||
var connectionContainer = ((ServiceHubContext)serviceHubContext).ServiceProvider.GetRequiredService<IServiceConnectionContainer>(); | ||
var serviceHubContext = await serviceManager.CreateHubContextAsync("hub", default); | ||
var connectionContainer = ((ServiceHubContext)serviceHubContext).ServiceProvider.GetRequiredService<IServiceConnectionContainer>();//TODO |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove this //TODO
comments with little meanings, or add more details into it about what todo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test is for reconnection function and doesn't work anymore, because connectionContainer
isn't in the service provider of a ServiceHubContext
and it's not convenient to add it to the service collection per hub. I think ideally this test should do for connection container class. I intend to add a unit test for connection container separately latter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Summary of the changes
GenerateClientAccessToken
to get client endpoint and access token.ILoggerFactory
specified inCreateHubContextAsync
is only passed to per hub instance. Global singleton instances shares one ILoggerFactory.Usage example
Method1: use global service collection
Method2: use
ServiceManagerBuilder
In this way, dynamic configuration is not supported.