/// <summary> /// Atomically tries to set the first value on the target field and returns /// false if its not null. /// </summary> /// <param name="field"></param> /// <param name="first"></param> /// <returns></returns> public static bool SetOnce(ref ISubscription field, ISubscription first) { for (;;) { ISubscription a = Volatile.Read(ref field); if (a == Cancelled) { first?.Cancel(); return(false); } if (a != null) { first?.Cancel(); OnSubscribeHelper.ReportSubscriptionSet(); return(false); } if (Interlocked.CompareExchange(ref field, first, null) == null) { return(true); } } }
public void OnNext(T t) { if (done) { return; } bool b; try { b = predicate(t); } catch (Exception e) { done = true; s.Cancel(); actual.OnError(e); return; } if (!b) { done = true; s.Cancel(); actual.OnComplete(); } actual.OnNext(t); }
public void OnNext(T t) { if (done) { return; } long r = Volatile.Read(ref requested); long p = produced; if (p != r) { produced = p + 1; actual.OnNext(t); } else { try { onDrop?.Invoke(t); } catch (Exception ex) { ExceptionHelper.ThrowIfFatal(ex); s.Cancel(); OnError(ex); } } }
public void OnNext(T t) { if (done) { return; } C b = buffer; if (b == null) { try { b = bufferFactory(); } catch (Exception e) { done = true; s.Cancel(); actual.OnError(e); return; } buffer = b; } b.Add(t); if (b.Count == size) { buffer = default(C); actual.OnNext(b); } }
void OnTerminate() { if (Interlocked.Decrement(ref active) == 0) { upstream.Cancel(); } }
/// <summary> /// Atomically set the given ISubscription on a null target or cancel it if the /// target is not empty or contains the Cancelled instance. /// </summary> /// <param name="current">The target field</param> /// <param name="s">The new value</param> /// <returns>True if successful</returns> public static bool SetOnce(ref ISubscription current, ISubscription s) { if (s == null) { ReportSubscriptionNull(); return(false); } for (;;) { var a = Volatile.Read(ref current); if (a == Cancelled) { s.Cancel(); return(false); } if (a != null) { s.Cancel(); ReportSubscriptionSet(); return(false); } if (Interlocked.CompareExchange(ref current, s, null) == null) { return(true); } } }
/// <summary> /// Atomically sets the single ISubscriber on the target field and requests the accumulated /// request amount. /// </summary> /// <param name="field"></param> /// <param name="requested"></param> /// <param name="s"></param> /// <returns>False if the target field contains the common Cancelled instance or is not null</returns> public static bool SingleSetSubscription(ref ISubscription field, ref long requested, ISubscription s) { ISubscription a = Volatile.Read(ref field); if (a == SubscriptionHelper.Cancelled) { s.Cancel(); return(false); } a = Interlocked.CompareExchange(ref field, s, a); if (a == SubscriptionHelper.Cancelled) { s.Cancel(); return(false); } if (a == null) { long r = Interlocked.Exchange(ref requested, 0); if (r != 0) { s.Request(r); } return(true); } s.Cancel(); OnSubscribeHelper.ReportSubscriptionSet(); return(false); }
public void Cancel() { Volatile.Write(ref cancelled, true); upstream.Cancel(); if (Interlocked.Increment(ref wip) == 1) { Clear(); } }
public void OnNext(T t) { if (!queue.Offer(t)) { s.Cancel(); OnError(BackpressureHelper.MissingBackpressureException()); return; } Schedule(); }
public void OnComplete() { if (_subscription == null) { throw new InvalidOperationException("Cannot call OnComplete before OnSubscribe."); } _observer.OnCompleted(); _subscription.Cancel(); }
public void Cancel() { bp.Cancel(); s.Cancel(); worker.Dispose(); if (bp.Enter()) { q.Clear(); } }
public void OnNext(T t) { if (!sds.Value()) { return; } s.Cancel(); sds.Complete(false, actual); }
public void Cancel() { s.Cancel(); d.Dispose(); worker.Dispose(); if (QueueDrainHelper.Enter(ref wip)) { queue.Clear(); buffer = null; } }
public void OnNext(T t) { if (done) { return; } K key; V value; try { key = keySelector(t); value = valueSelector(t); } catch (Exception ex) { ExceptionHelper.ThrowIfFatal(ex); s.Cancel(); OnError(ex); return; } GroupUnicast g; bool newGroup = false; lock (this) { if (!groups.TryGetValue(key, out g)) { if (Volatile.Read(ref cancelled) != 0) { return; } Interlocked.Increment(ref groupCount); g = new GroupUnicast(this, key, prefetch); groups.Add(key, g); newGroup = true; } } g.OnNext(value); if (newGroup) { queue.Offer(g); Drain(); } }
public void OnNext(T t) { if (sourceMode == FuseableHelper.NONE) { if (!queue.Offer(t)) { s.Cancel(); OnError(BackpressureHelper.MissingBackpressureException("Queue is full?!")); return; } } Schedule(); }
void InnerDone() { if (Interlocked.Decrement(ref wip) == 0) { s.Cancel(); } }
public void OnNext(T t) { hasValue = true; value = t; s.Cancel(); cde.Signal(); }
public async Task RequestAndReplies() { var theOptions = new OptionsWrapper <RabbitMQOptions>(RabbitMQOptions.Default); var thePublisher = new RabbitMQIntegrationRequestPublisher(theOptions); // Request handler registrieren var theServiceCollection = new ServiceCollection(); theServiceCollection.AddIntegrationRequestHandler(typeof(MyRequestHandler)); ServiceProvider theServiceProvider = theServiceCollection.BuildServiceProvider(); var theSubscriber = new RabbitMQIntegrationRequestSubscriber(theOptions, theServiceProvider); ISubscription theSubscription = await theSubscriber.SubscribeAsync <MyRequest, MyReply>(topic : "MyTopic"); // Einen Request senden MyReply theReply = await thePublisher.PublishAsync <MyRequest, MyReply>(new MyRequest { Topic = "MyTopic", Number = 5 }); Assert.That(theReply.DoubledNumber, Is.EqualTo(10)); theSubscription.Cancel(); theSubscriber.Dispose(); thePublisher.Dispose(); }
public void OnNext(T element) { if (hasValue) { try { T v = reducer(value, element); if (v == null) { throw new NullReferenceException("The reducer returned a null value"); } value = v; } catch (Exception ex) { upstream.Cancel(); OnError(ex); return; } } else { hasValue = true; value = element; } }
public async Task Timeout() { var theOptions = new OptionsWrapper <RabbitMQOptions>(RabbitMQOptions.Default); var thePublisher = new RabbitMQIntegrationRequestPublisher(theOptions); // Request handler registrieren var theServiceCollection = new ServiceCollection(); theServiceCollection.AddIntegrationRequestHandler(typeof(MyDelayRequestHandler)); ServiceProvider theServiceProvider = theServiceCollection.BuildServiceProvider(); var theSubscriber = new RabbitMQIntegrationRequestSubscriber(theOptions, theServiceProvider); ISubscription theSubscription = await theSubscriber.SubscribeAsync <MyRequest, MyReply>(); // Einen Request senden Assert.ThrowsAsync <TimeoutException>(async() => { await thePublisher.PublishAsync <MyRequest, MyReply>(new MyRequest { Number = 5 }, timeoutSeconds: 1); }); theSubscription.Cancel(); theSubscriber.Dispose(); thePublisher.Dispose(); }
public void OnNext(T t) { if (done) { return; } bool b; try { b = predicate(t); } catch (Exception e) { done = true; s.Cancel(); actual.OnError(e); return; } if (b) { actual.OnNext(t); } else { s.Request(1); } }
public void OnNext(T t) { if (done) { return; } K k; try { k = keyExtractor(t); } catch (Exception ex) { done = true; s.Cancel(); actual.OnError(ex); return; } if (set.Add(k)) { actual.OnNext(t); } else { s.Request(1); } }
public void Cancel() { if (lvCancelled()) { return; } Volatile.Write(ref cancelled, true); s.Cancel(); CancelAll(); if (QueueDrainHelper.Enter(ref wip)) { var sq = Volatile.Read(ref scalarQueue); sq?.Clear(); } }
public void OnNext(T t) { if (done) { return; } if (skipping) { bool b; try { b = predicate(t); } catch (Exception e) { done = true; s.Cancel(); actual.OnError(e); return; } if (b) { s.Request(1); return; } skipping = false; } actual.OnNext(t); }
public virtual void OnSubscribe(ISubscription subscription) { // As per rule 2.13, we need to throw a `ArgumentNullException` if the `Subscription` is `null` if (subscription == null) { throw new ArgumentNullException(nameof(subscription)); } if (_subscription != null) // If someone has made a mistake and added this Subscriber multiple times, let's handle it gracefully { try { subscription.Cancel(); // Cancel the additional subscription } catch (Exception ex) { //Subscription.cancel is not allowed to throw an exception, according to rule 3.15 System.Diagnostics.Trace.TraceError( new IllegalStateException( subscription + " violated the Reactive Streams rule 3.15 by throwing an exception from cancel.", ex) .StackTrace); } } else { // We have to assign it locally before we use it, if we want to be a synchronous `Subscriber` // Because according to rule 3.10, the Subscription is allowed to call `onNext` synchronously from within `request` _subscription = subscription; } }
public void OnSubscribe(ISubscription s) { if (Volatile.Read(ref subscribers) == Terminated) { s.Cancel(); } }
internal static bool SetOnce(ref ISubscription subscription, ISubscription next, bool crash = true) { if (next == null) { throw new ArgumentNullException(nameof(next)); } ISubscription current = Interlocked.CompareExchange(ref subscription, next, null); if (current == null) { return(true); } next?.Cancel(); if (current != Cancelled) { var e = new InvalidOperationException("ISubscription already set!"); if (crash) { throw e; } else { System.Diagnostics.Debug.WriteLine(e.ToString()); } } return(false); }
public void Cancel() { if (Interlocked.CompareExchange(ref once, 1, 0) == 0) { parent.Remove(connection); upstream.Cancel(); } }
public override void AroundPostStop() { _state.Remove(Self); if (!_canceled) { _subscription?.Cancel(); } base.AroundPostStop(); }
public void OnSubscribe(ISubscription subscription) { _environment.Debug($"WhiteboxSubscriber.OnSubscribe({subscription})"); if (_subscription.IsCompleted()) { subscription.Cancel(); // the Probe must also pass subscriber verification } _probe.RegisterOnSubscribe(new Subscription(subscription)); }