public async Task <IBattleHandlerSubscription> GetBattleHandlerAndSubscribeAsync(long battleId) { while (true) { var battleHandlerLazy = _battleHandlers.GetOrAdd( key: battleId, valueFactory: key => new AsyncLazy <BattleHandler>( factory: () => _battleHandlerFactory.CreateBattleHandlerAsync(battleId), flags: AsyncLazyFlags.RetryOnFailure)); var battleHandler = await battleHandlerLazy; if (battleHandler == null) { throw new InvalidOperationException($"Battle {battleId} is not found."); } battleHandler.IncrementSubscriptionCounter(); if (battleHandler.IsClosing) { battleHandler.DecrementSubscriptionCounter(); continue; } var subscription = new BattleHandlerSubscription(battleHandler); return(subscription); } }
public async override Task OnDisconnectedAsync(Exception exception) { foreach (var subscription in Subscriptions.Values) { subscription.Dispose(); } Subscriptions.Clear(); BattleHandlerSubscription.Dispose(); await base.OnDisconnectedAsync(exception); }