<commit_before>package io.muoncore.api;

import io.muoncore.channel.ChannelConnection;
import org.reactivestreams.Publisher;
import reactor.rx.broadcast.Broadcaster;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * Take a bidirectional channel and add request/ response semantics to
 * it at the code level.
 */
public class ChannelFutureAdapter<Receive, Send> {

    private ChannelConnection<Send, Receive> channelConnection;

    public ChannelFutureAdapter(ChannelConnection<Send, Receive> channelConnection) {
        this.channelConnection = channelConnection;
    }

    public MuonFuture<Receive> request(Send obj) {
        ChannelFuture<Receive> ret = new ChannelFuture<>();

        channelConnection.receive(ret::setData);
        channelConnection.send(obj);

        return ret;
    }

    class ChannelFuture<X> implements MuonFuture<X> {

        final CountDownLatch responseReceivedSignal = new CountDownLatch(1);

        private volatile boolean isDone;
        private X data;
        private boolean cancelled = false;
        private PromiseFunction<X> onFulfilled;
        private Broadcaster<X> broadcast;

        public void setData(X data) {
            if (isDone) return;

            this.data = data;
            if (onFulfilled != null) {
                onFulfilled.call(data);
            }
            if (broadcast != null) {
                broadcast.accept(data);
            }
            shutdown();
            responseReceivedSignal.countDown();
        }

        @Override
        public Publisher<X> toPublisher() {
            if (broadcast == null) {
                broadcast = Broadcaster.create();
            }
            return broadcast;
        }

        @Override
        public boolean cancel(boolean b) {
            if (isDone) return false;
            shutdown();
            isDone = true;
            cancelled = true;
            return true;
        }

        @Override
        public boolean isCancelled() {
            return cancelled;
        }

        @Override
        public boolean isDone() {
            return isDone;
        }

        @Override
        public void then(PromiseFunction<X> onFulfilled) {
            this.onFulfilled = onFulfilled;
        }
<commit_msg>Handle potential race in highly performant remotes.<commit_after>package io.muoncore.api;

import io.muoncore.channel.ChannelConnection;
import org.reactivestreams.Publisher;
import reactor.rx.broadcast.Broadcaster;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * Take a bidirectional channel and add request/ response semantics to
 * it at the code level.
 */
public class ChannelFutureAdapter<Receive, Send> {

    private ChannelConnection<Send, Receive> channelConnection;

    public ChannelFutureAdapter(ChannelConnection<Send, Receive> channelConnection) {
        this.channelConnection = channelConnection;
    }

    public MuonFuture<Receive> request(Send obj) {
        ChannelFuture<Receive> ret = new ChannelFuture<>();

        channelConnection.receive(ret::setData);
        channelConnection.send(obj);

        return ret;
    }

    class ChannelFuture<X> implements MuonFuture<X> {

        final CountDownLatch responseReceivedSignal = new CountDownLatch(1);

        private volatile boolean isDone;
        private X data;
        private boolean cancelled = false;
        private PromiseFunction<X> onFulfilled;
        private Broadcaster<X> broadcast;

        public void setData(X data) {
            if (isDone) return;

            this.data = data;
            if (onFulfilled != null) {
                onFulfilled.call(data);
            }
            if (broadcast != null) {
                broadcast.accept(data);
            }
            shutdown();
            responseReceivedSignal.countDown();
        }

        @Override
        public Publisher<X> toPublisher() {
            if (broadcast == null) {
                broadcast = Broadcaster.create();
            }
            return broadcast;
        }

        @Override
        public boolean cancel(boolean b) {
            if (isDone) return false;
            shutdown();
            isDone = true;
            cancelled = true;
            return true;
        }

        @Override
        public boolean isCancelled() {
            return cancelled;
        }

        @Override
        public boolean isDone() {
            return isDone;
        }

        @Override
        public void then(PromiseFunction<X> onFulfilled) {
            this.onFulfilled = onFulfilled;
            if (this.data != null) {
                onFulfilled.call(data);
            }
        }

{'fraction_non_alphanumeric': 0.08321435855598613, 'fraction_numerical': 0.0004079135223332653, 'mean_word_length': 2.078468298807282, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3227725', 'n_tokens_mistral': 1289, 'n_tokens_neox': 1227, 'n_words': 306}