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

ratpack.exec.internal.CachingPromise 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.exec.internal;

import ratpack.exec.*;
import ratpack.func.Action;
import ratpack.func.Function;
import ratpack.func.NoArgAction;
import ratpack.func.Predicate;

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.function.Supplier;

public class CachingPromise implements Promise {

  private final Consumer> fulfillment;
  private final Supplier executionSupplier;
  private final Action errorHandler;

  private final AtomicBoolean fired = new AtomicBoolean();

  private final Queue waiting = new ConcurrentLinkedQueue<>();
  private final AtomicBoolean draining = new AtomicBoolean();
  private final AtomicReference> result = new AtomicReference<>();

  public CachingPromise(Consumer> fulfillment, Supplier executionSupplier, Action errorHandler) {
    this.fulfillment = fulfillment;
    this.executionSupplier = executionSupplier;
    this.errorHandler = errorHandler;
  }

  private class Job {
    final Fulfiller fulfiller;
    final ExecutionBacking.StreamHandle streamHandle;

    private Job(Fulfiller fulfiller, ExecutionBacking.StreamHandle streamHandle) {
      this.fulfiller = fulfiller;
      this.streamHandle = streamHandle;
    }
  }

  @Override
  public SuccessPromise onError(Action errorHandler) {
    return new DefaultSuccessPromise<>(executionSupplier, new Fulfillment(), errorHandler);
  }

  @Override
  public void then(Action then) {
    newPromise().then(then);
  }

  private DefaultSuccessPromise newPromise() {
    return new DefaultSuccessPromise<>(executionSupplier, new Fulfillment(), errorHandler);
  }

  @Override
  public  Promise map(Function transformer) {
    return newPromise().map(transformer);
  }

  @Override
  public  Promise blockingMap(Function transformer) {
    return newPromise().blockingMap(transformer);
  }

  @Override
  public  Promise flatMap(Function> transformer) {
    return newPromise().flatMap(transformer);
  }

  @Override
  public Promise route(Predicate predicate, Action action) {
    return newPromise().route(predicate, action);
  }

  @Override
  public Promise onNull(NoArgAction action) {
    return newPromise().onNull(action);
  }

  @Override
  public Promise defer(Action releaser) {
    return newPromise().defer(releaser);
  }

  @Override
  public Promise onYield(Runnable onYield) {
    return newPromise().onYield(onYield);
  }

  @Override
  public Promise wiretap(Action> listener) {
    return newPromise().wiretap(listener);
  }

  @Override
  public Promise throttled(Throttle throttle) {
    return newPromise().throttled(throttle);
  }

  @Override
  public Promise cache() {
    return this;
  }

  private void tryDrain() {
    if (draining.compareAndSet(false, true)) {
      try {
        Result result = this.result.get();

        Job job = waiting.poll();
        while (job != null) {
          Job finalJob = job;
          job.streamHandle.complete(() -> finalJob.fulfiller.accept(result));
          job = waiting.poll();
        }
      } finally {
        draining.set(false);
      }
    }
    if (!draining.get() && !waiting.isEmpty()) {
      tryDrain();
    }
  }

  private class Fulfillment implements Consumer> {

    @Override
    public void accept(Fulfiller fulfiller) {
      if (fired.compareAndSet(false, true)) {
        fulfillment.accept(new Fulfiller() {
          @Override
          public void error(Throwable throwable) {
            result.set(Result.failure(throwable));
            fulfiller.error(throwable);
            executionSupplier.get().getEventLoop().execute(CachingPromise.this::tryDrain);
          }

          @Override
          public void success(T value) {
            result.set(Result.success(value));
            fulfiller.success(value);
            executionSupplier.get().getEventLoop().execute(CachingPromise.this::tryDrain);
          }
        });
      } else {
        executionSupplier.get().streamSubscribe((streamHandle) -> {
          waiting.add(new Job(fulfiller, streamHandle));
          if (result.get() != null) {
            tryDrain();
          }
        });
      }
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy