private async Task SubscribeToGroupAsync(string groupChannel, HubConnectionStore groupConnections) { RedisLog.Subscribing(_logger, groupChannel); var channel = await _bus.SubscribeAsync(groupChannel); channel.OnMessage(async(channelMessage) => { try { var invocation = _protocol.ReadInvocation((byte[])channelMessage.Message); var tasks = new List <Task>(); foreach (var groupConnection in groupConnections) { if (invocation.ExcludedConnectionIds?.Contains(groupConnection.ConnectionId) == true) { continue; } tasks.Add(groupConnection.WriteAsync(invocation.Message).AsTask()); } await Task.WhenAll(tasks); } catch (Exception ex) { RedisLog.FailedWritingMessage(_logger, ex); } }); }
public static IEnumerable <HubConnectionContext> AsEnumerable(this HubConnectionStore connections) { foreach (var connection in connections) { yield return(connection); } }
private Task SubscribeToGroupAsync(string groupChannel, HubConnectionStore groupConnections) { RedisLog.Subscribing(_logger, groupChannel); var server = _options.ServerResovler.Resolve(_shardingServers, groupChannel); return(server.Subscriber.SubscribeAsync(groupChannel, async(_, data) => { try { var invocation = _protocol.ReadInvocation((byte[])data); var tasks = groupConnections.AsEnumerable() .Where(connection => { if (invocation.ExcludedConnectionIds == null) { return true; } return !invocation.ExcludedConnectionIds.Contains(connection.ConnectionId); }) .Select(connection => { var task = connection.WriteAsync(invocation.Message); return task.AsTask(); }); await Task.WhenAll(tasks); } catch (Exception ex) { RedisLog.FailedWritingMessage(_logger, ex); } })); }
public HubLifetimeManagerOptions() { ServerName = $"{Environment.MachineName}_{NewId.NextGuid():N}"; RequestTimeout = TimeSpan.FromSeconds(20); ConnectionStore = new HubConnectionStore(); GroupsSubscriptionManager = new MassTransitSubscriptionManager(); UsersSubscriptionManager = new MassTransitSubscriptionManager(); }
/// <summary> /// Constructor /// </summary> /// <param name="bus"></param> /// <param name="hubProtocolResolver"></param> /// <param name="logger"></param> public RebusHubLifetimeManager(IBus bus, IHubProtocolResolver hubProtocolResolver, ILogger <RebusHubLifetimeManager <THub> > logger) { _bus = bus; _logger = logger; _protocols = hubProtocolResolver.AllProtocols; Connections = new HubConnectionStore(); GroupConnections = new ConcurrentDictionary <string, HubConnectionStore>(); UserConnections = new ConcurrentDictionary <string, HubConnectionStore>(); ServerName = $"{Environment.MachineName}-{Guid.NewGuid():N}"; }
private async Task AddGroup(HubConnectionContext connection, string group) { await _Lock.WaitAsync(); try { HubConnectionStore subscription = this._Groups.GetOrAdd(group, _ => new HubConnectionStore()); subscription.Add(connection); } finally { _Lock.Release(); } }
public async Task SubscribeToUser(HubConnectionContext connection) { await _UserLock.WaitAsync(); try { HubConnectionStore subscription = this._Users.GetOrAdd(connection.UserIdentifier, _ => new HubConnectionStore()); subscription.Add(connection); RabbitMQLog.Subscribing(_Logger, "SubscribeToUser:" + connection.ConnectionId); } finally { _UserLock.Release(); } }
/// <summary> /// Constructs the <see cref="Enumerator"/> over the <see cref="HubConnectionStore"/>. /// </summary> /// <param name="hubConnectionList">The store of connections to enumerate over.</param> public Enumerator(HubConnectionStore hubConnectionList) { _enumerator = hubConnectionList._connections.GetEnumerator(); }