Search in sources :

Example 1 with Topic

use of com.oath.cyclops.async.adapters.Topic in project cyclops by aol.

the class ReactiveStreamX method broadcast.

@Override
public Topic<T> broadcast() {
    if (async == Type.NO_BACKPRESSURE) {
        Queue<T> queue = QueueFactories.<T>boundedNonBlockingQueue(1000).build().withTimeout(1);
        Topic<T> topic = new Topic<>(queue, QueueFactories.<T>boundedNonBlockingQueue(1000));
        AtomicBoolean wip = new AtomicBoolean(false);
        Continuation[] contRef = { null };
        Continuation cont = new Continuation(() -> {
            if (wip.compareAndSet(false, true)) {
                try {
                    source.subscribeAll(topic::offer, e -> topic.close(), () -> topic.close());
                } finally {
                    wip.set(false);
                }
            }
            return Continuation.empty();
        });
        contRef[0] = cont;
        queue.addContinuation(cont);
        return topic;
    }
    Queue<T> queue = QueueFactories.<T>boundedNonBlockingQueue(1000).build().withTimeout(1);
    Topic<T> topic = new Topic<>(queue, QueueFactories.<T>boundedNonBlockingQueue(1000));
    AtomicBoolean wip = new AtomicBoolean(false);
    Subscription s = source.subscribe(topic::offer, e -> topic.close(), () -> topic.close());
    Continuation[] contRef = { null };
    Continuation cont = new Continuation(() -> {
        if (wip.compareAndSet(false, true)) {
            try {
                // use the first consuming thread to tell this Stream onto the Queue
                s.request(1000 - queue.size());
            } finally {
                wip.set(false);
            }
        }
        return contRef[0];
    });
    contRef[0] = cont;
    queue.addContinuation(cont);
    return topic;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Continuation(com.oath.cyclops.types.futurestream.Continuation) Topic(com.oath.cyclops.async.adapters.Topic) Subscription(org.reactivestreams.Subscription)

Aggregations

Topic (com.oath.cyclops.async.adapters.Topic)1 Continuation (com.oath.cyclops.types.futurestream.Continuation)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Subscription (org.reactivestreams.Subscription)1