Skip to content

Commit 47ebbbf

Browse files
committed
fix Amb backpressure problems
1 parent 1a85656 commit 47ebbbf

File tree

2 files changed

+98
-23
lines changed

2 files changed

+98
-23
lines changed

src/main/java/rx/internal/operators/OnSubscribeAmb.java

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Collection;
2020
import java.util.List;
2121
import java.util.concurrent.ConcurrentLinkedQueue;
22+
import java.util.concurrent.atomic.AtomicBoolean;
2223
import java.util.concurrent.atomic.AtomicReference;
2324

2425
import rx.Observable;
@@ -343,30 +344,54 @@ public void unsubscribeOthers(AmbSubscriber<T> notThis) {
343344
}
344345

345346
}
346-
347-
private final Iterable<? extends Observable<? extends T>> sources;
348-
private final Selection<T> selection = new Selection<T>();
349-
347+
348+
//give default access instead of private as a micro-optimization
349+
//for access from anonymous classes below
350+
final Iterable<? extends Observable<? extends T>> sources;
351+
final Selection<T> selection = new Selection<T>();
352+
final AtomicReference<AmbSubscriber<T>> choice = selection.choice;
353+
350354
private OnSubscribeAmb(Iterable<? extends Observable<? extends T>> sources) {
351355
this.sources = sources;
352356
}
353357

354358
@Override
355359
public void call(final Subscriber<? super T> subscriber) {
360+
361+
//need to subscribe to all the sources
362+
for (Observable<? extends T> source : sources) {
363+
if (subscriber.isUnsubscribed()) {
364+
return;
365+
}
366+
AmbSubscriber<T> ambSubscriber = new AmbSubscriber<T>(0, subscriber, selection);
367+
selection.ambSubscribers.add(ambSubscriber);
368+
// check again if choice has been made so can stop subscribing
369+
// if all sources were backpressure aware then this check
370+
// would be pointless given that 0 was requested above from each ambSubscriber
371+
AmbSubscriber<T> c;
372+
if ((c = choice.get()) != null) {
373+
// Already chose one, the rest can be skipped and we can clean up
374+
selection.unsubscribeOthers(c);
375+
return;
376+
}
377+
source.unsafeSubscribe(ambSubscriber);
378+
}
356379
subscriber.add(Subscriptions.create(new Action0() {
357380

358381
@Override
359382
public void call() {
360-
if (selection.choice.get() != null) {
383+
AmbSubscriber<T> c;
384+
if ((c = choice.get()) != null) {
361385
// there is a single winner so we unsubscribe it
362-
selection.choice.get().unsubscribe();
386+
c.unsubscribe();
363387
}
364388
// if we are racing with others still existing, we'll also unsubscribe them
365-
if(!selection.ambSubscribers.isEmpty()) {
366-
for (AmbSubscriber<T> other : selection.ambSubscribers) {
389+
Collection<AmbSubscriber<T>> ambSubs = selection.ambSubscribers;
390+
if(!ambSubs.isEmpty()) {
391+
for (AmbSubscriber<T> other : ambSubs) {
367392
other.unsubscribe();
368393
}
369-
selection.ambSubscribers.clear();
394+
ambSubs.clear();
370395
}
371396
}
372397

@@ -375,23 +400,25 @@ public void call() {
375400

376401
@Override
377402
public void request(long n) {
378-
if (selection.choice.get() != null) {
403+
final AmbSubscriber<T> c;
404+
if ((c = choice.get()) != null) {
379405
// propagate the request to that single Subscriber that won
380-
selection.choice.get().requestMore(n);
406+
c.requestMore(n);
381407
} else {
382-
for (Observable<? extends T> source : sources) {
383-
if (subscriber.isUnsubscribed()) {
384-
break;
385-
}
386-
AmbSubscriber<T> ambSubscriber = new AmbSubscriber<T>(n, subscriber, selection);
387-
selection.ambSubscribers.add(ambSubscriber);
388-
// possible race condition in previous lines ... a choice may have been made so double check (instead of synchronizing)
389-
if (selection.choice.get() != null) {
390-
// Already chose one, the rest can be skipped and we can clean up
391-
selection.unsubscribeOthers(selection.choice.get());
392-
break;
408+
//propagate the request to all the amb subscribers
409+
for (AmbSubscriber<T> ambSubscriber: selection.ambSubscribers) {
410+
if (!ambSubscriber.isUnsubscribed()) {
411+
// make a best endeavours check to not waste requests
412+
// if first emission has already occurred
413+
if (choice.get() == ambSubscriber) {
414+
ambSubscriber.requestMore(n);
415+
// don't need to request from other subscribers because choice has been made
416+
// and request has gone to choice
417+
return;
418+
} else {
419+
ambSubscriber.requestMore(n);
420+
}
393421
}
394-
source.unsafeSubscribe(ambSubscriber);
395422
}
396423
}
397424
}

src/test/java/rx/internal/operators/OnSubscribeAmbTest.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static rx.internal.operators.OnSubscribeAmb.amb;
2323

2424
import java.io.IOException;
25+
import java.util.Arrays;
2526
import java.util.concurrent.TimeUnit;
2627
import java.util.concurrent.atomic.AtomicLong;
2728

@@ -219,4 +220,51 @@ public void testBackpressure() {
219220
ts.assertNoErrors();
220221
assertEquals(RxRingBuffer.SIZE * 2, ts.getOnNextEvents().size());
221222
}
223+
224+
225+
@Test
226+
public void testSubscriptionOnlyHappensOnce() throws InterruptedException {
227+
final AtomicLong count = new AtomicLong();
228+
Action0 incrementer = new Action0() {
229+
@Override
230+
public void call() {
231+
count.incrementAndGet();
232+
}
233+
};
234+
//this aync stream should emit first
235+
Observable<Integer> o1 = Observable.just(1).doOnSubscribe(incrementer)
236+
.delay(100, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.computation());
237+
//this stream emits second
238+
Observable<Integer> o2 = Observable.just(1).doOnSubscribe(incrementer)
239+
.delay(100, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.computation());
240+
TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
241+
Observable.amb(o1, o2).subscribe(ts);
242+
ts.requestMore(1);
243+
ts.awaitTerminalEvent(5, TimeUnit.SECONDS);
244+
ts.assertNoErrors();
245+
assertEquals(2, count.get());
246+
}
247+
248+
@Test
249+
public void testSecondaryRequestsPropagatedToChildren() throws InterruptedException {
250+
//this aync stream should emit first
251+
Observable<Integer> o1 = Observable.from(Arrays.asList(1, 2, 3))
252+
.delay(100, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.computation());
253+
//this stream emits second
254+
Observable<Integer> o2 = Observable.from(Arrays.asList(4, 5, 6))
255+
.delay(200, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.computation());
256+
TestSubscriber<Integer> ts = new TestSubscriber<Integer>() {
257+
@Override
258+
public void onStart() {
259+
request(1);
260+
}};
261+
Observable.amb(o1, o2).subscribe(ts);
262+
// before first emission request 20 more
263+
// this request should suffice to emit all
264+
ts.requestMore(20);
265+
//ensure stream does not hang
266+
ts.awaitTerminalEvent(5, TimeUnit.SECONDS);
267+
ts.assertNoErrors();
268+
}
269+
222270
}

0 commit comments

Comments
 (0)