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; });
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(); }
public EventBusNats(INatsConnection connection, ILoggerFactory loggerFactory, IEventBusSubscriptionsManager subsManager, IServiceProvider serviceProvider, ITracer tracer) { _logger = loggerFactory.CreateLogger <EventBusNats>(); _connection = connection; _subsManager = subsManager; _serviceProvider = serviceProvider; _tracer = tracer; }
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; });
public static TContract GenerateClient(INatsConnection connection, string baseSubject) { return((TContract)Activator.CreateInstance(_type, connection, baseSubject)); }
public NatsEventPublisher(INatsConnection connection, IOptions <NatsOptions> options, ILogger <NatsEventPublisher> logger) { _connection = connection; _options = options.Value; _logger = logger; }
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)); }); } }
public static async Task Write(this INatsConnection @this, string data, CancellationToken cancellationToken) { await @this.Write(Encoding.UTF8.GetBytes(data + NewLine), cancellationToken); }
protected NatsClientProxy(INatsConnection connection, string baseSubject) { _connection = connection; _baseSubject = baseSubject; }