Check if the client is really closed

This commit is contained in:
Andrea Cavalli 2022-01-09 20:50:58 +01:00
parent fd0bfda2eb
commit 2156ec9ed7

View File

@ -16,13 +16,13 @@ import reactor.core.publisher.Mono;
public class AtomixReactiveApiMultiClient implements ReactiveApiMultiClient, AutoCloseable { public class AtomixReactiveApiMultiClient implements ReactiveApiMultiClient, AutoCloseable {
private final ReactiveApi api;
private final ClusterEventService eventService; private final ClusterEventService eventService;
private final Flux<ClientBoundEvent> clientBoundEvents; private final Flux<ClientBoundEvent> clientBoundEvents;
private volatile boolean closed = false;
AtomixReactiveApiMultiClient(AtomixReactiveApi api) { AtomixReactiveApiMultiClient(AtomixReactiveApi api) {
this.api = api;
this.eventService = api.getAtomix().getEventService(); this.eventService = api.getAtomix().getEventService();
clientBoundEvents = Flux clientBoundEvents = Flux
@ -38,6 +38,7 @@ public class AtomixReactiveApiMultiClient implements ReactiveApiMultiClient, Aut
sink.onDispose(() -> subscriptionFuture.thenAccept(Subscription::close)); sink.onDispose(() -> subscriptionFuture.thenAccept(Subscription::close));
}, OverflowStrategy.ERROR) }, OverflowStrategy.ERROR)
.onBackpressureBuffer(0xFFFF, BufferOverflowStrategy.ERROR) .onBackpressureBuffer(0xFFFF, BufferOverflowStrategy.ERROR)
.takeUntil(s -> closed)
.share(); .share();
} }
@ -48,21 +49,24 @@ public class AtomixReactiveApiMultiClient implements ReactiveApiMultiClient, Aut
@Override @Override
public <T extends TdApi.Object> Mono<T> request(long userId, long liveId, TdApi.Function<T> request, Instant timeout) { public <T extends TdApi.Object> Mono<T> request(long userId, long liveId, TdApi.Function<T> request, Instant timeout) {
return Mono.fromCompletionStage(() -> eventService.send("session-" + liveId + "-requests", return Mono.fromCompletionStage(() -> {
if (closed) {
return CompletableFuture.failedFuture(new TdError(500, "Session is closed"));
}
return eventService.send("session-" + liveId + "-requests",
new Request<>(liveId, request, timeout), new Request<>(liveId, request, timeout),
LiveAtomixReactiveApiClient::serializeRequest, LiveAtomixReactiveApiClient::serializeRequest,
LiveAtomixReactiveApiClient::deserializeResponse, LiveAtomixReactiveApiClient::deserializeResponse,
Duration.between(Instant.now(), timeout) Duration.between(Instant.now(), timeout)
)) );
.<T>handle((item, sink) -> { }).<T>handle((item, sink) -> {
if (item instanceof TdApi.Error error) { if (item instanceof TdApi.Error error) {
sink.error(new TdError(error.code, error.message)); sink.error(new TdError(error.code, error.message));
} else { } else {
//noinspection unchecked //noinspection unchecked
sink.next((T) item); sink.next((T) item);
} }
}) }).onErrorMap(ex -> {
.onErrorMap(ex -> {
if (ex instanceof MessagingException.NoRemoteHandler) { if (ex instanceof MessagingException.NoRemoteHandler) {
return new TdError(404, "Bot #IDU" + userId + " (live id: " + liveId + ") is not found on the cluster"); return new TdError(404, "Bot #IDU" + userId + " (live id: " + liveId + ") is not found on the cluster");
} else { } else {
@ -73,5 +77,6 @@ public class AtomixReactiveApiMultiClient implements ReactiveApiMultiClient, Aut
@Override @Override
public void close() { public void close() {
closed = true;
} }
} }