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

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

There is a newer version: 2.0.0-rc-1
Show newest version
/*
 * Copyright 2015 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.Subscriber;
import ratpack.exec.Promise;
import ratpack.func.Function;
import ratpack.stream.TransformablePublisher;
import ratpack.stream.YieldRequest;

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

public class FlatYieldingPublisher implements TransformablePublisher {

  private final Function> producer;
  private final AtomicLong subscriptionCounter = new AtomicLong();

  public FlatYieldingPublisher(Function> producer) {
    this.producer = producer;
  }

  @Override
  public void subscribe(final Subscriber subscriber) {
    new Subscription(subscriber);
  }

  private class Subscription extends SubscriptionSupport {

    private final long subscriptionNum = subscriptionCounter.getAndIncrement();
    private final AtomicInteger requestCounter = new AtomicInteger();
    private final AtomicLong waiting = new AtomicLong();
    private final AtomicBoolean draining = new AtomicBoolean();

    public Subscription(Subscriber subscriber) {
      super(subscriber);
      start();
    }

    @Override
    protected void doRequest(long n) {
      waiting.addAndGet(n);
      drain();
    }

    private void drain() {
      if (!isStopped() && draining.compareAndSet(false, true)) {
        long l = waiting.getAndDecrement();
        if (l > 0) {
          try {
            Promise promise = producer.apply(new DefaultYieldRequest(subscriptionNum, requestCounter.getAndIncrement()));
            promise
              .wiretap(r ->
                this.draining.set(false)
              )
              .onError(this::onError)
              .then(t -> {
                if (t == null) {
                  onComplete();
                } else {
                  onNext(t);
                  drain();
                }
              });
          } catch (Throwable e) {
            draining.set(false);
            onError(e);
            return;
          }
        } else {
          waiting.addAndGet(1);
          draining.set(false);
        }
        if (waiting.get() > 0) {
          drain();
        }
      }
    }

  }

}






© 2015 - 2024 Weber Informatics LLC | Privacy Policy