This commit is contained in:
Andrea Cavalli 2021-01-13 22:05:34 +01:00
parent ae0d2e1e68
commit 9b6812982f
4 changed files with 29 additions and 33 deletions

View File

@ -63,7 +63,7 @@ public class EventBusFlux {
subscriptionReady.unregister(subscriptionReadyUnregistered -> { subscriptionReady.unregister(subscriptionReadyUnregistered -> {
if (subscriptionReadyUnregistered.succeeded()) { if (subscriptionReadyUnregistered.succeeded()) {
var subscription = flux.subscribe(item -> { var subscription = flux.subscribe(item -> {
var request = eventBus.request(subscriptionAddress + ".signal", SignalMessage.<T>onNext(item), signalDeliveryOptions, msg2 -> { eventBus.request(subscriptionAddress + ".signal", SignalMessage.<T>onNext(item), signalDeliveryOptions, msg2 -> {
if (msg2.failed()) { if (msg2.failed()) {
logger.error("Failed to send onNext signal", msg2.cause()); logger.error("Failed to send onNext signal", msg2.cause());
} }
@ -83,15 +83,11 @@ public class EventBusFlux {
}); });
cancel.handler(msg3 -> { cancel.handler(msg3 -> {
if (!subscription.isDisposed()) { subscription.dispose();
subscription.dispose();
}
msg3.reply(EMPTY, deliveryOptions); msg3.reply(EMPTY, deliveryOptions);
}); });
dispose.handler(msg2 -> { dispose.handler(msg2 -> {
if (!subscription.isDisposed()) { subscription.dispose();
subscription.dispose();
}
cancel.unregister(v -> { cancel.unregister(v -> {
if (v.failed()) { if (v.failed()) {
logger.error("Failed to unregister cancel", v.cause()); logger.error("Failed to unregister cancel", v.cause());
@ -181,20 +177,28 @@ public class EventBusFlux {
msg2.reply(EMPTY); msg2.reply(EMPTY);
}); });
signalConsumer.completionHandler(h -> { signalConsumer.completionHandler(h -> {
if (h.failed()) { if (h.succeeded()) {
emitter.error(new IllegalStateException("Signal consumer registration failed", msg.cause()));
} else {
eventBus.<Long>request(fluxAddress + ".subscriptionReady", EMPTY, deliveryOptions, msg2 -> { eventBus.<Long>request(fluxAddress + ".subscriptionReady", EMPTY, deliveryOptions, msg2 -> {
if (msg2.failed()) { if (msg2.failed()) {
logger.error("Failed to tell that the subscription is ready"); logger.error("Failed to tell that the subscription is ready");
} }
}); });
} else {
emitter.error(new IllegalStateException("Signal consumer registration failed", msg.cause()));
} }
}); });
emitter.onDispose(() -> eventBus.send(subscriptionAddress + ".dispose", EMPTY, deliveryOptions)); emitter.onDispose(() -> eventBus.request(subscriptionAddress + ".dispose", EMPTY, deliveryOptions, msg2 -> {
if (msg.failed()) {
logger.error("Failed to tell that the subscription is disposed");
}
}));
emitter.onCancel(() -> eventBus.send(subscriptionAddress + ".cancel", EMPTY, deliveryOptions)); emitter.onCancel(() -> eventBus.request(subscriptionAddress + ".cancel", EMPTY, deliveryOptions, msg2 -> {
if (msg.failed()) {
logger.error("Failed to tell that the subscription is cancelled");
}
}));
} else { } else {
emitter.error(new IllegalStateException("Subscription failed", msg.cause())); emitter.error(new IllegalStateException("Subscription failed", msg.cause()));
} }

View File

@ -45,7 +45,7 @@ public class AsyncTdDirectImpl implements AsyncTdDirect {
} }
throw new IllegalStateException("TDLib client is destroyed"); throw new IllegalStateException("TDLib client is destroyed");
} }
}).publishOn(Schedulers.boundedElastic()).single().subscribeOn(tdScheduler)); }).publishOn(Schedulers.boundedElastic()).single());
} else { } else {
return td.asMono().flatMap(td -> Mono.<TdResult<T>>create(sink -> { return td.asMono().flatMap(td -> Mono.<TdResult<T>>create(sink -> {
if (td != null) { if (td != null) {
@ -60,7 +60,7 @@ public class AsyncTdDirectImpl implements AsyncTdDirect {
} }
sink.error(new IllegalStateException("TDLib client is destroyed")); sink.error(new IllegalStateException("TDLib client is destroyed"));
} }
})).single().subscribeOn(tdScheduler); })).single();
} }
} }
@ -89,8 +89,8 @@ public class AsyncTdDirectImpl implements AsyncTdDirect {
closedFromTd.asMono().take(Duration.ofMillis(10)).switchIfEmpty(Mono.fromRunnable(() -> client.send(new Close(), closedFromTd.asMono().take(Duration.ofMillis(10)).switchIfEmpty(Mono.fromRunnable(() -> client.send(new Close(),
result -> logger.trace("Close result: {}", result), result -> logger.trace("Close result: {}", result),
ex -> logger.trace("Error when disposing td client", ex) ex -> logger.trace("Error when disposing td client", ex)
))).subscribeOn(tdScheduler).subscribe(); ))).subscribe();
}); });
}).subscribeOn(tdScheduler); });
} }
} }

