All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ratpack.stream.internal.BufferingPublisher Maven / Gradle / Ivy

There is a newer version: 2.0.0-rc-1
Show newest version
/*
 * Copyright 2014 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 org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;

import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

public class BufferingPublisher implements Publisher {

  private final AtomicInteger wanted = new AtomicInteger();
  private final ConcurrentLinkedQueue buffer = new ConcurrentLinkedQueue<>();
  private final AtomicBoolean finished = new AtomicBoolean();
  private final AtomicBoolean draining = new AtomicBoolean();

  private final Publisher publisher;

  public BufferingPublisher(Publisher publisher) {
    this.publisher = publisher;
  }

  @Override
  public void subscribe(final Subscriber subscriber) {
    publisher.subscribe(new Subscriber() {
      public Subscription subscription;

      private void tryDrain() {
        if (draining.compareAndSet(false, true)) {
          try {
            int i = wanted.get();
            while (i > 0) {
              T item = buffer.poll();
              if (item == null) {
                if (finished.get()) {
                  subscriber.onComplete();
                  return;
                } else {
                  break;
                }
              } else {
                subscriber.onNext(item);
                i = wanted.decrementAndGet();
              }
            }
          } finally {
            draining.set(false);
          }
          if (buffer.peek() != null && wanted.get() > 0) {
            tryDrain();
          }
        }
      }

      @Override
      public void onSubscribe(final Subscription subscription) {
        this.subscription = subscription;
        subscription.request(Integer.MAX_VALUE);

        subscriber.onSubscribe(new Subscription() {
          @Override
          public void request(int n) {
            wanted.addAndGet(n);
            tryDrain();
          }

          @Override
          public void cancel() {
            finished.compareAndSet(false, true);
            subscription.cancel();
          }
        });
      }

      @Override
      public void onNext(T t) {
        buffer.add(t);
        tryDrain();
      }

      @Override
      public void onError(Throwable t) {
        buffer.clear();
        finished.compareAndSet(false, true);
        subscriber.onError(t);
      }

      @Override
      public void onComplete() {
        finished.compareAndSet(false, true);
        tryDrain();
      }
    });

  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy