ratpack.stream.internal.BufferingPublisher Maven / Gradle / Ivy
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ratpack.stream.internal;
import io.netty.util.internal.PlatformDependent;
import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ratpack.func.Action;
import ratpack.func.Function;
import ratpack.stream.TransformablePublisher;
import java.util.Deque;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
public class BufferingPublisher implements TransformablePublisher {
private static final Logger LOGGER = LoggerFactory.getLogger(BufferingPublisher.class);
private static final Object ON_COMPLETE = new Object();
private static final Object ON_ERROR = new Object();
private static final Object CANCEL = new Object();
private final Action super T> disposer;
private final Function super BufferedWriteStream, Subscription> function;
public BufferingPublisher(Action super T> disposer, Publisher publisher) {
this(disposer, write -> {
return new ConnectingSubscriber<>(publisher, write);
});
}
public BufferingPublisher(Action super T> disposer, Function super BufferedWriteStream, Subscription> function) {
this.disposer = disposer;
this.function = function;
}
@Override
public void subscribe(final Subscriber super T> subscriber) {
new BufferingSubscription(subscriber);
}
private static class ConnectingSubscriber implements Subscriber, Subscription {
private final Publisher publisher;
private final BufferedWriteStream write;
private volatile Subscription upstream;
private final AtomicBoolean connected = new AtomicBoolean();
private final AtomicBoolean draining = new AtomicBoolean();
private final Queue
© 2015 - 2025 Weber Informatics LLC | Privacy Policy