77using System . Threading ;
88using System . Threading . Tasks ;
99using ManagedCode . Orleans . SignalR . Core . Config ;
10+ using ManagedCode . Orleans . SignalR . Core . Helpers ;
1011using ManagedCode . Orleans . SignalR . Core . Interfaces ;
1112using ManagedCode . Orleans . SignalR . Core . Models ;
1213using ManagedCode . Orleans . SignalR . Core . SignalR . Observers ;
@@ -20,40 +21,41 @@ namespace ManagedCode.Orleans.SignalR.Core.SignalR;
2021
2122public class OrleansHubLifetimeManager < THub > : HubLifetimeManager < THub > where THub : Hub
2223{
23- private readonly ILogger _logger ;
2424 private readonly IClusterClient _clusterClient ;
2525 private readonly HubConnectionStore _connections = new ( ) ;
26- private readonly IOptions < HubOptions > ? _globalHubOptions ;
27- private readonly IOptions < HubOptions < THub > > ? _hubOptions ;
28- private readonly IOptions < OrleansSignalROptions > _options ;
26+ private readonly IOptions < HubOptions > _globalHubOptions ;
27+ private readonly IOptions < HubOptions < THub > > _hubOptions ;
28+ private readonly ILogger _logger ;
29+ private readonly IOptions < OrleansSignalROptions > _orleansSignalOptions ;
2930
30- public OrleansHubLifetimeManager ( ILogger < OrleansHubLifetimeManager < THub > > logger ,
31- IOptions < OrleansSignalROptions > options , IClusterClient clusterClient , IHubProtocolResolver hubProtocolResolver ,
32- IOptions < HubOptions > ? globalHubOptions , IOptions < HubOptions < THub > > ? hubOptions )
31+ public OrleansHubLifetimeManager ( ILogger < OrleansHubLifetimeManager < THub > > logger , IClusterClient clusterClient ,
32+ IHubProtocolResolver hubProtocolResolver , IOptions < OrleansSignalROptions > orleansSignalOptions ,
33+ IOptions < HubOptions > globalHubOptions , IOptions < HubOptions < THub > > hubOptions )
3334 {
3435 _logger = logger ;
35- _options = options ;
36- _clusterClient = clusterClient ;
36+ _orleansSignalOptions = orleansSignalOptions ;
3737 _globalHubOptions = globalHubOptions ;
3838 _hubOptions = hubOptions ;
39+ _clusterClient = clusterClient ;
3940 }
4041
4142 public override async Task OnConnectedAsync ( HubConnectionContext connection )
4243 {
43- var connectionHolderGrain = NameHelperGenerator . GetConnectionHolderGrain < THub > ( _clusterClient ) ;
44-
4544 var subscription = CreateConnectionObserver ( connection ) ;
4645
47- //Subscribe the instance to receive messages.
48- await connectionHolderGrain . AddConnection ( connection . ConnectionId , subscription . Reference )
49- . ConfigureAwait ( false ) ;
50- subscription . Grains . Add ( connectionHolderGrain ) ;
46+ if ( _orleansSignalOptions . Value . KeepEachConnectionAlive )
47+ {
48+ var connectionHolderGrain = NameHelperGenerator . GetConnectionHolderGrain < THub > ( _clusterClient ) ;
49+ await connectionHolderGrain . AddConnection ( connection . ConnectionId , subscription . Reference )
50+ . ConfigureAwait ( false ) ;
51+ subscription . AddGrain ( connectionHolderGrain ) ;
52+ }
5153
5254 if ( ! string . IsNullOrEmpty ( connection . UserIdentifier ) )
5355 {
5456 var userGrain = NameHelperGenerator . GetSignalRUserGrain < THub > ( _clusterClient , connection . UserIdentifier ! ) ;
5557 await userGrain . AddConnection ( connection . ConnectionId , subscription . Reference ) . ConfigureAwait ( false ) ;
56- subscription . Grains . Add ( userGrain ) ;
58+ subscription . AddGrain ( userGrain ) ;
5759 }
5860
5961 _connections . Add ( connection ) ;
@@ -162,7 +164,7 @@ public override async Task AddToGroupAsync(string connectionId, string groupName
162164 var groupGrain = NameHelperGenerator . GetSignalRGroupGrain < THub > ( _clusterClient , groupName ) ;
163165 await groupGrain . AddConnection ( connectionId , subscription . Reference ) . ConfigureAwait ( false ) ;
164166
165- subscription . Grains . Add ( groupGrain ) ;
167+ subscription . AddGrain ( groupGrain ) ;
166168 }
167169
168170 public override async Task RemoveFromGroupAsync ( string connectionId , string groupName ,
@@ -175,7 +177,7 @@ public override async Task RemoveFromGroupAsync(string connectionId, string grou
175177 var groupGrain = NameHelperGenerator . GetSignalRGroupGrain < THub > ( _clusterClient , groupName ) ;
176178 await groupGrain . RemoveConnection ( connectionId , subscription . Reference ) . ConfigureAwait ( false ) ;
177179
178- subscription . Grains . Remove ( groupGrain ) ;
180+ subscription . RemoveGrain ( groupGrain ) ;
179181 }
180182
181183 public override async Task < T > InvokeConnectionAsync < T > ( string connectionId , string methodName , object ? [ ] args ,
@@ -206,7 +208,7 @@ public override async Task<T> InvokeConnectionAsync<T>(string connectionId, stri
206208 } ) ;
207209
208210 var invocationGrain = NameHelperGenerator . GetInvocationGrain < THub > ( _clusterClient , invocationId ) ;
209- subscription . Grains . Add ( invocationGrain ) ;
211+ subscription . AddGrain ( invocationGrain ) ;
210212 await invocationGrain . AddInvocation ( subscription . Reference ,
211213 new InvocationInfo ( connectionId , invocationId , typeof ( T ) ) ) ;
212214
@@ -268,11 +270,20 @@ await NameHelperGenerator.GetInvocationGrain<THub>(_clusterClient, result.Invoca
268270
269271 public override bool TryGetReturnType ( string invocationId , [ NotNullWhen ( true ) ] out Type ? type )
270272 {
271- var result = NameHelperGenerator . GetInvocationGrain < THub > ( _clusterClient , invocationId ) . TryGetReturnType ( )
272- . Result ;
273+ var returnType = NameHelperGenerator . GetInvocationGrain < THub > ( _clusterClient , invocationId ) . TryGetReturnType ( ) ;
274+
275+ var timeSpan =
276+ TimeIntervalHelper . GetClientTimeoutInterval ( _orleansSignalOptions , _globalHubOptions , _hubOptions ) ;
277+ Task . WaitAny ( returnType , Task . Delay ( timeSpan * 0.8 ) ) ;
273278
274- type = result . GetReturnType ( ) ;
275- return result . Result ;
279+ if ( returnType . IsCompleted )
280+ {
281+ type = returnType . Result . GetReturnType ( ) ;
282+ return returnType . Result . Result ;
283+ }
284+
285+ type = null ;
286+ return false ;
276287 }
277288
278289 private Subscription CreateConnectionObserver ( HubConnectionContext connection )
@@ -282,15 +293,12 @@ private Subscription CreateConnectionObserver(HubConnectionContext connection)
282293 return subscription ;
283294 }
284295
296+
285297 private Subscription CreateSubscription ( Func < HubMessage , Task > ? onNextAction )
286298 {
287- TimeSpan timeSpan = _globalHubOptions . Value . KeepAliveInterval . Value ;
288- if ( timeSpan > _hubOptions . Value . KeepAliveInterval )
289- {
290- timeSpan = _hubOptions . Value . KeepAliveInterval . Value ;
291- }
292-
293- var subscription = new Subscription ( new SignalRObserver ( onNextAction ) , timeSpan ) ;
299+ var timeSpan =
300+ TimeIntervalHelper . GetClientTimeoutInterval ( _orleansSignalOptions , _globalHubOptions , _hubOptions ) ;
301+ var subscription = new Subscription ( new SignalRObserver ( onNextAction ) , timeSpan * 0.8 ) ;
294302 var reference = _clusterClient . CreateObjectReference < ISignalRObserver > ( subscription . GetObserver ( ) ) ;
295303 subscription . SetReference ( reference ) ;
296304 return subscription ;
0 commit comments