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;
}
Aggregations