Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract the single message processing functionality from processOutstandingBatches #4618

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -402,51 +402,53 @@ public void processOutstandingBatches() {
batchCallback = nextBatch.doneCallback;
}
}

final PubsubMessage message = outstandingMessage.receivedMessage().getMessage();
final AckHandler ackHandler = outstandingMessage.ackHandler();
final SettableApiFuture<AckReply> response = SettableApiFuture.create();
final AckReplyConsumer consumer =
new AckReplyConsumer() {
@Override
public void ack() {
response.set(AckReply.ACK);
}

@Override
public void nack() {
response.set(AckReply.NACK);
}
};
ApiFutures.addCallback(response, ackHandler, MoreExecutors.directExecutor());
executor.execute(
new Runnable() {
@Override
public void run() {
try {
if (ackHandler
.totalExpiration
.plusSeconds(messageDeadlineSeconds.get())
.isBefore(now())) {
// Message expired while waiting. We don't extend these messages anymore,
// so it was probably sent to someone else. Don't work on it.
// Don't nack it either, because we'd be nacking someone else's message.
ackHandler.forget();
return;
}

receiver.receiveMessage(message, consumer);
} catch (Exception e) {
response.setException(e);
}
}
});
processOutstandingMessage(
outstandingMessage.receivedMessage.getMessage(), outstandingMessage.ackHandler);
if (batchDone) {
batchCallback.run();
}
}
}

private void processOutstandingMessage(final PubsubMessage message, final AckHandler ackHandler) {
final SettableApiFuture<AckReply> response = SettableApiFuture.create();
final AckReplyConsumer consumer =
new AckReplyConsumer() {
@Override
public void ack() {
response.set(AckReply.ACK);
}

@Override
public void nack() {
response.set(AckReply.NACK);
}
};
ApiFutures.addCallback(response, ackHandler, MoreExecutors.directExecutor());
executor.execute(
new Runnable() {
@Override
public void run() {
try {
if (ackHandler
.totalExpiration
.plusSeconds(messageDeadlineSeconds.get())
.isBefore(now())) {
// Message expired while waiting. We don't extend these messages anymore,
// so it was probably sent to someone else. Don't work on it.
// Don't nack it either, because we'd be nacking someone else's message.
ackHandler.forget();
return;
}

receiver.receiveMessage(message, consumer);
} catch (Exception e) {
response.setException(e);
}
}
});
}

/** Compute the ideal deadline, set subsequent modacks to this deadline, and return it. */
@InternalApi
int computeDeadlineSeconds() {
Expand Down