|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package dev.cel.runtime; |
| 16 | + |
| 17 | +import static com.google.common.base.Preconditions.checkArgument; |
| 18 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | + |
| 20 | +import com.google.errorprone.annotations.Immutable; |
| 21 | +import java.time.Duration; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +/** |
| 25 | + * Controls when asynchronous evaluation re-evaluates the AST after async completions. |
| 26 | + * |
| 27 | + * <p>The evaluator consults the strategy each time completions are received. |
| 28 | + */ |
| 29 | +@Immutable |
| 30 | +public interface CelAsyncDrainStrategy { |
| 31 | + |
| 32 | + /** |
| 33 | + * Evaluates the current state of asynchronous evaluation and determines the next step. |
| 34 | + * |
| 35 | + * @param completedBatch The batch of async call completions accumulated so far in this drain |
| 36 | + * cycle. |
| 37 | + * @param activeCallsCount The number of async calls currently launched but unresolved. |
| 38 | + */ |
| 39 | + CelAsyncDrainAction nextAction(List<CelAsyncCall> completedBatch, int activeCallsCount); |
| 40 | + |
| 41 | + /** |
| 42 | + * Re-evaluates after a debounce window after the first completion, batching completions that |
| 43 | + * complete at roughly the same time. |
| 44 | + */ |
| 45 | + static CelAsyncDrainStrategy drainReady(Duration debounce) { |
| 46 | + return new DrainReadyStrategy(debounce); |
| 47 | + } |
| 48 | + |
| 49 | + /** Re-evaluates with the default debounce window of 100 microseconds. */ |
| 50 | + static CelAsyncDrainStrategy drainReady() { |
| 51 | + return drainReady(Duration.ofNanos(100_000)); |
| 52 | + } |
| 53 | + |
| 54 | + /** Re-evaluates immediately as soon as any single call completes. */ |
| 55 | + static CelAsyncDrainStrategy drainNone() { |
| 56 | + return (completed, active) -> { |
| 57 | + checkNotNull(completed, "completedBatch must not be null"); |
| 58 | + checkArgument(active >= 0, "activeCallsCount must be non-negative: %s", active); |
| 59 | + return active == 0 || !completed.isEmpty() |
| 60 | + ? CelAsyncDrainAction.reevaluate() |
| 61 | + : CelAsyncDrainAction.waitForMore(); |
| 62 | + }; |
| 63 | + } |
| 64 | + |
| 65 | + /** Waits for all currently pending calls to finish before re-evaluating. */ |
| 66 | + static CelAsyncDrainStrategy drainAll() { |
| 67 | + return (completed, active) -> { |
| 68 | + checkNotNull(completed, "completedBatch must not be null"); |
| 69 | + checkArgument(active >= 0, "activeCallsCount must be non-negative: %s", active); |
| 70 | + return active == 0 ? CelAsyncDrainAction.reevaluate() : CelAsyncDrainAction.waitForMore(); |
| 71 | + }; |
| 72 | + } |
| 73 | + |
| 74 | + /** Internal implementation of the drain ready strategy with configurable debounce duration. */ |
| 75 | + @Immutable |
| 76 | + final class DrainReadyStrategy implements CelAsyncDrainStrategy { |
| 77 | + private final Duration debounce; |
| 78 | + |
| 79 | + DrainReadyStrategy(Duration debounce) { |
| 80 | + this.debounce = checkNotNull(debounce); |
| 81 | + checkArgument(!debounce.isNegative(), "debounce duration must not be negative"); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public CelAsyncDrainAction nextAction(List<CelAsyncCall> completedBatch, int activeCallsCount) { |
| 86 | + checkNotNull(completedBatch, "completedBatch must not be null"); |
| 87 | + checkArgument( |
| 88 | + activeCallsCount >= 0, "activeCallsCount must be non-negative: %s", activeCallsCount); |
| 89 | + if (activeCallsCount == 0) { |
| 90 | + return CelAsyncDrainAction.reevaluate(); |
| 91 | + } |
| 92 | + if (completedBatch.isEmpty()) { |
| 93 | + return CelAsyncDrainAction.waitForMore(); |
| 94 | + } |
| 95 | + if (debounce.isZero()) { |
| 96 | + return CelAsyncDrainAction.reevaluate(); |
| 97 | + } |
| 98 | + return CelAsyncDrainAction.waitDuration(debounce); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments