示例#1
0
        private void DoSafeRelease() => Swallow.Everything(
            () =>
        {
            if (_cancellation == null)
            {
                return;
            }

            if (!_cancellation.IsCancellationRequested)
            {
                _cancellation.Cancel(false);
            }

            _cancellation = null;
        },
            () =>
        {
            if (_consumer == null)
            {
                return;
            }

            if (!_consumer.IsCompleted)
            {
                _consumer.Wait(WaitForConsumerCompleteMs);
            }

            _consumer = null;
        },
            () =>
        {
            _connection?.Dispose();
            _connection = null;
        });
示例#2
0
 public NatsEventSubscriber(INatsConnection connection, IOptions <NatsOptions> options, IEventProcessor eventProcessor)
 {
     _connection     = connection;
     _eventProcessor = eventProcessor;
     _options        = options.Value;
     _eventProcessor.Setup();
 }
 public NatsClient(INatsConnection connection)
 {
     Connection = connection;
     _inbox     = Connection.OnConnect
                  .Select(_ => GetSubscription(_inboxPrefix + "*"))
                  .Switch()
                  .Publish()
                  .RefCount();
 }
示例#4
0
 public EventBusNats(INatsConnection connection, ILoggerFactory loggerFactory,
                     IEventBusSubscriptionsManager subsManager,
                     IServiceProvider serviceProvider,
                     ITracer tracer)
 {
     _logger          = loggerFactory.CreateLogger <EventBusNats>();
     _connection      = connection;
     _subsManager     = subsManager;
     _serviceProvider = serviceProvider;
     _tracer          = tracer;
 }
示例#5
0
        private void DoSafeRelease() => Swallow.Everything(
            () =>
        {
            if (_cancellation == null)
            {
                return;
            }

            if (!_cancellation.IsCancellationRequested)
            {
                _cancellation.Cancel(false);
            }

            _cancellation.Dispose();
            _cancellation = null;
        },
            () =>
        {
            if (_consumer == null)
            {
                return;
            }

            if (!_consumer.IsCompleted)
            {
                _consumer.Wait(WaitForConsumerCompleteMs);
            }

            _consumer = null;
        },
            () =>
        {
            foreach (var key in _outstandingRequests.Keys)
            {
                if (_outstandingRequests.TryRemove(key, out var tcs))
                {
                    try
                    {
                        tcs.TrySetCanceled();
                    }
                    catch
                    {
                        // ignored
                    }
                }
            }
        },
            () =>
        {
            _connection?.Dispose();
            _connection = null;
        });
示例#6
0
 public static TContract GenerateClient(INatsConnection connection, string baseSubject)
 {
     return((TContract)Activator.CreateInstance(_type, connection, baseSubject));
 }
示例#7
0
 public NatsEventPublisher(INatsConnection connection, IOptions <NatsOptions> options, ILogger <NatsEventPublisher> logger)
 {
     _connection = connection;
     _options    = options.Value;
     _logger     = logger;
 }
示例#8
0
        public void Connect()
        {
            ThrowIfDisposed();

            if (IsConnected)
            {
                return;
            }

            using (_sync.Lock())
            {
                if (IsConnected)
                {
                    return;
                }

                DoSafeRelease();

                _cancellation = new CancellationTokenSource();

                var connectionResult = _connectionManager.OpenConnection(
                    _connectionInfo,
                    _cancellation.Token);

                _connection = connectionResult.Item1;

                var opsReceivedWhileConnecting = connectionResult.Item2;

                _eventMediator.Emit(new ClientConnected(this));

                foreach (var op in opsReceivedWhileConnecting)
                {
                    _opMediator.Emit(op);
                }

                foreach (var subscription in _subscriptions.Values)
                {
                    if (!IsConnected)
                    {
                        break;
                    }

                    DoSub(subscription.SubscriptionInfo);
                }

                _consumer = Task.Factory
                            .StartNew(
                    Worker,
                    _cancellation.Token,
                    TaskCreationOptions.LongRunning,
                    TaskScheduler.Default)
                            .ContinueWith(t =>
                {
                    if (!t.IsFaulted)
                    {
                        return;
                    }

                    DoSafeRelease();

                    _eventMediator.Emit(new ClientDisconnected(this, DisconnectReason.DueToFailure));

                    var ex = t.Exception?.GetBaseException() ?? t.Exception;
                    if (ex == null)
                    {
                        return;
                    }

                    _logger.Error("Internal client worker exception.", ex);

                    _eventMediator.Emit(new ClientWorkerFailed(this, ex));
                });
            }
        }
示例#9
0
 public static async Task Write(this INatsConnection @this, string data, CancellationToken cancellationToken)
 {
     await @this.Write(Encoding.UTF8.GetBytes(data + NewLine), cancellationToken);
 }
示例#10
0
 protected NatsClientProxy(INatsConnection connection, string baseSubject)
 {
     _connection  = connection;
     _baseSubject = baseSubject;
 }