33
33
34
34
import static rx .Observable .create ;
35
35
36
+ import java .util .Random ;
36
37
import java .util .concurrent .atomic .AtomicBoolean ;
37
38
import java .util .concurrent .atomic .AtomicLong ;
38
39
import java .util .concurrent .atomic .AtomicReference ;
@@ -339,9 +340,19 @@ public void onNext(Object t) {
339
340
// if there are outstanding requests
340
341
if (consumerCapacity .get () > 0 ) {
341
342
// schedule resubscription
343
+ // note that at this point consumerCapacity may have dropped to zero
344
+ // but this is a best endeavours check to try not to resubscribe more than
345
+ // we need to
342
346
worker .schedule (subscribeToSource );
343
347
} else {
344
348
// otherwise we indicate that on the next request we should resubscribe
349
+ // because it is a potential waste of resources to schedule resubscription
350
+ // now when there is the possibility that the child may unsubscribe and never use
351
+ // that resubscription
352
+
353
+ //TODO what if consumerCapacity increases now then we are in a situation where the
354
+ // next call of request(n) will do nothing because of the c ==0 check in request(n)
355
+ // this would cause the stream to stall?
345
356
resumeBoundary .compareAndSet (false , true );
346
357
}
347
358
}
@@ -359,9 +370,16 @@ public void setProducer(Producer producer) {
359
370
360
371
@ Override
361
372
public void request (final long n ) {
373
+ // TODO at this point we usually (?) add some optimizations
374
+ // like detecting that we are already on the fast path (non-backpressure)
375
+ // and not attempting to process additional requests
362
376
long c = BackpressureUtils .getAndAddRequest (consumerCapacity , n );
363
377
Producer producer = currentProducer .get ();
364
378
if (producer != null ) {
379
+ // TODO what if at this point the subscription finishes and currentProducer
380
+ // is set to null or even the next producer. The request would be added to consumerCapacity but
381
+ // if it more requests only come after emission then this call to the old producer could produce
382
+ // nothing and the stream would stall
365
383
producer .request (n );
366
384
} else
367
385
if (c == 0 && resumeBoundary .compareAndSet (true , false )) {
0 commit comments