Add optional message limit for receive command

This commit is contained in:
AsamK 2022-10-31 11:17:52 +01:00
parent 5ed9db4f08
commit de2bfc7f79
6 changed files with 59 additions and 43 deletions

View file

@ -202,12 +202,9 @@ public interface Manager extends Closeable {
/**
* Receive new messages from server, returns if no new message arrive in a timespan of timeout.
*/
void receiveMessages(Duration timeout, ReceiveMessageHandler handler) throws IOException;
/**
* Receive new messages from server, returns only if the thread is interrupted.
*/
void receiveMessages(ReceiveMessageHandler handler) throws IOException;
public void receiveMessages(
Optional<Duration> timeout, Optional<Integer> maxMessages, ReceiveMessageHandler handler
) throws IOException;
void setReceiveConfig(ReceiveConfig receiveConfig);

View file

@ -961,17 +961,16 @@ class ManagerImpl implements Manager {
}
@Override
public void receiveMessages(Duration timeout, ReceiveMessageHandler handler) throws IOException {
receiveMessages(timeout, true, handler);
}
@Override
public void receiveMessages(ReceiveMessageHandler handler) throws IOException {
receiveMessages(Duration.ofMinutes(1), false, handler);
public void receiveMessages(
Optional<Duration> timeout,
Optional<Integer> maxMessages,
ReceiveMessageHandler handler
) throws IOException {
receiveMessages(timeout.orElse(Duration.ofMinutes(1)), timeout.isPresent(), maxMessages.orElse(null), handler);
}
private void receiveMessages(
Duration timeout, boolean returnOnTimeout, ReceiveMessageHandler handler
Duration timeout, boolean returnOnTimeout, Integer maxMessages, ReceiveMessageHandler handler
) throws IOException {
if (isReceiving()) {
throw new IllegalStateException("Already receiving message.");
@ -979,7 +978,7 @@ class ManagerImpl implements Manager {
isReceivingSynchronous = true;
receiveThread = Thread.currentThread();
try {
context.getReceiveHelper().receiveMessages(timeout, returnOnTimeout, handler);
context.getReceiveHelper().receiveMessages(timeout, returnOnTimeout, maxMessages, handler);
} finally {
receiveThread = null;
isReceivingSynchronous = false;

View file

@ -80,7 +80,7 @@ public class ReceiveHelper {
public void receiveMessagesContinuously(Manager.ReceiveMessageHandler handler) {
while (!shouldStop) {
try {
receiveMessages(Duration.ofMinutes(1), false, handler);
receiveMessages(Duration.ofMinutes(1), false, null, handler);
break;
} catch (IOException e) {
logger.warn("Receiving messages failed, retrying", e);
@ -89,7 +89,7 @@ public class ReceiveHelper {
}
public void receiveMessages(
Duration timeout, boolean returnOnTimeout, Manager.ReceiveMessageHandler handler
Duration timeout, boolean returnOnTimeout, Integer maxMessages, Manager.ReceiveMessageHandler handler
) throws IOException {
needsToRetryFailedMessages = true;
hasCaughtUpWithOldMessages = false;
@ -107,7 +107,7 @@ public class ReceiveHelper {
signalWebSocket.connect();
try {
receiveMessagesInternal(signalWebSocket, timeout, returnOnTimeout, handler, queuedActions);
receiveMessagesInternal(signalWebSocket, timeout, returnOnTimeout, maxMessages, handler, queuedActions);
} finally {
hasCaughtUpWithOldMessages = false;
handleQueuedActions(queuedActions.keySet());
@ -122,13 +122,15 @@ public class ReceiveHelper {
final SignalWebSocket signalWebSocket,
Duration timeout,
boolean returnOnTimeout,
Integer maxMessages,
Manager.ReceiveMessageHandler handler,
final Map<HandleAction, HandleAction> queuedActions
) throws IOException {
int remainingMessages = maxMessages == null ? -1 : maxMessages;
var backOffCounter = 0;
isWaitingForMessage = false;
while (!shouldStop) {
while (!shouldStop && remainingMessages != 0) {
if (needsToRetryFailedMessages) {
retryFailedReceivedMessages(handler);
needsToRetryFailedMessages = false;
@ -154,6 +156,9 @@ public class ReceiveHelper {
backOffCounter = 0;
if (result.isPresent()) {
if (remainingMessages > 0) {
remainingMessages -= 1;
}
envelope = result.get();
logger.debug("New message received from server");
} else {