@@ -194,7 +194,7 @@ public void onError(Throwable e) {
194
194
// If we already have items queued when a request comes in we vend those and decrement the outstanding request count
195
195
196
196
void requestFromGroupedObservable (long n , GroupState <K , T > group ) {
197
- group .requested . getAndAdd ( n );
197
+ BackpressureUtils . getAndAddRequest ( group .requested , n );
198
198
if (group .count .getAndIncrement () == 0 ) {
199
199
pollQueue (group );
200
200
}
@@ -330,13 +330,19 @@ private void cleanupGroup(Object key) {
330
330
private void emitItem (GroupState <K , T > groupState , Object item ) {
331
331
Queue <Object > q = groupState .buffer ;
332
332
AtomicLong keyRequested = groupState .requested ;
333
+ //don't need to check for requested being Long.MAX_VALUE because this
334
+ //field is capped at MAX_QUEUE_SIZE
333
335
REQUESTED .decrementAndGet (this );
334
336
// short circuit buffering
335
337
if (keyRequested != null && keyRequested .get () > 0 && (q == null || q .isEmpty ())) {
336
338
@ SuppressWarnings ("unchecked" )
337
339
Observer <Object > obs = (Observer <Object >)groupState .getObserver ();
338
340
nl .accept (obs , item );
339
- keyRequested .decrementAndGet ();
341
+ if (keyRequested .get () != Long .MAX_VALUE ) {
342
+ // best endeavours check (no CAS loop here) because we mainly care about
343
+ // the initial request being Long.MAX_VALUE and that value being conserved.
344
+ keyRequested .decrementAndGet ();
345
+ }
340
346
} else {
341
347
q .add (item );
342
348
BUFFERED_COUNT .incrementAndGet (this );
@@ -381,7 +387,11 @@ private void drainIfPossible(GroupState<K, T> groupState) {
381
387
@ SuppressWarnings ("unchecked" )
382
388
Observer <Object > obs = (Observer <Object >)groupState .getObserver ();
383
389
nl .accept (obs , t );
384
- groupState .requested .decrementAndGet ();
390
+ if (groupState .requested .get ()!=Long .MAX_VALUE ) {
391
+ // best endeavours check (no CAS loop here) because we mainly care about
392
+ // the initial request being Long.MAX_VALUE and that value being conserved.
393
+ groupState .requested .decrementAndGet ();
394
+ }
385
395
BUFFERED_COUNT .decrementAndGet (this );
386
396
387
397
// if we have used up all the events we requested from upstream then figure out what to ask for this time based on the empty space in the buffer
0 commit comments