View File

@ -39,8 +39,6 @@ import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import reactor.core.publisher.Sinks; import reactor.core.publisher.Sinks;
import reactor.core.publisher.Sinks.Many; import reactor.core.publisher.Sinks.Many;
import reactor.core.scheduler.Scheduler;
import reactor.core.scheduler.Schedulers;
public class AsyncTdMiddleEventBusClient extends AbstractVerticle implements AsyncTdMiddle { public class AsyncTdMiddleEventBusClient extends AbstractVerticle implements AsyncTdMiddle {
@ -49,7 +47,6 @@ public class AsyncTdMiddleEventBusClient extends AbstractVerticle implements Asy
public static final boolean OUTPUT_REQUESTS = false; public static final boolean OUTPUT_REQUESTS = false;
public static final byte[] EMPTY = new byte[0]; public static final byte[] EMPTY = new byte[0];
private final Scheduler tdMiddleScheduler = Schedulers.single();
private final Many<Boolean> tdClosed = Sinks.many().replay().latestOrDefault(false); private final Many<Boolean> tdClosed = Sinks.many().replay().latestOrDefault(false);
private final DeliveryOptions deliveryOptions; private final DeliveryOptions deliveryOptions;
private final DeliveryOptions deliveryOptionsWithTimeout; private final DeliveryOptions deliveryOptionsWithTimeout;
@ -149,7 +146,6 @@ public class AsyncTdMiddleEventBusClient extends AbstractVerticle implements Asy
if (msg.succeeded()) { if (msg.succeeded()) {
this.listen() this.listen()
.timeout(Duration.ofSeconds(30)) .timeout(Duration.ofSeconds(30))
.subscribeOn(tdMiddleScheduler)
.subscribe(v -> {}, future::fail, future::complete); .subscribe(v -> {}, future::fail, future::complete);
} else { } else {
future.fail(msg.cause()); future.fail(msg.cause());
@ -244,7 +240,7 @@ public class AsyncTdMiddleEventBusClient extends AbstractVerticle implements Asy
tdClosed.tryEmitNext(true); tdClosed.tryEmitNext(true);
} }
} }
})).subscribeOn(tdMiddleScheduler); }));
} }
@Override @Override
@ -300,6 +296,6 @@ public class AsyncTdMiddleEventBusClient extends AbstractVerticle implements Asy
} }
}).switchIfEmpty(Mono.fromSupplier(() -> { }).switchIfEmpty(Mono.fromSupplier(() -> {
return TdResult.failed(new TdApi.Error(500, "Client is closed or response is empty")); return TdResult.failed(new TdApi.Error(500, "Client is closed or response is empty"));
})).subscribeOn(tdMiddleScheduler); }));
} }
} }

View File

