Skip to content

Commit

Permalink
Clean up MessageDispatcher by changing processOutstandingBatches to e…
Browse files Browse the repository at this point in the history
…xplicitly loop instead of while(true) with breaks. There is now only 1 explicit return and 1 runtime error. (#4619)
  • Loading branch information
dpcollins-google authored and sduskis committed Mar 4, 2019
1 parent 4517817 commit 5e0acc0
Showing 1 changed file with 24 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -371,41 +371,31 @@ public void processReceivedMessages(List<ReceivedMessage> messages, Runnable don
processOutstandingBatches();
}

public void processOutstandingBatches() {
while (true) {
boolean batchDone = false;
Runnable batchCallback = null;
OutstandingMessage outstandingMessage;
synchronized (outstandingMessageBatches) {
OutstandingMessageBatch nextBatch = outstandingMessageBatches.peek();
if (nextBatch == null) {
return;
}
outstandingMessage = nextBatch.messages.peek();
if (outstandingMessage == null) {
return;
}
try {
// This is a non-blocking flow controller.
flowController.reserve(
1, outstandingMessage.receivedMessage().getMessage().getSerializedSize());
} catch (FlowController.MaxOutstandingElementCountReachedException
| FlowController.MaxOutstandingRequestBytesReachedException flowControlException) {
return;
} catch (FlowControlException unexpectedException) {
throw new IllegalStateException("Flow control unexpected exception", unexpectedException);
}
nextBatch.messages.poll(); // We got a hold to the message already.
batchDone = nextBatch.messages.isEmpty();
if (batchDone) {
outstandingMessageBatches.poll();
batchCallback = nextBatch.doneCallback;
private void processOutstandingBatches() {
synchronized (outstandingMessageBatches) {
for (OutstandingMessageBatch nextBatch = outstandingMessageBatches.poll();
nextBatch != null;
nextBatch = outstandingMessageBatches.poll()) {
for (OutstandingMessage nextMessage = nextBatch.messages.poll();
nextMessage != null;
nextMessage = nextBatch.messages.poll()) {
try {
// This is a non-blocking flow controller.
flowController.reserve(1, nextMessage.receivedMessage.getMessage().getSerializedSize());
} catch (FlowController.MaxOutstandingElementCountReachedException
| FlowController.MaxOutstandingRequestBytesReachedException flowControlException) {
// Unwind previous changes in the batches outstanding.
nextBatch.messages.addFirst(nextMessage);
outstandingMessageBatches.addFirst(nextBatch);
return;
} catch (FlowControlException unexpectedException) {
throw new IllegalStateException(
"Flow control unexpected exception", unexpectedException);
}
processOutstandingMessage(
nextMessage.receivedMessage.getMessage(), nextMessage.ackHandler);
}
}
processOutstandingMessage(
outstandingMessage.receivedMessage.getMessage(), outstandingMessage.ackHandler);
if (batchDone) {
batchCallback.run();
nextBatch.doneCallback.run();
}
}
}
Expand Down

0 comments on commit 5e0acc0

Please sign in to comment.