private void Fetch()
 {
     if (_isFetching.CompareExchange(true, false))
     {
         return;
     }
     Task.Run(async() =>
     {
         try
         {
             bool isEnd = false;
             while (_shouldFetch.CompareExchange(false, true) || !isEnd)
             {
                 isEnd = await DoFetch();
             }
         }
         catch (Exception ex)
         {
             // Drop subscription
         }
         finally
         {
             _isFetching.Set(false);
         }
     }, IsDisposed);
 }
Exemplo n.º 2
0
 private void Push()
 {
     if (_isPushing.CompareExchange(true, false))
     {
         return;
     }
     Task.Run(async() =>
     {
         ICommit commit;
         while (_commits.TryDequeue(out commit))
         {
             try
             {
                 await _onCommit(commit);
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.ToString());
             }
             if (_commits.Count < _threshold)
             {
                 _onThreashold();
             }
         }
         _isPushing.Set(false);
     });
 }
 public InMemoryStreamStore(GetUtcNow getUtcNow = null, string logName = null)
     : base(TimeSpan.FromMinutes(1), 10000, getUtcNow, logName ?? nameof(InMemoryStreamStore))
 {
     _getUtcNow = getUtcNow ?? SystemClock.GetUtcNow;
     _allStream.AddFirst(new InMemoryStreamMessage(
                             "<in-memory-root-message>",
                             Guid.NewGuid(),
                             -1,
                             -1,
                             _getUtcNow(),
                             null,
                             null,
                             null));
     _onStreamAppended = () =>
     {
         if (_signallingToSubscribers.CompareExchange(true, false) == false)
         {
             Task.Run(() =>
             {
                 _subscriptions.OnNext(Unit.Default);
                 _signallingToSubscribers.Set(false);
             });
         }
     };
 }
Exemplo n.º 4
0
        public void set_value_true_was_true()
        {
            var interlockedBoolean = new InterlockedBoolean(true);
            var oldValue           = interlockedBoolean.Set(true);

            Assert.IsTrue(interlockedBoolean.Value);
            Assert.IsTrue(oldValue);
        }
        public void set_value_true_was_true()
        {
            var interlockedBoolean = new InterlockedBoolean(true);
            var oldValue = interlockedBoolean.Set(true);

            Assert.IsTrue(interlockedBoolean.Value);
            Assert.IsTrue(oldValue);
        }
Exemplo n.º 6
0
        public void Dispose()
        {
            _disposed.Set(true);

#if ENABLE_CHANNELPOOLING
            Flush();
#endif
        }
Exemplo n.º 7
0
        public void Disconnect()
        {
            if (_isDisconnected.Set(true))
            {
                return;
            }

            Disconnected();
        }
 public virtual Task Start(CancellationToken cancellationToken)
 {
     _streamStoreAppendedSubscription = StreamStoreAppendedNotification.Subscribe(_ =>
     {
         _shouldFetch.Set(true);
         Fetch();
     });
     Fetch();
     return(Task.FromResult(0));
 }
Exemplo n.º 9
0
        public void Disconnect()
        {
            if (_isDisconnected.Set(true))
            {
                return;
            }

            TcpClient.Close();
            Disconnected();
        }
Exemplo n.º 10
0
        public void Disconnect()
        {
            if (_isDisconnected.Set(true))
            {
                return;
            }

            _stream.Close();

            Disconnected();
        }
Exemplo n.º 11
0
        public void benchmark_set_value()
        {
            var interlockedBoolean = new InterlockedBoolean();

            const int iterations = 100000000;
            var       sw         = Stopwatch.StartNew();

            for (var i = 0; i < iterations; i++)
            {
                interlockedBoolean.Set(true);
            }
            sw.Stop();

            Assert.Inconclusive("{0} ({1:N0} ops/sec)", sw.Elapsed, iterations / sw.Elapsed.TotalMilliseconds * 1000);
        }
Exemplo n.º 12
0
        private void UpdateTokens()
        {
            if (_updatingTokens.EnsureCalledOnce())
            {
                return;
            }
            var currentTime = _getUtcNow().Ticks;

            if (currentTime >= _nextRefillTime)
            {
                Interlocked.Exchange(ref _tokens, _getBucketTokenCapacty());
                _nextRefillTime = currentTime + _refillIntervalTicks;
            }

            _updatingTokens.Set(false);
        }
Exemplo n.º 13
0
 protected void Fetch()
 {
     if (_isFetching.CompareExchange(true, false))
     {
         return;
     }
     Task.Run(async() =>
     {
         bool isEnd = false;
         while (!isEnd || _shouldFetch.CompareExchange(false, true))
         {
             isEnd = await DoFetch();
         }
         _isFetching.Set(false);
     }, IsDisposed);
 }
Exemplo n.º 14
0
 private void Push()
 {
     if (_isPushing.CompareExchange(true, false))
     {
         return;
     }
     Task.Run(async() =>
     {
         ResolvedEvent resolvedEvent;
         while (!_token.IsCancellationRequested && _events.TryDequeue(out resolvedEvent))
         {
             try
             {
                 await _onResolvedEvent(resolvedEvent, _token);
             }
             catch (Exception ex)
             {
                 s_logger.ErrorException(ex.Message, ex);
             }
         }
         _isPushing.Set(false);
     }, _token);
 }
Exemplo n.º 15
0
        public KeyValueWatchSubscription(KeyValue kv, string keyPattern,
                                         IKeyValueWatcher watcher, params KeyValueWatchOption[] watchOptions)
        {
            string subject = kv.RawKeySubject(keyPattern);

            // figure out the result options
            bool          headersOnly    = false;
            bool          includeDeletes = true;
            DeliverPolicy deliverPolicy  = DeliverPolicy.LastPerSubject;

            foreach (KeyValueWatchOption wo in watchOptions)
            {
                switch (wo)
                {
                case KeyValueWatchOption.MetaOnly: headersOnly = true; break;

                case KeyValueWatchOption.IgnoreDelete: includeDeletes = false; break;

                case KeyValueWatchOption.UpdatesOnly: deliverPolicy = DeliverPolicy.New; break;

                case KeyValueWatchOption.IncludeHistory: deliverPolicy = DeliverPolicy.All; break;
                }
            }

            if (deliverPolicy == DeliverPolicy.New)
            {
                endOfDataSent = new InterlockedBoolean(true);
                watcher.EndOfData();
            }
            else
            {
                KeyValueEntry kveCheckPending = kv._kvGetLastMessage(keyPattern);
                if (kveCheckPending == null)
                {
                    endOfDataSent = new InterlockedBoolean(true);
                    watcher.EndOfData();
                }
                else
                {
                    endOfDataSent = new InterlockedBoolean(false);
                }
            }

            PushSubscribeOptions pso = PushSubscribeOptions.Builder()
                                       .WithStream(kv.StreamName)
                                       .WithOrdered(true)
                                       .WithConfiguration(
                ConsumerConfiguration.Builder()
                .WithAckPolicy(AckPolicy.None)
                .WithDeliverPolicy(deliverPolicy)
                .WithHeadersOnly(headersOnly)
                .WithFilterSubject(subject)
                .Build())
                                       .Build();

            EventHandler <MsgHandlerEventArgs> handler = (sender, args) =>
            {
                KeyValueEntry kve = new KeyValueEntry(args.msg);
                if (includeDeletes || kve.Operation.Equals(KeyValueOperation.Put))
                {
                    watcher.Watch(kve);
                }

                if (endOfDataSent.IsFalse() && kve.Delta == 0)
                {
                    endOfDataSent.Set(true);
                    watcher.EndOfData();
                }
            };

            sub = kv.js.PushSubscribeAsync(subject, handler, false, pso);
            if (endOfDataSent.IsFalse())
            {
                ulong pending = sub.GetConsumerInformation().CalculatedPending;
                if (pending == 0)
                {
                    endOfDataSent.Set(true);
                    watcher.EndOfData();
                }
            }
        }
Exemplo n.º 16
0
        public void benchmark_set_value()
        {
            var interlockedBoolean = new InterlockedBoolean();

            const int iterations = 10000000;
            var sw = Stopwatch.StartNew();
            for (var i = 0; i < iterations; i++)
            {
                interlockedBoolean.Set(true);
            }
            sw.Stop();

            Assert.Inconclusive("{0} ({1:N0} ops/sec)", sw.Elapsed, (decimal)iterations / sw.ElapsedMilliseconds * 1000m);
        }
