// Copyright 2025 Envoy Project 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.

syntax = "proto3";

package envoy.extensions.filters.http.adaptive_concurrency.v3;

import "envoy/config/core/v3/base.proto";
import "envoy/type/v3/http_status.proto";
import "envoy/type/v3/percent.proto";

import "google/protobuf/duration.proto";
import "google/protobuf/wrappers.proto";

import "udpa/annotations/status.proto";
import "udpa/annotations/versioning.proto";
import "validate/validate.proto";

option java_package = "io.envoyproxy.envoy.extensions.filters.http.adaptive_concurrency.v3";
option java_outer_classname = "AdaptiveConcurrencyProto";
option java_multiple_files = true;
option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/http/adaptive_concurrency/v3;adaptive_concurrencyv3";
option (udpa.annotations.file_status).package_version_status = ACTIVE;

// [#protodoc-title: Adaptive Concurrency]
// Adaptive Concurrency Control :ref:`configuration overview
// <config_http_filters_adaptive_concurrency>`.
// [#extension: envoy.filters.http.adaptive_concurrency]

// Configuration parameters for the gradient controller.
message GradientControllerConfig {
  option (udpa.annotations.versioning).previous_message_type =
      "envoy.config.filter.http.adaptive_concurrency.v2alpha.GradientControllerConfig";

  // Parameters controlling the periodic recalculation of the concurrency limit from sampled request
  // latencies.
  message ConcurrencyLimitCalculationParams {
    option (udpa.annotations.versioning).previous_message_type =
        "envoy.config.filter.http.adaptive_concurrency.v2alpha.GradientControllerConfig."
        "ConcurrencyLimitCalculationParams";

    // The allowed upper-bound on the calculated concurrency limit. Defaults to 1000.
    google.protobuf.UInt32Value max_concurrency_limit = 2 [(validate.rules).uint32 = {gt: 0}];

    // The period of time samples are taken to recalculate the concurrency limit.
    google.protobuf.Duration concurrency_update_interval = 3 [(validate.rules).duration = {
      required: true
      gt {}
    }];
  }

  // Parameters controlling the periodic minRTT recalculation.
  // [#next-free-field: 7]
  message MinimumRTTCalculationParams {
    option (udpa.annotations.versioning).previous_message_type =
        "envoy.config.filter.http.adaptive_concurrency.v2alpha.GradientControllerConfig."
        "MinimumRTTCalculationParams";

    // The time interval between recalculating the minimum request round-trip time. Has to be
    // positive. If set to zero, dynamic sampling of the minRTT is disabled.
    google.protobuf.Duration interval = 1 [(validate.rules).duration = {gte {nanos: 1000000}}];

    // The fixed value for the minRTT. This value is used when minRTT is not sampled dynamically.
    // If dynamic sampling of the minRTT is disabled, this field must be set.
    google.protobuf.Duration fixed_value = 6 [(validate.rules).duration = {gt {}}];

    // The number of requests to aggregate/sample during the minRTT recalculation window before
    // updating. Defaults to 50.
    google.protobuf.UInt32Value request_count = 2 [(validate.rules).uint32 = {gt: 0}];

    // Randomized time delta that will be introduced to the start of the minRTT calculation window.
    // This is represented as a percentage of the interval duration. Defaults to 15%.
    //
    // Example: If the interval is 10s and the jitter is 15%, the next window will begin
    // somewhere in the range (10s - 11.5s).
    type.v3.Percent jitter = 3;

    // The concurrency limit set while measuring the minRTT. Defaults to 3.
    google.protobuf.UInt32Value min_concurrency = 4 [(validate.rules).uint32 = {gt: 0}];

    // Amount added to the measured minRTT to add stability to the concurrency limit during natural
    // variability in latency. This is expressed as a percentage of the measured value and can be
    // adjusted to allow more or less tolerance to the sampled latency values.
    //
    // Defaults to 25%.
    type.v3.Percent buffer = 5;
  }

  // The percentile to use when summarizing aggregated samples. Defaults to p50.
  type.v3.Percent sample_aggregate_percentile = 1;

  ConcurrencyLimitCalculationParams concurrency_limit_params = 2
      [(validate.rules).message = {required: true}];

  MinimumRTTCalculationParams min_rtt_calc_params = 3 [(validate.rules).message = {required: true}];
}

message AdaptiveConcurrency {
  option (udpa.annotations.versioning).previous_message_type =
      "envoy.config.filter.http.adaptive_concurrency.v2alpha.AdaptiveConcurrency";

  oneof concurrency_controller_config {
    option (validate.required) = true;

    // Gradient concurrency control will be used.
    GradientControllerConfig gradient_controller_config = 1
        [(validate.rules).message = {required: true}];
  }

  // If set to false, the adaptive concurrency filter will operate as a pass-through filter. If the
  // message is unspecified, the filter will be enabled.
  config.core.v3.RuntimeFeatureFlag enabled = 2;

  // This field allows for a custom HTTP response status code to the downstream client when
  // the concurrency limit has been exceeded.
  // Defaults to 503 (Service Unavailable).
  //
  // .. note::
  //   If this is set to < 400, 503 will be used instead.
  type.v3.HttpStatus concurrency_limit_exceeded_status = 3;
}
