예제 #1
0
        public NatsClient(ConnectionInfo connectionInfo, ISocketFactory socketFactory = null)
        {
            if (connectionInfo == null)
            {
                throw new ArgumentNullException(nameof(connectionInfo));
            }

            Id = UniqueId.Generate();

            _inboxAddress      = $"IB.{Id}";
            _sync              = new SemaphoreSlim(1, 1);
            _connectionInfo    = connectionInfo.Clone();
            _subscriptions     = new ConcurrentDictionary <string, Subscription>(StringComparer.OrdinalIgnoreCase);
            _eventMediator     = new NatsObservableOf <IClientEvent>();
            _opMediator        = new NatsOpMediator();
            _connectionManager = new NatsConnectionManager(socketFactory ?? new SocketFactory());

            Events.SubscribeSafe(async ev =>
            {
                switch (ev)
                {
                case ClientDisconnected disconnected
                    when disconnected.Reason == DisconnectReason.DueToFailure && _connectionInfo.AutoReconnectOnFailure:
                    await ReconnectAsync().ConfigureAwait(false);
                    break;
                }
            });
        }
예제 #2
0
        public NatsClient(ConnectionInfo connectionInfo, ISocketFactory socketFactory = null)
        {
            EnsureArg.IsNotNull(connectionInfo, nameof(connectionInfo));

            _sync              = new Locker();
            _connectionInfo    = connectionInfo.Clone();
            _subscriptions     = new ConcurrentDictionary <string, Subscription>();
            _eventMediator     = new NatsObservableOf <IClientEvent>();
            _opMediator        = new NatsOpMediator();
            _connectionManager = new NatsConnectionManager(socketFactory ?? new SocketFactory());

            Events.SubscribeSafe(ev =>
            {
                if (ev is ClientDisconnected disconnected)
                {
                    var shouldTryToReconnect = _connectionInfo.AutoReconnectOnFailure && disconnected.Reason == DisconnectReason.DueToFailure;
                    if (!shouldTryToReconnect)
                    {
                        return;
                    }

                    Reconnect();
                }
            });
        }
예제 #3
0
        public void Dispose()
        {
            ThrowIfDisposed();

            _isDisposed = true;

            DoSafeRelease();

            var exs = new List <Exception>();

            void TryDispose(IDisposable disposable)
            {
                if (disposable == null)
                {
                    return;
                }

                try
                {
                    disposable.Dispose();
                }
                catch (Exception ex)
                {
                    exs.Add(ex);
                }
            }

            TryDispose(_inboxSubscription);

            foreach (var s in _subscriptions.Values)
            {
                TryDispose(s);
            }

            TryDispose(_eventMediator);
            TryDispose(_opMediator);
            TryDispose(_sync);

            _subscriptions.Clear();
            _inboxSubscription = null;
            _eventMediator     = null;
            _opMediator        = null;
            _sync = null;

            if (exs.Any())
            {
                throw new AggregateException("Failed while disposing client. See inner exception(s) for more details.", exs);
            }
        }
예제 #4
0
        private void Dispose(bool disposing)
        {
            if (_isDisposed || !disposing)
            {
                return;
            }

            DoSafeRelease();

            Try.All(
                () =>
            {
                var subscriptions = _subscriptions.Values.Cast <IDisposable>().ToArray();
                _subscriptions.Clear();
                Try.DisposeAll(subscriptions);
            },
                () =>
            {
                Try.DisposeAll(_eventMediator, _opMediator, _sync);
                _eventMediator = null;
                _opMediator    = null;
                _sync          = null;
            });
        }