|
| 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 com.google.auto.value.AutoValue; |
| 18 | +import javax.annotation.concurrent.ThreadSafe; |
| 19 | +import java.util.Optional; |
| 20 | +import java.util.concurrent.Executors; |
| 21 | +import java.util.concurrent.ScheduledExecutorService; |
| 22 | +import java.util.concurrent.atomic.AtomicLong; |
| 23 | + |
| 24 | +/** Options for configuring asynchronous CEL evaluation. */ |
| 25 | +@AutoValue |
| 26 | +@ThreadSafe |
| 27 | +public abstract class CelAsyncEvaluationOptions { |
| 28 | + |
| 29 | + private static final int DEFAULT_MAX_CONCURRENCY = 100; |
| 30 | + private static final int DEFAULT_MAX_ITERATIONS = 1_000; |
| 31 | + |
| 32 | + /** |
| 33 | + * Maximum number of concurrent async function calls in-flight simultaneously. A value <= 0 |
| 34 | + * indicates unbounded concurrency. |
| 35 | + */ |
| 36 | + public abstract int maxConcurrency(); |
| 37 | + |
| 38 | + /** Strategy governing when to trigger re-evaluation after async call completions. */ |
| 39 | + public abstract CelAsyncDrainStrategy drainStrategy(); |
| 40 | + |
| 41 | + /** Safety cap on the maximum number of AST re-evaluation passes before aborting. */ |
| 42 | + public abstract int maxIterations(); |
| 43 | + |
| 44 | + /** |
| 45 | + * Returns the custom configured {@link ScheduledExecutorService}, if present. |
| 46 | + * |
| 47 | + * <p>If absent, {@link #resolveScheduledExecutorService()} falls back to an internal, shared |
| 48 | + * single-threaded daemon scheduler. |
| 49 | + */ |
| 50 | + public abstract Optional<ScheduledExecutorService> scheduledExecutorService(); |
| 51 | + |
| 52 | + /** Returns the configured lifecycle observer, if present. */ |
| 53 | + public abstract Optional<CelAsyncObserver> observer(); |
| 54 | + |
| 55 | + /** |
| 56 | + * Resolves the {@link ScheduledExecutorService} used for debounce timers, falling back to a |
| 57 | + * shared, lazily initialized single-threaded daemon scheduler (named {@code |
| 58 | + * cel-async-debounce-*}) if not custom-configured. |
| 59 | + * |
| 60 | + * <p>The scheduler is used exclusively as an alarm clock to trigger continuation wakeups; it does |
| 61 | + * not execute CEL evaluation tasks. |
| 62 | + */ |
| 63 | + public ScheduledExecutorService resolveScheduledExecutorService() { |
| 64 | + return scheduledExecutorService().orElse(DefaultDebounceSchedulerHolder.INSTANCE); |
| 65 | + } |
| 66 | + |
| 67 | + public abstract Builder toBuilder(); |
| 68 | + |
| 69 | + /** |
| 70 | + * Returns a new {@link Builder} initialized with standard default options: |
| 71 | + * |
| 72 | + * <ul> |
| 73 | + * <li>Maximum concurrency: 100 in-flight calls |
| 74 | + * <li>Maximum iterations: 1,000 evaluation passes |
| 75 | + * <li>Drain strategy: {@link CelAsyncDrainStrategy#drainReady()} (100-microsecond debounce |
| 76 | + * window) |
| 77 | + * <li>Scheduled executor service: A shared, lazily initialized single-threaded daemon scheduler |
| 78 | + * used exclusively for debounce timer wakeups. |
| 79 | + * </ul> |
| 80 | + */ |
| 81 | + public static Builder newBuilder() { |
| 82 | + return new AutoValue_CelAsyncEvaluationOptions.Builder() |
| 83 | + .setMaxConcurrency(DEFAULT_MAX_CONCURRENCY) |
| 84 | + .setDrainStrategy(CelAsyncDrainStrategy.drainReady()) |
| 85 | + .setMaxIterations(DEFAULT_MAX_ITERATIONS); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Returns a new {@link Builder} initialized with standard default options. |
| 90 | + * |
| 91 | + * <p>Equivalent to calling {@link #newBuilder()}. |
| 92 | + */ |
| 93 | + public static Builder builder() { |
| 94 | + return newBuilder(); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Returns a {@link CelAsyncEvaluationOptions} instance with the {@link #newBuilder() default |
| 99 | + * configuration}. |
| 100 | + */ |
| 101 | + public static CelAsyncEvaluationOptions defaultOptions() { |
| 102 | + return newBuilder().build(); |
| 103 | + } |
| 104 | + |
| 105 | + private static final class DefaultDebounceSchedulerHolder { |
| 106 | + private static final AtomicLong counter = new AtomicLong(); |
| 107 | + private static final ScheduledExecutorService INSTANCE = |
| 108 | + Executors.newSingleThreadScheduledExecutor( |
| 109 | + r -> { |
| 110 | + Thread t = new Thread(r); |
| 111 | + t.setName("cel-async-debounce-" + counter.getAndIncrement()); |
| 112 | + t.setDaemon(true); |
| 113 | + return t; |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + /** Builder for {@link CelAsyncEvaluationOptions}. */ |
| 118 | + @AutoValue.Builder |
| 119 | + public abstract static class Builder { |
| 120 | + public abstract Builder setMaxConcurrency(int maxConcurrency); |
| 121 | + |
| 122 | + public abstract Builder setDrainStrategy(CelAsyncDrainStrategy drainStrategy); |
| 123 | + |
| 124 | + public abstract Builder setMaxIterations(int maxIterations); |
| 125 | + |
| 126 | + /** |
| 127 | + * Sets a custom {@link ScheduledExecutorService} for debounce timers. |
| 128 | + * |
| 129 | + * <p>If not set, defaults to an internal, shared single-threaded daemon scheduler. |
| 130 | + */ |
| 131 | + public abstract Builder setScheduledExecutorService( |
| 132 | + ScheduledExecutorService scheduledExecutorService); |
| 133 | + |
| 134 | + public abstract Builder setObserver(CelAsyncObserver observer); |
| 135 | + |
| 136 | + public abstract CelAsyncEvaluationOptions build(); |
| 137 | + } |
| 138 | + |
| 139 | + // Package-private constructor prevents extension outside package while allowing AutoValue. |
| 140 | + CelAsyncEvaluationOptions() {} |
| 141 | +} |
0 commit comments