internal RedisMessageBus(IStringMinifier stringMinifier,
                                     ILoggerFactory loggerFactory,
                                     IPerformanceCounterManager performanceCounterManager,
                                     IOptions<MessageBusOptions> optionsAccessor,
                                     IOptions<RedisScaleoutOptions> scaleoutConfigurationAccessor,
                                     IRedisConnection connection,
                                     bool connectAutomatically)
            : base(stringMinifier, loggerFactory, performanceCounterManager, optionsAccessor, scaleoutConfigurationAccessor)
        {
            _connectionString = scaleoutConfigurationAccessor.Options.ConnectionString;
            _db = scaleoutConfigurationAccessor.Options.Database;
            _key = scaleoutConfigurationAccessor.Options.EventKey;

            _connection = connection;

            _logger = loggerFactory.CreateLogger<RedisMessageBus>();

            ReconnectDelay = TimeSpan.FromSeconds(2);

            if (connectAutomatically)
            {
                ThreadPool.QueueUserWorkItem(_ =>
                {
                    var ignore = ConnectWithRetry();
                });
            }
        }
 public RedisMessageBus(IStringMinifier stringMinifier,
                              ILoggerFactory loggerFactory,
                              IPerformanceCounterManager performanceCounterManager,
                              IOptions<MessageBusOptions> optionsAccessor,
                              IOptions<RedisScaleoutOptions> scaleoutConfigurationAccessor, IRedisConnection connection)
     : this(stringMinifier, loggerFactory, performanceCounterManager, optionsAccessor, scaleoutConfigurationAccessor, connection, true)
 {
 }
 /// <summary>
 /// Creates a new instance of the SqlMessageBus class.
 /// </summary>
 /// <param name="resolver">The resolver to use.</param>
 /// <param name="configuration">The SQL scale-out configuration options.</param>
 public SqlMessageBus(IStringMinifier stringMinifier,
                              ILoggerFactory loggerFactory,
                              IPerformanceCounterManager performanceCounterManager,
                              IOptions<MessageBusOptions> optionsAccessor,
                              IOptions<SqlScaleoutOptions> scaleoutOptionsAccessor)
     : this(stringMinifier, loggerFactory, performanceCounterManager, optionsAccessor, scaleoutOptionsAccessor, SqlClientFactory.Instance.AsIDbProviderFactory())
 {
 }
Example #4
0
        public MessageBus(IStringMinifier stringMinifier,
                          ITraceManager traceManager,
                          IPerformanceCounterManager performanceCounterManager,
                          IConfigurationManager configurationManager,
                          int maxTopicsWithNoSubscriptions)
        {
            if (stringMinifier == null)
            {
                throw new ArgumentNullException("stringMinifier");
            }

            if (traceManager == null)
            {
                throw new ArgumentNullException("traceManager");
            }

            if (performanceCounterManager == null)
            {
                throw new ArgumentNullException("performanceCounterManager");
            }

            if (configurationManager == null)
            {
                throw new ArgumentNullException("configurationManager");
            }

            if (configurationManager.DefaultMessageBufferSize < 0)
            {
                throw new ArgumentOutOfRangeException(Resources.Error_BufferSizeOutOfRange);
            }

            _stringMinifier = stringMinifier;
            _traceManager = traceManager;
            Counters = performanceCounterManager;
            _trace = _traceManager["SignalR.MessageBus"];
            _maxTopicsWithNoSubscriptions = maxTopicsWithNoSubscriptions;

            _gcTimer = new Timer(_ => GarbageCollectTopics(), state: null, dueTime: _gcInterval, period: _gcInterval);

            _broker = new MessageBroker(Counters)
            {
                Trace = Trace
            };

            // The default message store size
            _messageStoreSize = (uint)configurationManager.DefaultMessageBufferSize;

            // Calculate keepAlive duration in context of ticks. This is necessary because keepAlive indicates how
            // many heartbeat intervals to wait before sending a keep alive.
            var keepAlive = configurationManager.KeepAlive * configurationManager.HeartbeatInterval.Ticks;

            // Keep topics alive for twice as long as we let connections to reconnect.
            // Also add twice the keepalive interval since clients might take a while to notice they are disconnected.
            // This should be a good enough estimate for how long until we should consider a topic dead.
            _topicTtl = TimeSpan.FromTicks((configurationManager.DisconnectTimeout.Ticks + keepAlive) * 2);

            Topics = new ConcurrentDictionary<string, Topic>();
        }
