Пример #1
0
            public StreamSubscription(IPublisher bus,
                                      string streamName,
                                      StreamRevision startRevision,
                                      bool resolveLinks,
                                      IPrincipal user,
                                      IReadIndex readIndex,
                                      CancellationToken cancellationToken)
            {
                if (bus == null)
                {
                    throw new ArgumentNullException(nameof(bus));
                }

                if (streamName == null)
                {
                    throw new ArgumentNullException(nameof(streamName));
                }

                if (readIndex == null)
                {
                    throw new ArgumentNullException(nameof(readIndex));
                }

                _bus                 = bus;
                _streamName          = streamName;
                _nextRevision        = startRevision;
                _resolveLinks        = resolveLinks;
                _user                = user;
                _readIndex           = readIndex;
                _disposedTokenSource = new CancellationTokenSource();
                _buffer              = new ConcurrentQueue <ResolvedEvent>();
                _tokenRegistration   = cancellationToken.Register(_disposedTokenSource.Dispose);
            }
            public AllSubscriptionFiltered(IPublisher bus,
                                           Position?startPosition,
                                           bool resolveLinks,
                                           IEventFilter eventFilter,
                                           ClaimsPrincipal user,
                                           bool requiresLeader,
                                           IReadIndex readIndex,
                                           uint?maxSearchWindow,
                                           uint checkpointIntervalMultiplier,
                                           Func <Position, Task> checkpointReached,
                                           CancellationToken cancellationToken)
            {
                if (bus == null)
                {
                    throw new ArgumentNullException(nameof(bus));
                }

                if (eventFilter == null)
                {
                    throw new ArgumentNullException(nameof(eventFilter));
                }

                if (readIndex == null)
                {
                    throw new ArgumentNullException(nameof(readIndex));
                }

                if (checkpointReached == null)
                {
                    throw new ArgumentNullException(nameof(checkpointReached));
                }

                if (checkpointIntervalMultiplier == 0)
                {
                    throw new ArgumentOutOfRangeException(nameof(checkpointIntervalMultiplier));
                }

                _subscriptionId  = Guid.NewGuid();
                _bus             = bus;
                _resolveLinks    = resolveLinks;
                _eventFilter     = eventFilter;
                _user            = user;
                _requiresLeader  = requiresLeader;
                _readIndex       = readIndex;
                _maxSearchWindow = maxSearchWindow;
                _checkpointIntervalMultiplier = checkpointIntervalMultiplier;
                _checkpointReached            = checkpointReached;
                _cancellationToken            = cancellationToken;
                _subscriptionStarted          = new TaskCompletionSource <bool>();
                _subscriptionStarted.SetResult(true);

                _inner = startPosition == Position.End
                                        ? (IStreamSubscription) new LiveStreamSubscription(_subscriptionId, _bus,
                                                                                           Position.FromInt64(_readIndex.LastIndexedPosition, _readIndex.LastIndexedPosition),
                                                                                           _resolveLinks, _eventFilter, _user, _requiresLeader, _maxSearchWindow,
                                                                                           _checkpointIntervalMultiplier, checkpointReached, _cancellationToken)
                                        : new CatchupAllSubscription(_subscriptionId, bus, startPosition ?? Position.Start, resolveLinks,
                                                                     _eventFilter, user, _requiresLeader, readIndex, _maxSearchWindow, _checkpointIntervalMultiplier, checkpointReached,
                                                                     cancellationToken);
            }
            public AllSubscriptionFiltered(IPublisher bus,
                                           Position position,
                                           bool resolveLinks,
                                           IEventFilter eventFilter,
                                           IPrincipal user,
                                           IReadIndex readIndex,
                                           CancellationToken cancellationToken)
            {
                if (bus == null)
                {
                    throw new ArgumentNullException(nameof(bus));
                }

                if (readIndex == null)
                {
                    throw new ArgumentNullException(nameof(readIndex));
                }

                if (eventFilter == null)
                {
                    throw new ArgumentNullException(nameof(eventFilter));
                }

                _bus                 = bus;
                _nextPosition        = position;
                _resolveLinks        = resolveLinks;
                _eventFilter         = eventFilter;
                _user                = user;
                _readIndex           = readIndex;
                _disposedTokenSource = new CancellationTokenSource();
                _buffer              = new ConcurrentQueue <ResolvedEvent>();
                _tokenRegistration   = cancellationToken.Register(_disposedTokenSource.Dispose);
            }
                public CatchupAllSubscription(Guid subscriptionId,
                                              IPublisher bus,
                                              Position position,
                                              bool resolveLinks,
                                              IEventFilter eventFilter,
                                              ClaimsPrincipal user,
                                              bool requiresLeader,
                                              IReadIndex readIndex,
                                              uint?maxWindowSize,
                                              uint checkpointIntervalMultiplier,
                                              Func <Position, Task> checkpointReached,
                                              CancellationToken cancellationToken)
                {
                    if (bus == null)
                    {
                        throw new ArgumentNullException(nameof(bus));
                    }

                    if (eventFilter == null)
                    {
                        throw new ArgumentNullException(nameof(eventFilter));
                    }

                    if (readIndex == null)
                    {
                        throw new ArgumentNullException(nameof(readIndex));
                    }

                    if (checkpointReached == null)
                    {
                        throw new ArgumentNullException(nameof(checkpointReached));
                    }

                    if (checkpointIntervalMultiplier == 0)
                    {
                        throw new ArgumentOutOfRangeException(nameof(checkpointIntervalMultiplier));
                    }

                    _subscriptionId = subscriptionId;
                    _bus            = bus;
                    _nextPosition   = position == Position.End
                                                ? Position.FromInt64(readIndex.LastIndexedPosition, readIndex.LastIndexedPosition)
                                                : position;
                    _startPosition  = position == Position.End ? Position.Start : position;
                    _resolveLinks   = resolveLinks;
                    _eventFilter    = eventFilter;
                    _user           = user;
                    _requiresLeader = requiresLeader;
                    _checkpointIntervalMultiplier = checkpointIntervalMultiplier;
                    _checkpointReached            = checkpointReached;
                    _maxWindowSize       = maxWindowSize ?? ReadBatchSize;
                    _disposedTokenSource = new CancellationTokenSource();
                    _buffer             = new ConcurrentQueueWrapper <(ResolvedEvent, Position?)>();
                    _tokenRegistration  = cancellationToken.Register(_disposedTokenSource.Dispose);
                    _currentPosition    = _startPosition;
                    _checkpointInterval = 0;
                    Log.Information("Catch-up subscription {subscriptionId} to $all:{eventFilter} running...",
                                    _subscriptionId, _eventFilter);
                }
                public CatchupStreamSubscription(Guid subscriptionId,
                                                 IPublisher bus,
                                                 string streamName,
                                                 StreamRevision startRevision,
                                                 bool resolveLinks,
                                                 ClaimsPrincipal user,
                                                 bool requiresLeader,
                                                 IReadIndex readIndex,
                                                 TaskCompletionSource <bool> subscriptionStarted,
                                                 CancellationToken cancellationToken)
                {
                    if (bus == null)
                    {
                        throw new ArgumentNullException(nameof(bus));
                    }

                    if (streamName == null)
                    {
                        throw new ArgumentNullException(nameof(streamName));
                    }

                    if (readIndex == null)
                    {
                        throw new ArgumentNullException(nameof(readIndex));
                    }

                    if (subscriptionStarted == null)
                    {
                        throw new ArgumentNullException(nameof(subscriptionStarted));
                    }

                    _subscriptionId = subscriptionId;
                    _bus            = bus;
                    _streamName     = streamName;
                    _nextRevision   = startRevision == StreamRevision.End
                                                ? StreamRevision.FromInt64(readIndex.GetStreamLastEventNumber(_streamName) + 1)
                                                : startRevision;
                    _startRevision  = startRevision == StreamRevision.End ? StreamRevision.Start : startRevision;
                    _resolveLinks   = resolveLinks;
                    _user           = user;
                    _requiresLeader = requiresLeader;
                    var subscriptionStarted1 = subscriptionStarted;

                    _disposedTokenSource = new CancellationTokenSource();
                    _buffer            = new ConcurrentQueue <ResolvedEvent>();
                    _tokenRegistration = cancellationToken.Register(_disposedTokenSource.Dispose);

                    if (!subscriptionStarted1.Task.IsCompleted)
                    {
                        subscriptionStarted1.SetResult(true);
                    }

                    Log.Information(
                        "Catch-up subscription {subscriptionId} to {streamName}@{streamRevision} running...",
                        _subscriptionId, streamName, _nextRevision);
                }
            public StreamSubscription(
                IPublisher bus,
                string streamName,
                StreamRevision?startRevision,
                bool resolveLinks,
                ClaimsPrincipal user,
                bool requiresLeader,
                IReadIndex readIndex,
                CancellationToken cancellationToken)
            {
                if (bus == null)
                {
                    throw new ArgumentNullException(nameof(bus));
                }

                if (streamName == null)
                {
                    throw new ArgumentNullException(nameof(streamName));
                }

                if (readIndex == null)
                {
                    throw new ArgumentNullException(nameof(readIndex));
                }

                _subscriptionId      = Guid.NewGuid();
                _bus                 = bus;
                _streamName          = streamName;
                _resolveLinks        = resolveLinks;
                _user                = user;
                _requiresLeader      = requiresLeader;
                _readIndex           = readIndex;
                _cancellationToken   = cancellationToken;
                _subscriptionStarted = new TaskCompletionSource <bool>();

                _startRevision = startRevision == StreamRevision.End
                                        ? StreamRevision.FromInt64(readIndex.GetStreamLastEventNumber(_streamName) + 1)
                                        : startRevision + 1 ?? StreamRevision.Start;

                _inner = startRevision == StreamRevision.End
                                        ? (IStreamEnumerator) new LiveStreamSubscription(_subscriptionId, _bus, _streamName,
                                                                                         StreamRevision.FromInt64(readIndex.GetStreamLastEventNumber(_streamName) + 1), _resolveLinks,
                                                                                         _user, _requiresLeader, _subscriptionStarted, _cancellationToken)
                                        : new CatchupStreamSubscription(_subscriptionId, bus, streamName,
                                                                        startRevision + 1 ?? StreamRevision.Start, resolveLinks, user, _requiresLeader, readIndex,
                                                                        _subscriptionStarted, cancellationToken);
            }
Пример #7
0
            public StreamSubscription(
                IPublisher bus,
                string streamName,
                StreamRevision?startRevision,
                bool resolveLinks,
                ClaimsPrincipal user,
                bool requiresLeader,
                IReadIndex readIndex,
                CancellationToken cancellationToken)
            {
                if (bus == null)
                {
                    throw new ArgumentNullException(nameof(bus));
                }

                if (streamName == null)
                {
                    throw new ArgumentNullException(nameof(streamName));
                }

                if (readIndex == null)
                {
                    throw new ArgumentNullException(nameof(readIndex));
                }

                _subscriptionId      = Guid.NewGuid();
                _bus                 = bus;
                _streamName          = streamName;
                _resolveLinks        = resolveLinks;
                _user                = user;
                _requiresLeader      = requiresLeader;
                _cancellationToken   = cancellationToken;
                _subscriptionStarted = new TaskCompletionSource <bool>();
                _channel             = Channel.CreateBounded <ResolvedEvent>(BoundedChannelOptions);
                _semaphore           = new SemaphoreSlim(1, 1);

                SubscriptionId = _subscriptionId.ToString();

                Subscribe(startRevision == StreamRevision.End
                                        ? StreamRevision.FromInt64(readIndex.GetStreamLastEventNumber(_streamName) + 1)
                                        : startRevision + 1 ?? StreamRevision.Start, startRevision != StreamRevision.End);
            }