Exemplo n.º 17
0
        public void Restart()
        {
            hasStarted.Set(true);

            //CLose connections and channels
            if (subscriberChannel != null)
            {
                if (subscriberConsumer != null)
                {
                    try
                    {
                        subscriberChannel.Channel.BasicCancel(subscriberConsumer.ConsumerTag);
                    }
                    catch { }
                }

                try
                {
                    subscriberChannel.Close();
                }
                catch
                {
                }
            }

            if (workChannel != null)
            {
                if (workConsumer != null)
                {
                    try
                    {
                        workChannel.Channel.BasicCancel(workConsumer.ConsumerTag);
                    }
                    catch { }
                }

                try
                {
                    workChannel.Close();
                }
                catch
                {
                }
            }

            if (_subscriberPool != null)
            {
                _subscriberPool.Dispose();
            }

            //NOTE: CreateConnection() can throw BrokerUnreachableException
            //That's okay because the exception needs to propagate to Reconnect() or Start()
            var conn = connectionFactory.CreateConnection();

            if (connectionBroken != null)
            {
                connectionBroken.Dispose();
            }
            connectionBroken = new CancellationTokenSource();

            if (stopWaitingOnQueue != null)
            {
                stopWaitingOnQueue.Dispose();
            }
            stopWaitingOnQueue = CancellationTokenSource.CreateLinkedTokenSource(disposedCancellationSource.Token, connectionBroken.Token);

            var pool = new AmqpChannelPooler(conn);

            _subscriberPool = pool;

            //Use pool reference henceforth.

            //Create work channel and declare exchanges and queues
            workChannel = pool.GetModel(ChannelFlags.Consumer);

            //Redeclare exchanges and queues
            AmqpUtils.DeclareExchangeAndQueues(workChannel.Channel, messageMapper, messagingConfig, serviceName, exchangeDeclareSync, Id);

            //Listen on work queue
            workConsumer = new ConcurrentQueueingConsumer(workChannel.Channel, requestQueued);
            string workQueueName = AmqpUtils.GetWorkQueueName(messagingConfig, serviceName);

            workChannel.Channel.BasicQos(0, (ushort)Settings.PrefetchCount, false);
            workChannel.Channel.BasicConsume(workQueueName, Settings.AckBehavior == SubscriberAckBehavior.Automatic, workConsumer);

            //Listen on subscriber queue
            subscriberChannel  = pool.GetModel(ChannelFlags.Consumer);
            subscriberConsumer = new ConcurrentQueueingConsumer(subscriberChannel.Channel, requestQueued);
            string subscriberWorkQueueName = AmqpUtils.GetSubscriberQueueName(serviceName, Id);

            subscriberChannel.Channel.BasicQos(0, (ushort)Settings.PrefetchCount, false);
            subscriberChannel.Channel.BasicConsume(subscriberWorkQueueName, Settings.AckBehavior == SubscriberAckBehavior.Automatic, subscriberConsumer);

            //Cancel connectionBroken on connection/consumer problems
            pool.Connection.ConnectionShutdown   += (s, e) => { connectionBroken.Cancel(); };
            workConsumer.ConsumerCancelled       += (s, e) => { connectionBroken.Cancel(); };
            subscriberConsumer.ConsumerCancelled += (s, e) => { connectionBroken.Cancel(); };
        }
Exemplo n.º 18
0
        /// <summary>
        /// Starts the Listener
        /// </summary>
        public void Start()
        {
            if (disposed.Value)
            {
                throw new ObjectDisposedException(ToString());
            }

            if (isStarted.EnsureCalledOnce())
            {
                return;
            }

            tcpListener = new TcpListener(IPAddress.Any, port);

            tcpListener.Start();

            log.LogTrace("Listener Started on Port {0}".Fmt(Port));

            subscription = Observable.FromAsync(tcpListener.AcceptTcpClientAsync)
                           .Repeat()
                           .TakeUntil(listenerTermination)
                           .Do(connection => log.LogTrace("New Connection from {0}".Fmt(connection.Client.RemoteEndPoint)))
                           .Select(
                tcpClient =>
            {
                try
                {
                    return(observableSocketFactory(tcpClient));
                }
                catch (Exception ex)
                {
                    //race condition - socket might shut down before we can initialize
                    log.LogError(ex, "Unable to create observableSocket");
                    return(null);
                }
            })
                           .Where(x => x != null)
                           .Subscribe(
                connection =>
            {
                if (connection != null)
                {
                    connections.Add(connection);
                    observable.OnNext(connection);

                    disposables.Add(
                        Observable.FromEventPattern(h => connection.Disposed += h, h => connection.Disposed -= h)
                        .FirstAsync()
                        .Subscribe(
                            _ =>
                    {
                        log.LogTrace("Connection Disposed");
                        connections.Remove(connection);
                    }));
                }
            },
                ex =>
            {
                //ObjectDisposedException is thrown by TcpListener when Stop() is called before EndAcceptTcpClient()
                if (!(ex is ObjectDisposedException))
                {
                    log.LogError(ex, "Error handling inbound connection");
                }
            },
                () => isStarted.Set(false));
        }