Esempio n. 1
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();
                }
            });
        }
Esempio n. 2
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;
                }
            });
        }
Esempio n. 3
0
        public void Dispose()
        {
            if (_isDisposed)
            {
                return;
            }

            _isDisposed = true;

            Exception ex1 = null, ex2 = null;

            try
            {
                _opStream.Dispose();
            }
            catch (Exception ex)
            {
                ex1 = ex;
            }

            try
            {
                _msgOpStream.Dispose();
            }
            catch (Exception ex)
            {
                ex2 = ex;
            }

            _opStream    = null;
            _msgOpStream = null;

            if (ex1 == null && ex2 == null)
            {
                return;
            }

            if (ex1 != null && ex2 != null)
            {
                throw new AggregateException(DisposeExMessage, ex1, ex2);
            }

            if (ex1 != null)
            {
                throw new AggregateException(DisposeExMessage, ex1);
            }

            if (ex2 != null)
            {
                throw new AggregateException(DisposeExMessage, ex2);
            }
        }
Esempio n. 4
0
        public void Dispose()
        {
            if (_isDisposed)
            {
                return;
            }

            _isDisposed = true;
            GC.SuppressFinalize(this);

            Try.DisposeAll(_opStream, _msgOpStream);
            _opStream    = null;
            _msgOpStream = null;
        }
Esempio n. 5
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);
            }
        }
Esempio n. 6
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;
            });
        }
Esempio n. 7
0
 public NatsOpMediator()
 {
     _opStream    = new NatsObservableOf <IOp>();
     _msgOpStream = new NatsObservableOf <MsgOp>();
 }