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

ratpack.exec.internal.CachingUpstream 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 java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

public class CachingUpstream implements Upstream {

  private Upstream upstream;
  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 CachingUpstream(Upstream upstream) {
    this.upstream = upstream;
  }

  private class Job {
    final Downstream downstream;
    final ExecutionBacking.StreamHandle streamHandle;

    private Job(Downstream downstream, ExecutionBacking.StreamHandle streamHandle) {
      this.downstream = downstream;
      this.streamHandle = streamHandle;
    }
  }

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

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

  @Override
  public void connect(Downstream downstream) throws Exception {
    if (fired.compareAndSet(false, true)) {
      upstream.connect(new Downstream() {
        @Override
        public void error(Throwable throwable) {
          result.set(new ResultBackedExecResult<>(Result.error(throwable), Execution.current()));
          doDrainInNewSegment();
          downstream.error(throwable);
        }

        @Override
        public void success(T value) {
          result.set(new ResultBackedExecResult<>(Result.success(value), Execution.current()));
          doDrainInNewSegment();
          downstream.success(value);
        }

        @Override
        public void complete() {
          result.set(new CompleteExecResult<>(Execution.current()));
          doDrainInNewSegment();
          downstream.complete();
        }
      });
    } else {
      ExecutionBacking.require().streamSubscribe((streamHandle) -> {
        waiting.add(new Job(downstream, streamHandle));
        if (result.get() != null) {
          tryDrain();
        }
      });
    }
  }

  private void doDrainInNewSegment() {
    this.upstream = null; // release
    ExecutionBacking.require().getEventLoop().execute(this::tryDrain);
  }

}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy