Skip to content

Commit

Permalink
Prevent Orleans + Kestrel from interfering with each other's networki…
Browse files Browse the repository at this point in the history
…ng services (#6042)
  • Loading branch information
ReubenBond authored and sergeybykov committed Oct 15, 2019
1 parent fd546a0 commit f9d525d
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 13 deletions.
6 changes: 5 additions & 1 deletion src/Orleans.Core/Core/DefaultClientServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ public static void AddDefaultServices(IClientBuilder builder, IServiceCollection
// Networking
services.TryAddSingleton<ConnectionManager>();
services.AddSingleton<ILifecycleParticipant<IClusterClientLifecycle>, ConnectionManagerLifecycleAdapter<IClusterClientLifecycle>>();
services.TryAddSingleton<IConnectionFactory, SocketConnectionFactory>();

services.AddSingletonKeyedService<object, IConnectionFactory>(
ClientOutboundConnectionFactory.ServicesKey,
(sp, key) => ActivatorUtilities.CreateInstance<SocketConnectionFactory>(sp));

services.TryAddTransient<IMessageSerializer>(sp => ActivatorUtilities.CreateInstance<MessageSerializer>(sp,
sp.GetRequiredService<IOptions<ClientMessagingOptions>>().Value.MaxMessageHeaderSize,
sp.GetRequiredService<IOptions<ClientMessagingOptions>>().Value.MaxMessageBodySize));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Orleans.Runtime.Messaging
{
internal sealed class ClientOutboundConnectionFactory : ConnectionFactory, IConnectionDirectionFeature
{
internal static readonly object ServicesKey = new object();
private readonly IServiceProvider serviceProvider;
private readonly ClientConnectionOptions clientConnectionOptions;
private readonly MessageFactory messageFactory;
Expand All @@ -22,10 +23,9 @@ public ClientOutboundConnectionFactory(
IServiceProvider serviceProvider,
IOptions<ConnectionOptions> connectionOptions,
IOptions<ClientConnectionOptions> clientConnectionOptions,
IConnectionFactory connectionFactory,
MessageFactory messageFactory,
INetworkingTrace trace)
: base(connectionFactory, serviceProvider, connectionOptions)
: base(serviceProvider.GetRequiredServiceByKey<object, IConnectionFactory>(ServicesKey), serviceProvider, connectionOptions)
{
this.serviceProvider = serviceProvider;
this.clientConnectionOptions = clientConnectionOptions.Value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.AspNetCore.Connections;

namespace Orleans.Configuration
Expand Down
13 changes: 11 additions & 2 deletions src/Orleans.Runtime/Hosting/DefaultSiloServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,17 @@ internal static void AddDefaultServices(IApplicationPartManager applicationPartM
services.TryAddSingleton<ConnectionManager>();
services.AddSingleton<ILifecycleParticipant<ISiloLifecycle>, ConnectionManagerLifecycleAdapter<ISiloLifecycle>>();
services.AddSingleton<ILifecycleParticipant<ISiloLifecycle>, SiloConnectionMaintainer>();
services.TryAddSingleton<IConnectionFactory, SocketConnectionFactory>();
services.TryAddSingleton<IConnectionListenerFactory, SocketConnectionListenerFactory>();

services.AddSingletonKeyedService<object, IConnectionFactory>(
SiloConnectionFactory.ServicesKey,
(sp, key) => ActivatorUtilities.CreateInstance<SocketConnectionFactory>(sp));
services.AddSingletonKeyedService<object, IConnectionListenerFactory>(
SiloConnectionListener.ServicesKey,
(sp, key) => ActivatorUtilities.CreateInstance<SocketConnectionListenerFactory>(sp));
services.AddSingletonKeyedService<object, IConnectionListenerFactory>(
GatewayConnectionListener.ServicesKey,
(sp, key) => ActivatorUtilities.CreateInstance<SocketConnectionListenerFactory>(sp));

services.TryAddTransient<IMessageSerializer>(sp => ActivatorUtilities.CreateInstance<MessageSerializer>(sp,
sp.GetRequiredService<IOptions<SiloMessagingOptions>>().Value.MaxMessageHeaderSize,
sp.GetRequiredService<IOptions<SiloMessagingOptions>>().Value.MaxMessageBodySize));
Expand Down
4 changes: 2 additions & 2 deletions src/Orleans.Runtime/Networking/GatewayConnectionListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Orleans.Runtime.Messaging
{
internal sealed class GatewayConnectionListener : ConnectionListener, ILifecycleParticipant<ISiloLifecycle>
{
internal static readonly object ServicesKey = new object();
private readonly INetworkingTrace trace;
private readonly ILocalSiloDetails localSiloDetails;
private readonly IOptions<MultiClusterOptions> multiClusterOptions;
Expand All @@ -25,7 +26,6 @@ public GatewayConnectionListener(
IServiceProvider serviceProvider,
IOptions<ConnectionOptions> connectionOptions,
IOptions<SiloConnectionOptions> siloConnectionOptions,
IConnectionListenerFactory listenerFactory,
MessageFactory messageFactory,
OverloadDetector overloadDetector,
Gateway gateway,
Expand All @@ -35,7 +35,7 @@ public GatewayConnectionListener(
IOptions<EndpointOptions> endpointOptions,
MessageCenter messageCenter,
ConnectionManager connectionManager)
: base(serviceProvider, listenerFactory, connectionOptions, connectionManager, trace)
: base(serviceProvider, serviceProvider.GetRequiredServiceByKey<object, IConnectionListenerFactory>(ServicesKey), connectionOptions, connectionManager, trace)
{
this.siloConnectionOptions = siloConnectionOptions.Value;
this.messageFactory = messageFactory;
Expand Down
4 changes: 2 additions & 2 deletions src/Orleans.Runtime/Networking/SiloConnectionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Orleans.Runtime.Messaging
{
internal sealed class SiloConnectionFactory : ConnectionFactory
{
internal static readonly object ServicesKey = new object();
private readonly INetworkingTrace trace;
private readonly ILocalSiloDetails localSiloDetails;
private readonly IServiceProvider serviceProvider;
Expand All @@ -25,11 +26,10 @@ public SiloConnectionFactory(
IServiceProvider serviceProvider,
IOptions<ConnectionOptions> connectionOptions,
IOptions<SiloConnectionOptions> siloConnectionOptions,
IConnectionFactory connectionFactory,
MessageFactory messageFactory,
INetworkingTrace trace,
ILocalSiloDetails localSiloDetails)
: base(connectionFactory, serviceProvider, connectionOptions)
: base(serviceProvider.GetRequiredServiceByKey<object, IConnectionFactory>(ServicesKey), serviceProvider, connectionOptions)
{
this.serviceProvider = serviceProvider;
this.siloConnectionOptions = siloConnectionOptions.Value;
Expand Down
4 changes: 2 additions & 2 deletions src/Orleans.Runtime/Networking/SiloConnectionListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Orleans.Runtime.Messaging
{
internal sealed class SiloConnectionListener : ConnectionListener, ILifecycleParticipant<ISiloLifecycle>
{
internal static readonly object ServicesKey = new object();
private readonly INetworkingTrace trace;
private readonly ILocalSiloDetails localSiloDetails;
private readonly SiloConnectionOptions siloConnectionOptions;
Expand All @@ -23,14 +24,13 @@ public SiloConnectionListener(
IServiceProvider serviceProvider,
IOptions<ConnectionOptions> connectionOptions,
IOptions<SiloConnectionOptions> siloConnectionOptions,
IConnectionListenerFactory listenerFactory,
MessageCenter messageCenter,
MessageFactory messageFactory,
INetworkingTrace trace,
IOptions<EndpointOptions> endpointOptions,
ILocalSiloDetails localSiloDetails,
ConnectionManager connectionManager)
: base(serviceProvider, listenerFactory, connectionOptions, connectionManager, trace)
: base(serviceProvider, serviceProvider.GetRequiredServiceByKey<object, IConnectionListenerFactory>(ServicesKey), connectionOptions, connectionManager, trace)
{
this.siloConnectionOptions = siloConnectionOptions.Value;
this.messageCenter = messageCenter;
Expand Down

0 comments on commit f9d525d

Please sign in to comment.