@ -33,8 +33,6 @@ import java.util.function.Consumer;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import reactor.core.scheduler.Scheduler;
import reactor.core.scheduler.Schedulers;
public class AsyncTdMiddleEventBusServer { public class AsyncTdMiddleEventBusServer {
@ -54,7 +52,6 @@ public class AsyncTdMiddleEventBusServer {
private boolean local; private boolean local;
protected AsyncTdDirectImpl td; protected AsyncTdDirectImpl td;
private final Scheduler tdSrvPoll;
/** /**
* Value is not important, emits when a request is received * Value is not important, emits when a request is received
*/ */
@ -68,7 +65,6 @@ public class AsyncTdMiddleEventBusServer {
public AsyncTdMiddleEventBusServer(TdClusterManager clusterManager) { public AsyncTdMiddleEventBusServer(TdClusterManager clusterManager) {
this.cluster = clusterManager; this.cluster = clusterManager;
this.tdOptions = new AsyncTdDirectOptions(WAIT_DURATION, 1000); this.tdOptions = new AsyncTdDirectOptions(WAIT_DURATION, 1000);
this.tdSrvPoll = Schedulers.single();
if (cluster.registerDefaultCodec(TdResultList.class, new TdResultListMessageCodec())) { if (cluster.registerDefaultCodec(TdResultList.class, new TdResultListMessageCodec())) {
cluster.registerDefaultCodec(ExecuteObject.class, new TdExecuteObjectMessageCodec()); cluster.registerDefaultCodec(ExecuteObject.class, new TdExecuteObjectMessageCodec());
cluster.registerDefaultCodec(TdResultMessage.class, new TdResultMessageCodec()); cluster.registerDefaultCodec(TdResultMessage.class, new TdResultMessageCodec());
@ -99,7 +95,7 @@ public class AsyncTdMiddleEventBusServer {
workingMsg.reply(EMPTY, cluster.newDeliveryOpts().setLocalOnly(local)); workingMsg.reply(EMPTY, cluster.newDeliveryOpts().setLocalOnly(local));
}); });
this.isWorkingConsumer.completionHandler(MonoUtils.toHandler(registrationSink)); this.isWorkingConsumer.completionHandler(MonoUtils.toHandler(registrationSink));
})).subscribeOn(this.tdSrvPoll) }))
.subscribe(v -> {}, ex -> { .subscribe(v -> {}, ex -> {
logger.info(botAddress + " server deployed and started. succeeded: false"); logger.info(botAddress + " server deployed and started. succeeded: false");
logger.error(ex.getLocalizedMessage(), ex); logger.error(ex.getLocalizedMessage(), ex);
@ -123,7 +119,7 @@ public class AsyncTdMiddleEventBusServer {
sink.error(h.cause()); sink.error(h.cause());
} }
}); });
}).subscribeOn(tdSrvPoll); });
} }
public void onBeforeStop(Consumer<Promise<Void>> r) { public void onBeforeStop(Consumer<Promise<Void>> r) {
@ -180,7 +176,7 @@ public class AsyncTdMiddleEventBusServer {
} catch (Exception ex) { } catch (Exception ex) {
sink.error(ex); sink.error(ex);
} }
}).subscribeOn(this.tdSrvPoll) })
.subscribe(response -> {}, ex -> { .subscribe(response -> {}, ex -> {
logger.error("Error when processing a request", ex); logger.error("Error when processing a request", ex);
msg.fail(500, ex.getLocalizedMessage()); msg.fail(500, ex.getLocalizedMessage());
@ -191,7 +187,7 @@ public class AsyncTdMiddleEventBusServer {
} }
}); });
executeConsumer.completionHandler(MonoUtils.toHandler(registrationSink)); executeConsumer.completionHandler(MonoUtils.toHandler(registrationSink));
}).subscribeOn(tdSrvPoll); });
} }
private void undeploy(Runnable whenUndeployed) { private void undeploy(Runnable whenUndeployed) {
@ -227,7 +223,7 @@ public class AsyncTdMiddleEventBusServer {
whenUndeployed.run(); whenUndeployed.run();
}); });
}).subscribeOn(this.tdSrvPoll).subscribe(v -> {}, ex -> { }).subscribe(v -> {}, ex -> {
logger.error("Error when stopping", ex); logger.error("Error when stopping", ex);
}, () -> {}); }, () -> {});
}); });
@ -251,7 +247,7 @@ public class AsyncTdMiddleEventBusServer {
System.out.println("<=: end (3)"); System.out.println("<=: end (3)");
} }
this.undeploy(() -> {}); this.undeploy(() -> {});
}).subscribeOn(tdSrvPoll); });
var fluxCodec = new TdResultListMessageCodec(); var fluxCodec = new TdResultListMessageCodec();
return EventBusFlux.<TdResultList>serve(updatesFlux, return EventBusFlux.<TdResultList>serve(updatesFlux,
cluster.getEventBus(), cluster.getEventBus(),
@ -259,6 +255,6 @@ public class AsyncTdMiddleEventBusServer {
cluster.newDeliveryOpts().setLocalOnly(local), cluster.newDeliveryOpts().setLocalOnly(local),
fluxCodec, fluxCodec,
Duration.ofSeconds(30) Duration.ofSeconds(30)
).subscribeOn(tdSrvPoll); );
} }
} }