Don't discard queue

This commit is contained in:
Andrea Cavalli 2021-07-31 18:01:29 +02:00
parent 21097c5135
commit abbf91cdf1
2 changed files with 24 additions and 7 deletions

View File

@ -340,7 +340,7 @@ public class AsyncTdMiddleEventBusServer extends AbstractVerticle {
} }
})) }))
.limitRate(Math.max(1, tdOptions.getEventsSize())) .limitRate(Math.max(1, tdOptions.getEventsSize()))
.transform(normal -> new BufferTimeOutPublisher<>(normal, Math.max(1, tdOptions.getEventsSize()), local ? Duration.ofMillis(1) : Duration.ofMillis(100))) .transform(normal -> new BufferTimeOutPublisher<>(normal, Math.max(1, tdOptions.getEventsSize()), local ? Duration.ofMillis(1) : Duration.ofMillis(100), false))
//.bufferTimeout(Math.max(1, tdOptions.getEventsSize()), local ? Duration.ofMillis(1) : Duration.ofMillis(100)) //.bufferTimeout(Math.max(1, tdOptions.getEventsSize()), local ? Duration.ofMillis(1) : Duration.ofMillis(100))
//.map(List::of) //.map(List::of)
.limitRate(Math.max(1, tdOptions.getEventsSize())) .limitRate(Math.max(1, tdOptions.getEventsSize()))

View File

@ -16,6 +16,7 @@ import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
import reactor.core.CoreSubscriber;
@SuppressWarnings("ReactiveStreamsPublisherImplementation") @SuppressWarnings("ReactiveStreamsPublisherImplementation")
public class BufferTimeOutPublisher<T> implements Publisher<List<T>> { public class BufferTimeOutPublisher<T> implements Publisher<List<T>> {
@ -25,23 +26,26 @@ public class BufferTimeOutPublisher<T> implements Publisher<List<T>> {
private final Publisher<T> source; private final Publisher<T> source;
private final int size; private final int size;
private final long duration; private final long duration;
private final boolean discardOnError;
public BufferTimeOutPublisher(Publisher<T> source, int size, Duration duration) { public BufferTimeOutPublisher(Publisher<T> source, int size, Duration duration, boolean discardOnError) {
this.source = source; this.source = source;
this.size = size; this.size = size;
this.duration = duration.toMillis(); this.duration = duration.toMillis();
this.discardOnError = discardOnError;
} }
@Override @Override
public void subscribe(Subscriber<? super List<T>> subscriber) { public void subscribe(Subscriber<? super List<T>> subscriber) {
subscriber.onSubscribe(new BufferTimeOutSubscription<T>(source, subscriber, size, duration)); subscriber.onSubscribe(new BufferTimeOutSubscription<T>(source, subscriber, size, duration, discardOnError));
} }
protected static class BufferTimeOutSubscription<T> implements Subscription, Subscriber<T> { protected static class BufferTimeOutSubscription<T> implements Subscription, CoreSubscriber<T> {
private final Subscriber<? super List<T>> subscriber; private final Subscriber<? super List<T>> subscriber;
private final int size; private final int size;
private final long duration; private final long duration;
private final boolean discardOnError;
private Subscription subscription; private Subscription subscription;
private final ReentrantLock lock = new ReentrantLock(); private final ReentrantLock lock = new ReentrantLock();
@ -59,10 +63,12 @@ public class BufferTimeOutPublisher<T> implements Publisher<List<T>> {
public BufferTimeOutSubscription(Publisher<T> source, public BufferTimeOutSubscription(Publisher<T> source,
Subscriber<? super List<T>> subscriber, Subscriber<? super List<T>> subscriber,
int size, int size,
long duration) { long duration,
boolean discardOnError) {
this.subscriber = subscriber; this.subscriber = subscriber;
this.size = size; this.size = size;
this.duration = duration; this.duration = duration;
this.discardOnError = discardOnError;
this.buffer = new ArrayList<>(size); this.buffer = new ArrayList<>(size);
source.subscribe(this); source.subscribe(this);
} }
@ -144,8 +150,19 @@ public class BufferTimeOutPublisher<T> implements Publisher<List<T>> {
@Override @Override
public void onError(Throwable t) { public void onError(Throwable t) {
scheduledFuture.cancel(false); if (discardOnError) {
subscriber.onError(t); scheduledFuture.cancel(false);
subscriber.onError(t);
} else {
lock.lock();
try {
checkSend();
scheduledFuture.cancel(false);
subscriber.onError(t);
} finally {
lock.unlock();
}
}
} }
@Override @Override