44using Hive . Both . General . Dispatchers ;
55using Hive . Network . Abstractions ;
66using Hive . Network . Abstractions . Session ;
7+ using Microsoft . Extensions . DependencyInjection ;
78using Microsoft . Extensions . Hosting ;
89using Microsoft . Extensions . Logging ;
910
@@ -14,14 +15,17 @@ namespace ConnectX.Server.Managers;
1415public class ClientManager : BackgroundService
1516{
1617 private readonly IDispatcher _dispatcher ;
18+ private readonly IServiceScopeFactory _serviceScopeFactory ;
1719 private readonly ILogger _logger ;
1820 private readonly ConcurrentDictionary < SessionId , WatchDog > _watchDogMapping = new ( ) ;
1921
2022 public ClientManager (
2123 IDispatcher dispatcher ,
24+ IServiceScopeFactory serviceScopeFactory ,
2225 ILogger < ClientManager > logger )
2326 {
2427 _dispatcher = dispatcher ;
28+ _serviceScopeFactory = serviceScopeFactory ;
2529 _logger = logger ;
2630
2731 _dispatcher . AddHandler < ShutdownMessage > ( OnReceivedShutdownMessage ) ;
@@ -75,10 +79,18 @@ private void OnReceivedHeartBeat(MessageContext<HeartBeat> ctx)
7579 ctx . Dispatcher . SendAsync ( ctx . FromSession , new ShutdownMessage ( ) ) . Forget ( ) ;
7680 ctx . Dispatcher . RemoveHandler < HeartBeat > ( OnReceivedHeartBeat ) ;
7781 return ;
78- }
82+ }
83+
84+ watchDog . Received ( ) ;
85+
86+ using var scope = _serviceScopeFactory . CreateScope ( ) ;
87+ var interconnectServerManager = scope . ServiceProvider . GetRequiredService < InterconnectServerManager > ( ) ;
88+
89+ // if the session is registered for interconnect, we don't need to send heartbeat back
90+ if ( interconnectServerManager . IsServerRegisteredForInterconnect ( ctx . FromSession . Id ) )
91+ return ;
7992
8093 ctx . Dispatcher . SendAsync ( ctx . FromSession , new HeartBeat ( ) ) . Forget ( ) ;
81- watchDog . Received ( ) ;
8294 }
8395
8496 protected override async Task ExecuteAsync ( CancellationToken stoppingToken )
0 commit comments