Example #5
0
        public MessageBus(IStringMinifier stringMinifier,
                          ILoggerFactory loggerFactory,
                          IPerformanceCounterManager performanceCounterManager,
                          IOptions<MessageBusOptions> optionsAccessor)
        {
            if (stringMinifier == null)
            {
                throw new ArgumentNullException("stringMinifier");
            }

            if (loggerFactory == null)
            {
                throw new ArgumentNullException("traceManager");
            }

            if (performanceCounterManager == null)
            {
                throw new ArgumentNullException("performanceCounterManager");
            }

            if (optionsAccessor == null)
            {
                throw new ArgumentNullException("optionsAccessor");
            }

            var options = optionsAccessor.Options;

            if (options.MessageBufferSize < 0)
            {
                throw new ArgumentOutOfRangeException(Resources.Error_BufferSizeOutOfRange);
            }

            _stringMinifier = stringMinifier;
            _loggerFactory = loggerFactory;
            Counters = performanceCounterManager;
            _logger = _loggerFactory.CreateLogger<MessageBus>();
            _maxTopicsWithNoSubscriptions = options.MaxTopicsWithNoSubscriptions;

            _gcTimer = new Timer(_ => GarbageCollectTopics(), state: null, dueTime: _gcInterval, period: _gcInterval);

            _broker = new MessageBroker(Counters)
            {
                Logger = _logger
            };

            // The default message store size
            _messageStoreSize = (uint)options.MessageBufferSize;

            _topicTtl = options.TopicTTL;
            _createTopic = CreateTopic;
            _addEvent = AddEvent;
            _removeEvent = RemoveEvent;
            _disposeSubscription = o => DisposeSubscription(o);

            Topics = new TopicLookup();
        }
Example #6
0
        public MessageBus(IStringMinifier stringMinifier,
                          ITraceManager traceManager,
                          IPerformanceCounterManager performanceCounterManager,
                          IConfigurationManager configurationManager,
                          int maxTopicsWithNoSubscriptions)
        {
            if (stringMinifier == null)
            {
                throw new ArgumentNullException("stringMinifier");
            }

            if (traceManager == null)
            {
                throw new ArgumentNullException("traceManager");
            }

            if (performanceCounterManager == null)
            {
                throw new ArgumentNullException("performanceCounterManager");
            }

            if (configurationManager == null)
            {
                throw new ArgumentNullException("configurationManager");
            }

            if (configurationManager.DefaultMessageBufferSize < 0)
            {
                throw new ArgumentOutOfRangeException(Resources.Error_BufferSizeOutOfRange);
            }

            _stringMinifier = stringMinifier;
            _traceManager = traceManager;
            Counters = performanceCounterManager;
            _trace = _traceManager["SignalR." + typeof(MessageBus).Name];
            _maxTopicsWithNoSubscriptions = maxTopicsWithNoSubscriptions;

            _gcTimer = new Timer(_ => GarbageCollectTopics(), state: null, dueTime: _gcInterval, period: _gcInterval);

            _broker = new MessageBroker(Counters)
            {
                Trace = _trace
            };

            // The default message store size
            _messageStoreSize = (uint)configurationManager.DefaultMessageBufferSize;

            _topicTtl = configurationManager.TopicTtl();
            _createTopic = CreateTopic;
            _addEvent = AddEvent;
            _removeEvent = RemoveEvent;
            _disposeSubscription = o => DisposeSubscription(o);

            Topics = new TopicLookup();
        }
 protected ScaleoutMessageBus(IStringMinifier stringMinifier,
                              ILoggerFactory loggerFactory,
                              IPerformanceCounterManager performanceCounterManager,
                              IOptions<MessageBusOptions> optionsAccessor,
                              IOptions<ScaleoutOptions> scaleoutOptionsAccessor)
     : base(stringMinifier, loggerFactory, performanceCounterManager, optionsAccessor)
 {
     _logger = loggerFactory.CreateLogger<ScaleoutMessageBus>();
     _perfCounters = performanceCounterManager;
     _streamManager = new Lazy<ScaleoutStreamManager>(() => new ScaleoutStreamManager(Send, OnReceivedCore, StreamCount, _logger, _perfCounters, scaleoutOptionsAccessor.Value));
 }
        public DefaultSubscription(string identity,
                                   IEnumerable <string> eventKeys,
                                   IDictionary <string, Topic> topics,
                                   string cursor,
                                   Func <MessageResult, Task <bool> > callback,
                                   int maxMessages,
                                   IStringMinifier stringMinifier,
                                   IPerformanceCounterManager counters) :
            base(identity, eventKeys, callback, maxMessages, counters)
        {
            _stringMinifier = stringMinifier;

            IEnumerable <Cursor> cursors;

            if (cursor == null)
            {
                cursors = GetCursorsFromEventKeys(EventKeys, topics);
            }
            else
            {
                cursors = Cursor.GetCursors(cursor, stringMinifier.Unminify) ?? GetCursorsFromEventKeys(EventKeys, topics);
            }

            _cursors      = new List <Cursor>(cursors);
            _cursorTopics = new List <Topic>();

            if (!String.IsNullOrEmpty(cursor))
            {
                // Update all of the cursors so we're within the range
                for (int i = _cursors.Count - 1; i >= 0; i--)
                {
                    Cursor c = _cursors[i];
                    Topic  topic;
                    if (!EventKeys.Contains(c.Key))
                    {
                        _cursors.Remove(c);
                    }
                    else if (!topics.TryGetValue(_cursors[i].Key, out topic) || _cursors[i].Id > topic.Store.GetMessageCount())
                    {
                        UpdateCursor(c.Key, 0);
                    }
                }
            }

            // Add dummy entries so they can be filled in
            for (int i = 0; i < _cursors.Count; i++)
            {
                _cursorTopics.Add(null);
            }
        }
        public DefaultSubscription(string identity,
                                   IList<string> eventKeys,
                                   TopicLookup topics,
                                   string cursor,
                                   Func<MessageResult, object, Task<bool>> callback,
                                   int maxMessages,
                                   IStringMinifier stringMinifier,
                                   IPerformanceCounterManager counters,
                                   object state) :
            base(identity, eventKeys, callback, maxMessages, counters, state)
        {
            _stringMinifier = stringMinifier;

            if (String.IsNullOrEmpty(cursor))
            {
                _cursors = GetCursorsFromEventKeys(EventKeys, topics);
            }
            else
            {
                // Ensure delegate continues to use the C# Compiler static delegate caching optimization.
                _cursors = Cursor.GetCursors(cursor, _defaultCursorPrefix, (k, s) => UnminifyCursor(k, s), stringMinifier) ?? GetCursorsFromEventKeys(EventKeys, topics);
            }

            _cursorTopics = new List<Topic>();

            if (!String.IsNullOrEmpty(cursor))
            {
                // Update all of the cursors so we're within the range
                for (int i = _cursors.Count - 1; i >= 0; i--)
                {
                    Cursor c = _cursors[i];
                    Topic topic;
                    if (!EventKeys.Contains(c.Key))
                    {
                        _cursors.Remove(c);
                    }
                    else if (!topics.TryGetValue(_cursors[i].Key, out topic) || _cursors[i].Id > topic.Store.GetMessageCount())
                    {
                        UpdateCursor(c.Key, 0);
                    }
                }
            }

            // Add dummy entries so they can be filled in
            for (int i = 0; i < _cursors.Count; i++)
            {
                _cursorTopics.Add(null);
            }
        }
