...

Source file src/google.golang.org/grpc/orca/internal/internal.go

Documentation: google.golang.org/grpc/orca/internal

     1  /*
     2   *
     3   * Copyright 2022 gRPC authors.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   */
    18  
    19  // Package internal contains orca-internal code, for testing purposes and to
    20  // avoid polluting the godoc of the top-level orca package.
    21  package internal
    22  
    23  import (
    24  	"errors"
    25  	"fmt"
    26  
    27  	ibackoff "google.golang.org/grpc/internal/backoff"
    28  	"google.golang.org/grpc/metadata"
    29  	"google.golang.org/protobuf/proto"
    30  
    31  	v3orcapb "github.com/cncf/xds/go/xds/data/orca/v3"
    32  )
    33  
    34  // AllowAnyMinReportingInterval prevents clamping of the MinReportingInterval
    35  // configured via ServiceOptions, to a minimum of 30s.
    36  //
    37  // For testing purposes only.
    38  var AllowAnyMinReportingInterval any // func(*ServiceOptions)
    39  
    40  // DefaultBackoffFunc is used by the producer to control its backoff behavior.
    41  //
    42  // For testing purposes only.
    43  var DefaultBackoffFunc = ibackoff.DefaultExponential.Backoff
    44  
    45  // TrailerMetadataKey is the key in which the per-call backend metrics are
    46  // transmitted.
    47  const TrailerMetadataKey = "endpoint-load-metrics-bin"
    48  
    49  // ToLoadReport unmarshals a binary encoded [ORCA LoadReport] protobuf message
    50  // from md and returns the corresponding struct. The load report is expected to
    51  // be stored as the value for key "endpoint-load-metrics-bin".
    52  //
    53  // If no load report was found in the provided metadata, if multiple load
    54  // reports are found, or if the load report found cannot be parsed, an error is
    55  // returned.
    56  //
    57  // [ORCA LoadReport]: (https://github.com/cncf/xds/blob/main/xds/data/orca/v3/orca_load_report.proto#L15)
    58  func ToLoadReport(md metadata.MD) (*v3orcapb.OrcaLoadReport, error) {
    59  	vs := md.Get(TrailerMetadataKey)
    60  	if len(vs) == 0 {
    61  		return nil, nil
    62  	}
    63  	if len(vs) != 1 {
    64  		return nil, errors.New("multiple orca load reports found in provided metadata")
    65  	}
    66  	ret := new(v3orcapb.OrcaLoadReport)
    67  	if err := proto.Unmarshal([]byte(vs[0]), ret); err != nil {
    68  		return nil, fmt.Errorf("failed to unmarshal load report found in metadata: %v", err)
    69  	}
    70  	return ret, nil
    71  }
    72  

View as plain text