Example #10
0
        public DefaultSubscription(string identity,
                                   IList <string> eventKeys,
                                   TopicLookup topics,
                                   string cursor,
                                   Func <MessageResult, object, Task <bool> > callback,
                                   int maxMessages,
                                   IStringMinifier stringMinifier,
                                   IPerformanceCounterManager counters,
                                   object state) :
            base(identity, eventKeys, callback, maxMessages, counters, state)
        {
            _stringMinifier = stringMinifier;

            if (String.IsNullOrEmpty(cursor))
            {
                _cursors = GetCursorsFromEventKeys(EventKeys, topics);
            }
            else
            {
                // Ensure delegate continues to use the C# Compiler static delegate caching optimization.
                _cursors = Cursor.GetCursors(cursor, _defaultCursorPrefix, (k, s) => UnminifyCursor(k, s), stringMinifier) ?? GetCursorsFromEventKeys(EventKeys, topics);
            }

            _cursorTopics = new List <Topic>();

            if (!String.IsNullOrEmpty(cursor))
            {
                // Update all of the cursors so we're within the range
                for (int i = _cursors.Count - 1; i >= 0; i--)
                {
                    Cursor c = _cursors[i];
                    Topic  topic;
                    if (!EventKeys.Contains(c.Key))
                    {
                        _cursors.Remove(c);
                    }
                    else if (!topics.TryGetValue(_cursors[i].Key, out topic) || _cursors[i].Id > topic.Store.GetMessageCount())
                    {
                        UpdateCursor(c.Key, 0);
                    }
                }
            }

            // Add dummy entries so they can be filled in
            for (int i = 0; i < _cursors.Count; i++)
            {
                _cursorTopics.Add(null);
            }
        }
Example #11
0
        public DefaultSubscription(string identity,
                                   IEnumerable<string> eventKeys,
                                   IDictionary<string, Topic> topics,
                                   string cursor,
                                   Func<MessageResult, Task<bool>> callback,
                                   int maxMessages,
                                   IStringMinifier stringMinifier,
                                   IPerformanceCounterManager counters)
            : base(identity, eventKeys, callback, maxMessages, counters)
        {
            _stringMinifier = stringMinifier;

            IEnumerable<Cursor> cursors;
            if (cursor == null)
            {
                cursors = GetCursorsFromEventKeys(EventKeys, topics);
            }
            else
            {
                cursors = Cursor.GetCursors(cursor, stringMinifier.Unminify) ?? GetCursorsFromEventKeys(EventKeys, topics);
            }

            _cursors = new List<Cursor>(cursors);
            _cursorTopics = new List<Topic>();

            if (!String.IsNullOrEmpty(cursor))
            {
                // Update all of the cursors so we're within the range
                for (int i = _cursors.Count - 1; i >= 0; i--)
                {
                    Cursor c = _cursors[i];
                    Topic topic;
                    if (!EventKeys.Contains(c.Key))
                    {
                        _cursors.Remove(c);
                    }
                    else if (!topics.TryGetValue(_cursors[i].Key, out topic) || _cursors[i].Id > topic.Store.GetMessageCount())
                    {
                        UpdateCursor(c.Key, 0);
                    }
                }
            }

            // Add dummy entries so they can be filled in
            for (int i = 0; i < _cursors.Count; i++)
            {
                _cursorTopics.Add(null);
            }
        }
        internal SqlMessageBus(IStringMinifier stringMinifier,
                                     ILoggerFactory loggerFactory,
                                     IPerformanceCounterManager performanceCounterManager,
                                     IOptions<MessageBusOptions> optionsAccessor,
                                     IOptions<SqlScaleoutOptions> scaleoutOptionsAccessor,
                                     IDbProviderFactory dbProviderFactory)
            : base(stringMinifier, loggerFactory, performanceCounterManager, optionsAccessor, scaleoutOptionsAccessor)
        {
            var configuration = scaleoutOptionsAccessor.Options;
            _connectionString = configuration.ConnectionString;
            _configuration = configuration;
            _dbProviderFactory = dbProviderFactory;

            _logger = loggerFactory.CreateLogger<SqlMessageBus>();
            ThreadPool.QueueUserWorkItem(Initialize);
        }
Example #13
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="traceManager"></param>
        /// <param name="performanceCounterManager"></param>
        public MessageBus(IStringMinifier stringMinifier, ITraceManager traceManager, IPerformanceCounterManager performanceCounterManager, IConfigurationManager configurationManager)
        {
            _stringMinifier = stringMinifier;
            _trace = traceManager;
            _counters = performanceCounterManager;

            _gcTimer = new Timer(_ => CheckTopics(), state: null, dueTime: _gcInterval, period: _gcInterval);

            _broker = new MessageBroker(_counters)
            {
                Trace = Trace
            };

            // Keep topics alive for as long as we let connections wait until they are disconnected.
            // This should be a good enough estimate for how long until we should consider a topic dead.
            _topicTtl = configurationManager.DisconnectTimeout;
        }
Example #14
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="traceManager"></param>
        /// <param name="performanceCounterManager"></param>
        public MessageBus(IStringMinifier stringMinifier, ITraceManager traceManager, IPerformanceCounterManager performanceCounterManager, IConfigurationManager configurationManager)
        {
            _stringMinifier = stringMinifier;
            _trace          = traceManager;
            _counters       = performanceCounterManager;

            _gcTimer = new Timer(_ => CheckTopics(), state: null, dueTime: _gcInterval, period: _gcInterval);

            _broker = new MessageBroker(_counters)
            {
                Trace = Trace
            };

            // Keep topics alive for as long as we let connections wait until they are disconnected.
            // This should be a good enough estimate for how long until we should consider a topic dead.
            _topicTtl = configurationManager.DisconnectTimeout;
        }
        internal SqlMessageBus(IStringMinifier stringMinifier,
                               ILoggerFactory loggerFactory,
                               IPerformanceCounterManager performanceCounterManager,
                               IOptions <MessageBusOptions> optionsAccessor,
                               IOptions <SqlScaleoutOptions> scaleoutOptionsAccessor,
                               IDbProviderFactory dbProviderFactory)
            : base(stringMinifier, loggerFactory, performanceCounterManager, optionsAccessor, scaleoutOptionsAccessor)
        {
            var configuration = scaleoutOptionsAccessor.Value;

            _connectionString  = configuration.ConnectionString;
            _configuration     = configuration;
            _dbProviderFactory = dbProviderFactory;

            _logger = loggerFactory.CreateLogger <SqlMessageBus>();
            ThreadPool.QueueUserWorkItem(Initialize);
        }
Example #16
0
        public MessageBus(IStringMinifier stringMinifier,
                          ITraceManager traceManager,
                          IPerformanceCounterManager performanceCounterManager,
                          IConfigurationManager configurationManager)
        {
            if (stringMinifier == null)
            {
                throw new ArgumentNullException("stringMinifier");
            }

            if (traceManager == null)
            {
                throw new ArgumentNullException("traceManager");
            }

            if (performanceCounterManager == null)
            {
                throw new ArgumentNullException("performanceCounterManager");
            }

            if (configurationManager == null)
            {
                throw new ArgumentNullException("configurationManager");
            }

            _stringMinifier = stringMinifier;
            _traceManager   = traceManager;
            Counters        = performanceCounterManager;
            _trace          = _traceManager["SignalR.MessageBus"];

            _gcTimer = new Timer(_ => CheckTopics(), state: null, dueTime: _gcInterval, period: _gcInterval);

            _broker = new MessageBroker(Counters)
            {
                Trace = Trace
            };

            // Keep topics alive for twice as long as we let connections to reconnect.
            // Also add twice the keepalive interval since clients might take a while to notice they are disconnected.
            // This should be a good enough estimate for how long until we should consider a topic dead.
            var keepAlive = configurationManager.KeepAlive ?? TimeSpan.Zero;

            _topicTtl = TimeSpan.FromTicks((configurationManager.DisconnectTimeout.Ticks + keepAlive.Ticks) * 2);

            Topics = new ConcurrentDictionary <string, Topic>();
        }
Example #17
0
        public MessageBus(IStringMinifier stringMinifier,
                          ITraceManager traceManager,
                          IPerformanceCounterManager performanceCounterManager,
                          IConfigurationManager configurationManager)
        {
            if (stringMinifier == null)
            {
                throw new ArgumentNullException("stringMinifier");
            }

            if (traceManager == null)
            {
                throw new ArgumentNullException("traceManager");
            }

            if (performanceCounterManager == null)
            {
                throw new ArgumentNullException("performanceCounterManager");
            }

            if (configurationManager == null)
            {
                throw new ArgumentNullException("configurationManager");
            }

            _stringMinifier = stringMinifier;
            _traceManager = traceManager;
            Counters = performanceCounterManager;
            _trace = _traceManager["SignalR.MessageBus"];

            _gcTimer = new Timer(_ => CheckTopics(), state: null, dueTime: _gcInterval, period: _gcInterval);

            _broker = new MessageBroker(Counters)
            {
                Trace = Trace
            };

            // Keep topics alive for twice as long as we let connections to reconnect.
            // Also add twice the keepalive interval since clients might take a while to notice they are disconnected.
            // This should be a good enough estimate for how long until we should consider a topic dead.
            var keepAlive = configurationManager.KeepAlive ?? TimeSpan.Zero;
            _topicTtl = TimeSpan.FromTicks((configurationManager.DisconnectTimeout.Ticks + keepAlive.Ticks) * 2);

            Topics = new ConcurrentDictionary<string, Topic>();
        }
Example #18
0
        public MessageBus(IStringMinifier stringMinifier,
                          ITraceManager traceManager,
                          IPerformanceCounterManager performanceCounterManager,
                          IConfigurationManager configurationManager)
        {
            if (stringMinifier == null)
            {
                throw new ArgumentNullException("stringMinifier");
            }

            if (traceManager == null)
            {
                throw new ArgumentNullException("traceManager");
            }

            if (performanceCounterManager == null)
            {
                throw new ArgumentNullException("performanceCounterManager");
            }

            if (configurationManager == null)
            {
                throw new ArgumentNullException("configurationManager");
            }

            _stringMinifier = stringMinifier;
            _traceManager = traceManager;
            Counters = performanceCounterManager;
            _trace = _traceManager["SignalR.MessageBus"];

            _gcTimer = new Timer(_ => CheckTopics(), state: null, dueTime: _gcInterval, period: _gcInterval);

            _broker = new MessageBroker(Counters)
            {
                Trace = Trace
            };

            // Keep topics alive for as long as we let connections wait until they are disconnected.
            // This should be a good enough estimate for how long until we should consider a topic dead.
            _topicTtl = configurationManager.DisconnectTimeout;

            Topics = new ConcurrentDictionary<string, Topic>();
        }
 public TestScaleoutBus(int streams,
                        IStringMinifier stringMinifier,
                        ILoggerFactory loggerFactory,
                        IPerformanceCounterManager performanceCounterManager,
                        IOptions<MessageBusOptions> optionsAccessor,
                        IOptions<ScaleoutOptions> scaleoutOptionsAccessor)
     : base(stringMinifier, loggerFactory, performanceCounterManager, optionsAccessor, scaleoutOptionsAccessor)
 {
     _streams = streams;
 }
            public TestMessageBus(IStringMinifier stringMinifier,
                                 ILoggerFactory loggerFactory,
                                 IPerformanceCounterManager performanceCounterManager,
                                 IOptions<MessageBusOptions> optionsAccessor) :
                base(stringMinifier, loggerFactory, performanceCounterManager, optionsAccessor)
            {

            }